问题描述
我的代码有效,但想要一个清洁器。我有 24 个开和关按钮。单击 ON 按钮时,它将被禁用并显示为绿色,而 OFF 按钮则变灰并显示为“!disabled”。有没有办法在不制作大量 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','bold'))
self.style.configure('n2_on.TButton','bold'))
self.style.configure('n2_off.TButton','bold'))
# changes state (disabled) and font color when clocked
self.style.map('n1_on.TButton',foreground=[('disabled','green')])
self.style.map('n1_off.TButton','red')])
self.style.map('n2_off.TButton','red')])
self.style.map('n2_on.TButton','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,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,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'])
解决方法
你就不能这样定义吗?为什么每次都要为每个按钮定义函数?
# Define this above your code and pass it the buttons you want to turn on or off
def turn_off_buttons(turn_off,turn_on):
# Also when we want to inform the user we usually put the 'print' statement after the work is finished
# print(f'{turn_off} turned off') (Not here)
# turn_on.state(['!disabled'])
# turn_off.state(['disabled'])
# I would suggest you to use 'config' instead of state since it is more applicable
turn_off.config(state=DISABLED)
turn_on.config(state=NORMAL)
print(f'{turn_off} turned off')
示例:
from tkinter import *
from tkinter import ttk
# Define this above your code and pass it the buttons you want to turn on or off
def turn_off_buttons(turn_off,turn_on):
# Also when we want to inform the user we usually put the 'print' statement after the work is finished
# print(f'{turn_off} turned off') (Not here)
# turn_on.state(['!disabled'])
# turn_off.state(['disabled'])
# I would suggest you to use 'config' instead of state since it is more applicable
turn_off.config(state=DISABLED)
turn_on.config(state=NORMAL)
print(f'{turn_off} turned off')
class TurnOnOff:
def __init__(self):
self.root = Tk()
self.root.geometry("550x280")
self.button1 = ttk.Button(self.root,text="Turn On",command=lambda: turn_off_buttons(self.button1,self.button2))
self.button2 = ttk.Button(self.root,text="Turn Off",command=lambda: turn_off_buttons(self.button2,self.button1))
self.button1.pack(pady=5)
self.button2.pack(pady=5)
self.root.mainloop()
if __name__ == "__main__":
TurnOnOff()