checkbutton(多个)
代表一个变量,具有两个不同值。点击这个按钮将会在这两个值之间切换。
案列如下:
代码如下:
import tkinter as tk
window = tk.Tk()
window.title ('My window')
window.geometry('600x400')
l = tk.Label(window, bg='yellow', width=20, text='empty',font=('Arial',18))
l.pack()
def print_selection():
if (var1.get() == 1) & (var2.get() == 0):
l.config (text='I love Python')
elif (var1.get() == 0) & (var2.get() == 1):
l.config (text = 'I love c++')
elif (var1.get() == 0) & (var2.get() == 0):
l.config (text = 'none')
else:
l.config (text = 'I love both')
var1 = tk.IntVar() #记录数值
var2 = tk.IntVar()
c1 = tk.Checkbutton(window, text='Python', variable=var1, onvalue=1, offvalue=0, command=print_selection) #onvalue 选定,offvalue不选定
c1.pack()
c2 = tk.Checkbutton(window, text='C++', variable=var2, onvalue=1, offvalue=0, command=print_selection)
c2.pack()