Python窗口学习之搜索框美化

初学tkinter,感觉这个插件虽然是做界面的,但是没有html,也没有android那么人性化

既没有画圆角长方形的办法也没有添加透明按钮的办法(可能是我没找到)

所以自己用canvas画了两个扇形和一个长方形拼起来哈哈哈哈哈

在canvas上叠加输入框、搜索label和删除label

代码:

import ctypes
import tkinter as tk
from tkinter import *

# 实例化object,建立窗口window
window = Tk()

# 设定窗口的大小(长 * 宽)
window.geometry('1200x600')

# 将label标签的内容设置为字符类型,用var_search来接收内容用以显示在标签上
var_search = tk.StringVar()

#使窗口更加高清
# 告诉操作系统使用程序自身的dpi适配
ctypes.windll.shcore.SetProcessDpiAwareness(1)

# 在图形界面上创建 500 * 200 大小的画布并放置各种元素
canvas = tk.Canvas(window)

# 设置输入框在画布中上下左右的位置
top, bottom, left, right =  0, 40, 30, 800

#绘制长方形
rect = canvas.create_rectangle(left, top, right, bottom, outline="white", fill="white")

#绘制两个半圆
arc = canvas.create_arc(left - 30, top, left + 30, bottom, start=90, extent=180, outline="white",
                        fill="white")  # 画扇形 从0度打开收到180度结束
arce = canvas.create_arc(right - 30, top, right + 30, bottom, start=270, extent=180, outline="white",
                         fill="white")  # 画扇形 从0度打开收到180度结束

#防止画布
canvas.place(x=30, y=10, height=40, relwidth=1)

# 在图形界面上设定输入框控件entry框并放置
entry_search = tk.Entry(window, textvariable=var_search, font=("黑体", 14), relief=FLAT)
entry_search.place(x=60, y=15, height=30, width=700)


#搜索
def searc(self):
    print(var_search.get())

#清空输入框
def delete(self):
    entry_search.delete(0, END)

# Enter键搜索
window.bind('<Return>', searc)

# 在窗口界面设置放置Label
# Creating a photoimage object to use image
search_photo = PhotoImage(file=r"search.png")

# 调整图片尺寸适应按钮大小
search_photoimage = search_photo.subsample(9, 9)
search_b = tk.Label(window, text='', image=search_photoimage, relief=FLAT, bg="white", activebackground='white')
search_b.bind('<Button-1>', searc)
search_b.place(x=38, y=20, width=20, height=20, )  # 在窗口界面设置放置Button按键

# 在窗口界面设置放置Label
# Creating a photoimage object to use image
delete_photo = PhotoImage(file=r"delete.png")

# 调整图片尺寸适应按钮大小
delete_photoimage = delete_photo.subsample(9, 9)
delete_b = tk.Label(window, text='', image=delete_photoimage, relief=FLAT, bg="white", activebackground='white')
delete_b.bind('<Button-1>', delete)
delete_b.place(x=830, y=20, width=20, height=20, )  # 在窗口界面设置放置Button按键

window.mainloop()

 

效果:

 

posted @ 2022-01-14 21:55  我试试这个昵称好使不  阅读(416)  评论(0编辑  收藏  举报