多个按钮可更改多个标签的颜色TKINTER、PYTHON?
因此我有多个按钮 我需要每个名字的按钮: i、 e.紫色按钮,将其上方的标签变为紫色, 和紫色按钮可将上面的标签变成紫色 重置按钮会将它们全部重置为灰色 如果有人能修改我的代码,使“重置”按钮的间距在紫色和蓝色之间(但仍向下一行),那将非常感谢 我的代码现在的作用是: 它把所有的盒子都变成各种颜色。 我如何做到这一点当我按下一个按钮时,一个标签会改变颜色,如果可能的话,我希望在一个功能中做到这一点(我已经考虑过制作多个功能,并且认为这样会更好) 导入Tkinter函数多个按钮可更改多个标签的颜色TKINTER、PYTHON?,python,python-3.x,button,tkinter,label,Python,Python 3.x,Button,Tkinter,Label,因此我有多个按钮 我需要每个名字的按钮: i、 e.紫色按钮,将其上方的标签变为紫色, 和紫色按钮可将上面的标签变成紫色 重置按钮会将它们全部重置为灰色 如果有人能修改我的代码,使“重置”按钮的间距在紫色和蓝色之间(但仍向下一行),那将非常感谢 我的代码现在的作用是: 它把所有的盒子都变成各种颜色。 我如何做到这一点当我按下一个按钮时,一个标签会改变颜色,如果可能的话,我希望在一个功能中做到这一点(我已经考虑过制作多个功能,并且认为这样会更好) 导入Tkinter函数 这应该满足您的要求: d
这应该满足您的要求:
def get_function(cmd):
def change_colour():
if 'violet' == cmd:
label_violet['bg'] = 'violet'
if 'purple' == cmd:
label_purple['bg'] = 'purple'
if 'blue' == cmd:
label_blue['bg'] = 'blue'
if 'green' == cmd:
label_green['bg'] = 'green'
if 'reset' == cmd:
label_violet['bg'] = 'grey'
label_purple['bg'] = 'grey'
label_blue['bg'] = 'grey'
label_green['bg'] = 'grey'
return change_colour
violet_button = Button(the_window, text = 'Violet', width = button_size,
font = window_font, command = get_function('violet'))
purple_button = Button(the_window, text = 'Purple', width = button_size,
font = window_font, command = get_function('purple'))
blue_button = Button(the_window, text = 'Blue', width = button_size,
font = window_font, command = get_function('blue'))
green_button = Button(the_window, text = 'Green', width = button_size,
font = window_font, command = get_function('green'))
reset_button = Button(the_window, text = 'RESET', width = button_size,
font = window_font, command = get_function('reset'))
对于重置按钮的问题,只需指定网格函数的columnspan
参数:
reset_button.grid(row = 2, column = 1, pady = margin_size_height, columnspan = 2)
这里有很多代码可以复制/粘贴。计算机真的很擅长在改变一些变量的情况下重复事情。你可以说这就是计算机所擅长的。因此,你自己这样做,就是在做计算机的工作。此外,如果你想以后改变一些事情,你正在为你未来的自己做更多的工作。最好是把东西放在一个位置,这样以后就可以进行一次更改,而不是对每个标签都进行更改。我可以看出你已经考虑过这个了,因为你有这么多常数。更进一步,就是制作一个“常量”小部件,所有实例都从中复制。这有点高级,但下面是您将如何做到这一点:
import tkinter as tk
# Constants (aka variables that don't change during the program run)
BUTTON_SIZE = 10
FONT=('Arial', 8)
class Corey(tk.Frame):
'''a new widget that combines a Label and a Button'''
instances = []
def __init__(self, master=None, color='grey', **kwargs):
tk.Frame.__init__(self, master, **kwargs)
self.color = color
self.lbl = tk.Label(self, bg=color) # set initial color
self.lbl.grid(sticky='nsew') # sticky makes the label as big as possible
btn = tk.Button(self, text=color.title(), width=BUTTON_SIZE, command=self.colorize, font=FONT)
btn.grid()
self.instances.append(self)
def colorize(self):
self.lbl.config(bg=self.color)
@classmethod
def reset(cls):
for widget in cls.instances:
widget.lbl.config(bg='grey')
# Create a window
the_window = tk.Tk()
# Give the window a title
the_window.title('MULTI Button Colour')
# make some Corey widgets
colors = 'violet', 'purple', 'blue', 'green'
for col, color in enumerate(colors):
widget = Corey(the_window, color)
widget.grid(row=0, column=col, padx=10, pady=2)
reset_button = tk.Button(the_window, text = 'RESET', width=BUTTON_SIZE, command=Corey.reset, font=FONT)
reset_button.grid(row=1, column=0, columnspan=len(colors), pady=4)
the_window.mainloop()
最直接的优点是,为了从我的版本中添加或减少颜色,您只需更改一行。此外,如果您想更改外观,例如添加浮雕或将按钮更改为复选框,您只需更改一行即可。像这样的子类是GUI设计的重要组成部分,我建议您尽快开始 啊,我的天哪,你是上帝。对我的重置按钮问题也有帮助吗?快速提问为什么必须将其放在另一个函数中?@CoreyRobinson这只是一种方法。您必须这样做,因为除了使用
lambda
或functools.partial
之外,不可能获得内部函数的参数,这两种方法看起来都比这更难看。@MegaIng此结构称为a。@MegaIng和我不同意;我认为自己做一个闭包比使用functools.partial
做一个闭包更难看。
import tkinter as tk
# Constants (aka variables that don't change during the program run)
BUTTON_SIZE = 10
FONT=('Arial', 8)
class Corey(tk.Frame):
'''a new widget that combines a Label and a Button'''
instances = []
def __init__(self, master=None, color='grey', **kwargs):
tk.Frame.__init__(self, master, **kwargs)
self.color = color
self.lbl = tk.Label(self, bg=color) # set initial color
self.lbl.grid(sticky='nsew') # sticky makes the label as big as possible
btn = tk.Button(self, text=color.title(), width=BUTTON_SIZE, command=self.colorize, font=FONT)
btn.grid()
self.instances.append(self)
def colorize(self):
self.lbl.config(bg=self.color)
@classmethod
def reset(cls):
for widget in cls.instances:
widget.lbl.config(bg='grey')
# Create a window
the_window = tk.Tk()
# Give the window a title
the_window.title('MULTI Button Colour')
# make some Corey widgets
colors = 'violet', 'purple', 'blue', 'green'
for col, color in enumerate(colors):
widget = Corey(the_window, color)
widget.grid(row=0, column=col, padx=10, pady=2)
reset_button = tk.Button(the_window, text = 'RESET', width=BUTTON_SIZE, command=Corey.reset, font=FONT)
reset_button.grid(row=1, column=0, columnspan=len(colors), pady=4)
the_window.mainloop()