'''Button按钮 点击执行对应的命令'''
import tkinter as tk
#初始化窗口
window = tk.Tk()
#窗口名称
window.title("My Window")
#窗口大小,是 x 不是 *
window.geometry("400x400")
#创建对象num,用来计数
num = 0
label = tk.Label(window,text="Hello World",height=2,width=20,fg="green")
label.pack()
def hit_me():
#使用全局变量
global num
num = num + 1
text = "you hit " + str(num)
#配置label的属性
label.config(text=text)
def forget_button():
button.forget()
# 创建按钮,这里的command是调用事件
def b_pack():
button.pack()
button = tk.Button(window,text="hit_me",fg="red",command=hit_me)
button.pack()
# 使button不可显示
b_forget = tk.Button(window,text="forget_button",command=forget_button)
b_forget.pack()
# 再次使button显示
button_p = tk.Button(window,text="button_pack",command=b_pack).pack()
# 循环窗口
window.mainloop()