1 # tkinter的Checkbutton实现开关操作 2 3 import tkinter as tk 4 5 root = tk.Tk() 6 root.title(‘开关‘) 7 root.geometry(‘170x60‘) # 设置窗口大小 8 v = tk.StringVar() 9 v.set(‘1‘) 10 11 12 def click_event(): 13 print(v.get()) 14 ck[‘text‘] = v.get() 15 16 17 ‘‘‘窗体控件‘‘‘ 18 # 标题显示 19 lab = tk.Label(root, text=‘开关状态:‘, font=‘宋体 -16‘) 20 lab.grid(row=0, sticky=tk.W) 21 22 # 多选框 23 ck = tk.Checkbutton(root, variable=v, text=‘OFF‘, font=‘宋体 -18‘, 24 onvalue=‘ON‘, offvalue=‘OFF‘, 25 command=click_event) 26 ck.grid(row=1, padx=50, sticky=tk.W) 27 28 root.mainloop()
截图:
时间: 2024-10-29 03:09:58