2016-06-19 8 views
2

他のウィジェットのグループに関連付けられたチェックボタンがあります。私がしたいのは、チェックボタンがチェックされているときです。関連付けられている/子ウィジェットの状態をすべて無効に変更したいのです。状態属性をTkinterのウィジェットのグループに変更します

関連するウィジェットの状態をコールバック関数から変更することを考えていましたが、個々のウィジェットを個別に設定する必要はありません。すべての子ウィジェットをまとめてグループ化する方法はありますか?個別に設定するのではなく、すべての子ウィジェットの状態を一度に変更することはできますか?

checkbutton 1 (unchecked) 
    entry1 (disable) 
    entry2 (disable) 
    ... 
    entry20 (disable) 

checkbutton is checked 

checkbutton 1 (checked) 
    entry1 (active) 
    entry2 (active) 
    ... 
    entry20 (active 

20個のウィジェットのプロパティを個別に設定する必要はないので、グループ化する方法が必要であると確信しています。これは私の設定の辞書を作成することができますし、私は辞書を変更することによって大量のウィジェットを変更することができますので参考になります。

+0

それぞれを個別に設定する必要があります。あなたがしなければならないのは、すべてのウィジェットのリストを保持し、リストを反復するだけです。 2行のコードが必要です。 –

答えて

1

次のようにあなたは、あなたのプログラムを設計することによって、あなたの問題を解決することができます

  • を使用するには1つのウィジェット内の他のウィジェットのそれに関連するグループ(フレームまたはラベルがより適切であろう)
  • whith各チェックボタンを押したままCheckbuttonを使用するには、Tkinter変数を作成する必要があります。ボタンの状態を調べるには、変数(source)を照会します。
  • 上記の変数が変更されている場合は、問題のチェックボタンに関連付けられたウィジェットをループし、その状態を変更します。このアプローチは、頭痛のあなたを大幅に節約します

    for associated_widget in frame.winfo_children(): 
        associated_widget.configure(state='disabled') 
    

最後の点は、この方法(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() 

デモ

は、最初のチェックボタンをチェックした後、プログラム上で実行中のスクリーンショットですグループ:

enter image description here

+1

それは働いた、ありがとう – user2067030

+1

これは非常に有用な答えです。より多くのアップホーセットが必要です! – bitsmack

関連する問題