源代码:
# 创建主窗口
import tkinter as tk
window = tk.Tk()
window.title('勾选项')
window.geometry('300x200')
# 创建标签,用于显示勾选中的内容
l = tk.Label(window, bg='green', width=25, height=2, text='empty')
l.pack()
var1 = tk.IntVar() # 用来储存下面勾选项1中返回的0或1
var2 = tk.IntVar() # 用来储存下面勾选项2中返回的0或1
# 在标签中打印内容
def print_selection():
if (var1.get()==1) and (var2.get()==0): # 只勾选选项1
l.config(text='未来是人工智能的时代')
elif (var1.get()==1) and (var2.get()==1): # 勾选选项1和2
l.config(text='未来是人工智能和区块链的时代')
elif (var1.get()==0) and (var2.get()==0): # 选项1和2都不勾选
l.config(text='未来是不确定的时代')
else: # 只勾选选项2
l.config(text='未来是区块链的时代')
# 创建勾选项
c1 = tk.Checkbutton(window, text='人工智能',
variable=var1,
onvalue=1, # 勾选中该项时,把1放入var1
offvalue=0, # 不勾选中该项时,把0放入var1
command=print_selection,
)
c1.pack()
c2 = tk.Checkbutton(window, text='区块链',
variable=var2,
onvalue=1, # 勾选中该项时,把1放入var1
offvalue=0, # 不勾选中该项时,把0放入var1
command=print_selection,
)
c2.pack()
window.mainloop()