1 import tkinter as tk 2 3 # 实例化一个窗口对象 4 window = tk.Tk() 5 # 设置title 6 window.title('my window') 7 # 设置大小,XxY的字符串格式 8 window.geometry('600x400') 9 10 l = tk.Label(window, bg='yellow', width=100, text='empty') 11 l.pack() 12 13 def print_selection(): 14 # 根据var.get到的数字做判断,通过l.config传递给label 15 if var1.get() and var2.get(): 16 l.config(text='i love both') 17 elif var1.get() or var2.get(): 18 text = 'Python' if var1.get() else 'C++' 19 l.config(text='i love ' + text) 20 else: 21 l.config(text='i don\'t like either') 22 23 24 var1 = tk.IntVar() # 接收到的value是数字 25 var2 = tk.IntVar() 26 c1 = tk.Checkbutton(window,text='Python',variable=var1,onvalue=1,offvalue=0, 27 command=print_selection) 28 # checkbutton onvalue=1,offvalue=0 -> 选中状态value=1,没选中value=0 , variable=var1 -> value传递给var1 29 c2 = tk.Checkbutton(window,text='C++',variable=var2,onvalue=1,offvalue=0, 30 command=print_selection) 31 c1.pack() 32 c2.pack() 33 34 window.mainloop() # 循环显示
标签:var1,get,text,value,复选框,window,tk,tkinter From: https://www.cnblogs.com/kunshanpipixia/p/17872436.html