import tkinter
root = tkinter.Tk()
root.title("复选框的使用")
root.geometry("360x320")
lb1=tkinter.Label(root,text='爱好列表:',font=("黑体",12))
lb1.place(x=160,y=5)
btninvert=tkinter.Button(root,text="反选",command= lambda:invert(),width=5,font=("微软雅黑",12))
btninvert.place(x=40,y=260)
btnall=tkinter.Button(root,text="全选",command=lambda:all(),width=5,font=("微软雅黑",12))
btnall.place(x=120,y=260)
btncancel=tkinter.Button(root,text="重置",command=lambda:cancel(),width=5,font=("微软雅黑",12))
btncancel.place(x=200,y=260)
btn=tkinter.Button(root,text="确定",command=lambda:run(),width=5,font=("微软雅黑",12))
btn.place(x=280,y=260)
lb2=tkinter.Label(root,text='',font=("宋体",12))
lb2.place(x=50,y=200)
ls=['足球','篮球','游泳','田径']
listbox=tkinter.Listbox(root)
listbox.place(x=160,y=35)
for item in ls:
listbox.insert("end",item)
var=[]
#*******************GUI*******************#
def all():
for i in range(1, 5):
eval('t'*i).set(1)
def invert():
for i in range(1, 5):
s = eval('t'*i)
if s.get() == 0:
s.set(1)
else:
s.set(0)
def cancel():
for i in range(1, 5):
s = eval('t'*i)
s.set(0)
lb2.config(text='')
def run():
count = ''
for i in range(1, 5):
s = eval('t'*i)
if s.get() == 1:
count += ls[i-1]
lb2.config(text=f'您选择了{count}', fg='red')
t = tkinter.IntVar()
tt = tkinter.IntVar()
ttt = tkinter.IntVar()
tttt = tkinter.IntVar()
f = 1
for i in ls:
s = tkinter.Checkbutton(listbox, text=i,variable=eval('t'*f), onvalue=1, offvalue=0)
f += 1
s.pack()
#*******************End*******************#
root.mainloop()