我的代码有效,但想要一个更清洁的。我有 24 个 ON 和 OFF 按钮。单击 ON 按钮时,它将被禁用并变为绿色,而 OFF 按钮将变为灰色并显示“!禁用”。有没有办法做到这一点,而无需大量的 ON 和 OFF 方法?目前我有 48 种方法、样式等,总计 800 行代码。我知道必须有更好的方法...
这是一个例子:
from tkinter import *
from tkinter import ttk
class APP:
def __init__(self, master):
master.title('APP')
master.resizable(False, False)
self.style = ttk.Style()
self.frame_header = ttk.Frame(master)
self.frame_header.pack()
self.frame_stations = ttk.Frame(master)
self.frame_stations.pack()
# default style and text of button
self.style.configure('n1_on.TButton', foreground='#d2d2d2',font=('Arial', 15,'bold'))
self.style.configure('n1_off.TButton', foreground='#d2d2d2', font=('Arial',15,'bold'))
self.style.configure('n2_on.TButton', foreground='#d2d2d2',font=('Arial', 15,'bold'))
self.style.configure('n2_off.TButton', foreground='#d2d2d2', font=('Arial', 15,'bold'))
# changes state (disabled) and font color when clocked
self.style.map('n1_on.TButton', foreground=[('disabled','green')])
self.style.map('n1_off.TButton', foreground=[('disabled','red')])
self.style.map('n2_off.TButton', foreground=[('disabled','red')])
self.style.map('n2_on.TButton', foreground=[('disabled','green')])
ttk.Label(self.frame_stations, text="BUTTONS").grid(row=0, column=1, columnspan=2, padx=5)
#buttons created adn location
self.n1_on = ttk.Button(self.frame_stations, text='ON', style='n1_on.TButton', command=self.n1_on_state,)
self.n1_on.grid(row=1, column=1)
self.n2_on = ttk.Button(self.frame_stations, text='ON', style='n2_on.TButton', command=self.n2_on_state)
self.n2_on.grid(row=2, column=1)
self.n1_off = ttk.Button(self.frame_stations, text='OFF', style= 'n1_off.TButton', command=self.n1_off_state)
self.n1_off.grid(row=1, column=2)
self.n2_off= ttk.Button(self.frame_stations, text='OFF', style='n2_off.TButton', command=self.n2_off_state)
self.n2_off.grid(row=2, column=2)
def n1_on_state(self):
print('n1 turned on')
self.n1_on.state(['disabled'])
self.n1_off.state(['!disabled'])
def n2_on_state(self):
print('n2 turned on')
self.n2_on.state(['disabled'])
self.n2_off.state(['!disabled'])
def n1_off_state(self):
print('n1_turned off')
self.n1_on.state(['!disabled'])
self.n1_off.state(['disabled'])
def n2_off_state(self):
print('n2 turned off')
self.n2_on.state(['!disabled'])
self.n2_off.state(['disabled'])