1. 程式人生 > 其它 >Python視窗學習之搜尋框美化

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()

效果: