1 # tkinter复选框操作 2 3 import tkinter as tk 4 5 root = tk.Tk() 6 root.title(‘问卷调查‘) 7 root.geometry(‘220x80‘) # 设置窗口大小 8 9 flag_1 = False 10 flag_2 = False 11 flag_3 = False 12 list_content = [‘你的爱好是:‘] 13 hobby_list = [‘游泳‘, ‘唱歌‘, ‘旅游‘] 14 15 16 def click_1(): 17 global flag_1 18 flag_1 = not flag_1 19 if flag_1: 20 list_content.append(hobby_list[0]) 21 else: 22 list_content.remove(hobby_list[0]) 23 # print(‘你的爱好是:‘, list_content) 24 lab_msg[‘text‘] = list_content 25 26 27 def click_2(): 28 global flag_2 29 flag_2 = not flag_2 30 if flag_2: 31 list_content.append(hobby_list[1]) 32 else: 33 list_content.remove(hobby_list[1]) 34 # print(‘你的爱好是:‘, list_content) 35 lab_msg[‘text‘] = list_content 36 37 38 def click_3(): 39 global flag_3 40 flag_3 = not flag_3 41 if flag_3: 42 list_content.append(hobby_list[2]) 43 else: 44 list_content.remove(hobby_list[2]) 45 # print(‘你的爱好是:‘, list_content) 46 lab_msg[‘text‘] = list_content 47 48 49 ‘‘‘窗体控件‘‘‘ 50 # 标题显示 51 lab = tk.Label(root, text=‘请选择你的爱好:‘) 52 lab.grid(row=0, columnspan=3, sticky=tk.W) 53 54 # 多选框 55 frm = tk.Frame(root) 56 ck1 = tk.Checkbutton(frm, text=‘游泳‘, command=click_1) 57 ck2 = tk.Checkbutton(frm, text=‘唱歌‘, command=click_2) 58 ck3 = tk.Checkbutton(frm, text=‘旅游‘, command=click_3) 59 ck1.grid(row=0) 60 ck2.grid(row=0, column=1) 61 ck3.grid(row=0, column=2) 62 frm.grid(row=1) 63 64 lab_msg = tk.Label(root, text=‘‘) 65 lab_msg.grid(row=2, columnspan=3, sticky=tk.W) 66 67 root.mainloop()
截图:
时间: 2024-10-10 05:54:12