我有一个与一组其他小部件相关联的检查按钮。我想要做的是检查检查按钮时我想将所有关联/子窗口小部件状态从禁用更改为活动状态。
我正在考虑从回调函数更改关联窗口小部件的状态,但我不想单独配置每个窗口小部件。有没有办法将所有子窗口小部件组合在一起,这样我就可以一次更改所有子窗口小部件状态而不是单独配置?
checkbutton 1 (unchecked)
entry1 (disable)
entry2 (disable)
...
entry20 (disable)
checkbutton is checked
checkbutton 1 (checked)
entry1 (active)
entry2 (active)
...
entry20 (active
我确信必须有一种方法对它们进行分组,因此我不必单独配置每个20个小部件属性。这将是有用的,所以我可以创建我的设置的字典,然后我可以通过更改我的字典更改大量的小部件。
答案 0 :(得分:2)
您可以按照以下方式设计程序来解决问题:
mapper.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES);
与其关联的其他小部件组保持在一个小部件中(Checkbutton
或Frame
更合适)Label
,您必须创建一个Tkinter变量。要检查按钮状态,请查询变量(source)。Checkbutton
相关联的小部件并更改其状态。最后一点是这样完成的(more info):
Checkbutton
这种方法可以为您节省很多麻烦。
这是一个简单的MCVE,证明了这种方法的可行性:
for associated_widget in frame.winfo_children():
associated_widget.configure(state='disabled')
以下是检查第一组的'''
Created on Jun 19, 2016
@author: Billal Begueradj
'''
import Tkinter as Tk
class Begueradj(Tk.Frame):
'''
Control the state of multiple widgets associated to a checkbutton
'''
def __init__(self, parent):
'''
Inititialize the GUI with a button and a Canvas objects
'''
Tk.Frame.__init__(self, parent)
self.parent=parent
self.initialize_user_interface()
def initialize_user_interface(self):
"""
Draw the GUI
"""
self.parent.title("Billal BEGUERADJ")
self.parent.grid_rowconfigure(0,weight=1)
self.parent.grid_columnconfigure(0,weight=1)
self.parent.config(background="lavender")
# Draw a frame
self.frame = Tk.Frame(self.parent, bg='yellow')
self.frame.pack(side='left')
self.var = Tk.IntVar()
# Draw a checkbutton on the frame
self.checkbutton = Tk.Checkbutton(self.frame, text="Group 1", variable=self.var, command=self.callback1)
self.checkbutton.grid(row=0, column=0)
# Draw 5 buttons on the frame
for i in range(5):
self.button = Tk.Button(self.frame, text ='Button '+str(i))
self.button.grid(row=i+1, column=0)
# Draw a Label
self.label = Tk.Label(self.parent, bg='blue')
self.label.pack(side='right')
# Draw a checkbutton on the label
self.v = Tk.IntVar()
self.checkbuton = Tk.Checkbutton(self.label, text="Group 2", variable=self.v, command=self.callback2)
self.checkbuton.grid(row=0, column=0)
# Draw 5 buttons on the label
for i in range(5):
self.button = Tk.Button(self.label, text ='Button '+str(i))
self.button.grid(row=i+1, column=0)
print self.checkbutton
# Callback for checkbutton
def callback1(self):
if self.var.get() == 1:
for w in self.frame.winfo_children():
w.configure(state='disabled')
self.checkbutton.configure(state='normal')
# Callback for checkbuton
def callback2(self):
if self.v.get() == 1:
for w in self.label.winfo_children():
w.configure(state='disabled')
self.checkbuton.configure(state='normal')
# Main method
def main():
root=Tk.Tk()
d=Begueradj(root)
root.mainloop()
if __name__ == '__main__':
main()
后运行上述程序的屏幕截图: