思路1
import tkinter as tk
import random
def generate_checkbuttons():
num_checkbuttons = random.randint(3, 5)
for cb in checkbuttons:
cb.destroy()
checkbuttons.clear()
for i in range(num_checkbuttons):
var = tk.IntVar()
cb = tk.Checkbutton(root, text=f"Checkbutton {i + 1}", variable=var)
cb.pack()
checkbuttons.append(cb)
def select_all():
for cb in checkbuttons:
cb.select()
def deselect_all():
for cb in checkbuttons:
cb.deselect()
root = tk.Tk()
root.title("Checkbutton Demo")
checkbuttons = []
refresh_btn = tk.Button(root, text="Refresh", command=generate_checkbuttons)
refresh_btn.pack()
all_btn = tk.Button(root, text="All", command=select_all)
all_btn.pack()
no_btn = tk.Button(root, text="No", command=deselect_all)
no_btn.pack()
generate_checkbuttons()
root.mainloop()
优化:
将生成和管理复选按钮的部分封装成一个类,以提高代码的可读性和可维护性。
import tkinter as tk
import random
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("Checkbutton Demo")
self.checkbuttons = []
self.generate_buttons()
self.generate_checkbuttons()
def generate_buttons(self):
refresh_btn = tk.Button(self, text="Refresh", command=self.generate_checkbuttons)
refresh_btn.pack()
all_btn = tk.Button(self, text="All", command=self.select_all)
all_btn.pack()
no_btn = tk.Button(self, text="None", command=self.deselect_all)
no_btn.pack()
def generate_checkbuttons(self):
num_checkbuttons = random.randint(3, 5)
self.clear_checkbuttons()
for i in range(num_checkbuttons):
var = tk.IntVar()
cb = tk.Checkbutton(self, text=f"Checkbutton {i + 1}", variable=var)
cb.pack()
self.checkbuttons.append(cb)
def clear_checkbuttons(self):
for cb in self.checkbuttons:
cb.destroy()
self.checkbuttons.clear()
def select_all(self):
for cb in self.checkbuttons:
cb.select()
def deselect_all(self):
for cb in self.checkbuttons:
cb.deselect()
if __name__ == '__main__':
app = App()
app.mainloop()
再优化:
将generate_buttons()
方法中的按钮创建与App
类的初始化方法中的其他设置分开,这样可以更清晰地组织代码。另外,可以考虑将生成和管理复选按钮的功能封装成一个单独的类,以提高代码的模块化和可读性。
import tkinter as tk
import random
class CheckbuttonManager:
def __init__(self, root):
self.root = root
self.checkbuttons = []
def generate_checkbuttons(self):
num_checkbuttons = random.randint(3, 5)
self.clear_checkbuttons()
for i in range(num_checkbuttons):
var = tk.IntVar()
cb = tk.Checkbutton(self.root, text=f"Checkbutton {i + 1}", variable=var)
cb.pack()
self.checkbuttons.append(cb)
def clear_checkbuttons(self):
for cb in self.checkbuttons:
cb.destroy()
self.checkbuttons.clear()
def select_all(self):
for cb in self.checkbuttons:
cb.select()
def deselect_all(self):
for cb in self.checkbuttons:
cb.deselect()
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("Checkbutton Demo")
self.checkbutton_manager = CheckbuttonManager(self)
self.generate_buttons()
self.checkbutton_manager.generate_checkbuttons()
def generate_buttons(self):
refresh_btn = tk.Button(self, text="Refresh", command=self.checkbutton_manager.generate_checkbuttons)
refresh_btn.pack()
all_btn = tk.Button(self, text="All", command=self.checkbutton_manager.select_all)
all_btn.pack()
no_btn = tk.Button(self, text="None", command=self.checkbutton_manager.deselect_all)
no_btn.pack()
if __name__ == '__main__':
app = App()
app.mainloop()
思路2
import os
import tkinter
import time
class Decorator(object):
@staticmethod
def log(func):
def wrapper(*args, **kwargs):
func(*args, **kwargs)
try:
user_name = os.getlogin() if os.name == 'nt' else os.getenv('USER')
current_time = time.strftime("%H:%M:%S")
info = f"[{user_name}] [{current_time}] {func.__name__}"
with open("d:\\log.log", "a") as log_file:
log_file.write(info + "\n")
except Exception as e:
print(f"Error occurred: {e}")
return wrapper
class App(tkinter.Tk):
def __init__(self):
super().__init__()
self.set_window()
self.set_frame_left()
self.set_frame_right()
def set_window(self):
self.title("Automation kit")
self.rowconfigure(index=0, weight=1)
self.columnconfigure(index=0, weight=1)
self.columnconfigure(index=1, weight=2)
def set_frame_left(self):
# frame_left
self.frame_left = tkinter.LabelFrame(master=self, text="Device Info")
self.frame_left.grid(row=0, column=0, sticky=tkinter.NSEW)
tkinter.Button(master=self.frame_left,
text="refresh",
command=self.refresh).grid(row=0, column=0,
sticky=tkinter.NSEW)
tkinter.Button(master=self.frame_left,
text=time.strftime("%H:%M:%S")).grid(row=1, column=0)
def set_frame_right(self):
# frame_right
self.frame_right = tkinter.LabelFrame(master=self, text="Commands")
self.frame_right.grid(row=0, column=1, sticky=tkinter.NSEW)
tkinter.Button(master=self.frame_right, text="cmd").grid(row=0, column=0, sticky=tkinter.NSEW)
@Decorator.log
def refresh(self):
self.frame_left.destroy()
self.set_frame_left()
if __name__ == "__main__":
app = App()
app.mainloop()