次のようにあなたは、あなたのプログラムを設計することによって、あなたの問題を解決することができます
最後の点は、この方法(more info)に行われます。ここで
コード
は、このアプローチの実現可能性を実証するシンプルMCVEである:ここでは
'''
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()
デモ
は、最初のチェックボタンをチェックした後、プログラム上で実行中のスクリーンショットですグループ:
それぞれを個別に設定する必要があります。あなたがしなければならないのは、すべてのウィジェットのリストを保持し、リストを反復するだけです。 2行のコードが必要です。 –