Button 按钮部件
Button(按钮)部件是一个标准的Tkinter窗口部件,用以实现各种按钮
案列如下:
点击hit me后
代码如下:
import tkinter as tk
window = tk.Tk()
window.title('My window')
window.geometry('600x400')
var = tk.StringVar() #将Label标签的内容设置为字符类型,用var来接收hit_me函数传出的内容,显示在标签上
l = tk.Label(window, textvariable=var, bg='green', fg='white', font=('Arial', 28), width=30, height=2)
#其中,bg为背景色,fg为字体色,font为字体
l.pack()
on_hit = False
def hit_me():
global on_hit
if on_hit == False:
on_hit = True
var.set('I ❤ you')
else:
on_hit = False
var.set('')
b = tk.Button(window, text='hit me', font=('Arial', 12), width=10, height=1, command=hit_me)
#在窗口设置Button按键
b.pack()
window.mainloop()