tkinter的按鈕很丑也很難改
怎么辦呢?
最好的方法就是不用按鈕!
給Label添加點擊事件,和按鈕的作用是一樣的!
代碼:
#!/usr/bin/env python # -*- coding: utf-8 -*- # author:洪衛 import tkinter as tk # 使用Tkinter前需要先導入 # 第1步,實例化object,建立窗口window window = tk.Tk() # 第2步,給窗口的可視化起名字 window.title('My Window') # 第3步,設定窗口的大小(長 * 寬) window.geometry('500x300') # 這里的乘是小x # 第4步,在圖形界面上設定標簽 var = tk.StringVar() # 將label標簽的內容設置為字符類型,用var來接收hit_me函數的傳出內容用以顯示在標簽上 l = tk.Label(window, textvariable=var, bg='green', fg='white', font=('Arial', 12), width=30, height=2) # 說明: bg為背景,fg為字體顏色,font為字體,width為長,height為高,這里的長和高是字符的長和高,比如height=2,就是標簽有2個字符這么高 l.pack() # 定義一個函數功能(內容自己自由編寫),供點擊Button按鍵時調用,調用命令參數command=函數名 on_hit = False def hit_me(self=None): global on_hit if on_hit == False: on_hit = True var.set('you hit me') else: on_hit = False var.set('') # 第5步,在窗口界面設置放置Button按鍵 b = tk.Button(window, text='hit me', font=('Arial', 12), width=10, height=1, command=hit_me) b.pack() # 在窗口界面設置放置Label # Creating a photoimage object to use image search_photo = tk.PhotoImage(file=r"search.png") # 調整圖片尺寸適應按鈕大小 search_photoimage = search_photo.subsample(9, 9) search_b = tk.Label(window, text='', image=search_photoimage, relief=tk.FLAT, bg="white", activebackground='white') search_b.bind('<Button-1>', hit_me) search_b.pack() # 第6步,主窗口循環顯示 window.mainloop()
效果: