2017-03-03 5 views
1

tkinterに1つのチェックボタンを付けることはできますか?チェックされていれば、他のチェックボタンもチェックしますか?tkinterで1つのチェックボタンを作って他のすべてのチェックボタンをチェックすることはできますか?

例:

Checkbutton(root, text="A").grid(row=0, column=0, sticky=W) 
Checkbutton(root, text="B").grid(row=1, column=0, sticky=W) 
Checkbutton(root, text="C").grid(row=2, column=0, sticky=W) 
Checkbutton(root, text="ABC").grid(row=3, column=0, sticky=W) 

ですから、ABCボタンをチェックするならば、他のすべてのボタンがaswell確認になるだろう。私が欲しいものを達成するための方法はありますか?

のPython:3.4

OS:Windowsの

答えて

2

A、B、およびCを作成するときにheckboxes、それらをリストに保存する; ABCがクリックされたときに、あなたは、リストを反復し、それらのすべてを確認することができます。より完全な例

A = Checkbutton(root, text="A") 
B = Checkbutton(root, text="B") 
C = Checkbutton(root, text="C") 
cbs = [A, B, C] 

A.grid(row=0, column=0, sticky=W) 
B.grid(row=1, column=0, sticky=W) 
C.grid(row=2, column=0, sticky=W) 

def checkall(): 
    for cb in cbs: 
     cb.select() 

Checkbutton(root, text="ABC", command=checkall).grid(row=3, column=0, sticky=W) 
0

考えるCheckbutton

check = Checkbutton(root, text="Checkbutton",...) 
check.grid(row=0, column=0, ...) 

のチェックを外し、それを使用して:check.deselect()

または(再)でそれを確認してください。check.select()

+0

私はこの取得:はAttributeError:「をNoneType」オブジェクトを使用していない、あなたはコードを置く方法 – Lojas

+0

「を選択解除」は属性を持っていません。グリッドを同じ行に配置します。 Haldean Brownと私のようにします:foo = Checkbutton(...)、そして 'foo.grid()'、 'foo.unselect()' –

0

は、次のコードを参照することもできます。これは、チェックボックスを使用して別の値を部分的に決定する方法を示しています。ロジックは単一チェックボックスとグループチェックボックスでわずかに異なるため、2つの異なるイベントハンドラが使用されます。

#! /usr/bin/env python3 
from tkinter import * 
from tkinter.ttk import * 


class Application(Frame): 

    @classmethod 
    def main(cls): 
     # Create a test environment to show how the class works. 
     NoDefaultRoot() 
     root = Tk() 
     root.title('Demonstration') 
     root.resizable(False, False) 
     root.minsize(250, 100) 
     frame = cls(root) 
     frame.grid() 
     root.mainloop() 

    def __init__(self, master=None, **kw): 
     super().__init__(master, **kw) 
     # Create variables for the checkboxes. 
     self.bv_a = BooleanVar(self) 
     self.bv_b = BooleanVar(self) 
     self.bv_c = BooleanVar(self) 
     self.bv_abc = BooleanVar(self) 
     self.cb_variables = self.bv_a, self.bv_b, self.bv_c 
     # Create each of the desired checkboxes. 
     options = dict(
      master=self, command=self.update_any, onvalue=True, offvalue=False 
     ) 
     self.cb_a = Checkbutton(text='A', variable=self.bv_a, **options) 
     self.cb_b = Checkbutton(text='B', variable=self.bv_b, **options) 
     self.cb_c = Checkbutton(text='C', variable=self.bv_c, **options) 
     options.update(command=self.update_all) 
     self.cb_abc = Checkbutton(text='ABC', variable=self.bv_abc, **options) 
     # Make sure the checkboxes are displayed. 
     self.cb_a.grid() 
     self.cb_b.grid() 
     self.cb_c.grid() 
     self.cb_abc.grid() 

    def update_any(self): 
     # Only check "ABC" if all the other boxes are checked. 
     self.bv_abc.set(all(variable.get() for variable in self.cb_variables)) 

    def update_all(self): 
     # Copy the status of "ABC" to all of the other boxes. 
     for variable in self.cb_variables: 
      variable.set(self.bv_abc.get()) 


if __name__ == '__main__': 
    Application.main() 
0

ただ、同じ変数にそれらのすべてを結ぶ:

var = IntVar() 

Checkbutton(root, text="A", variable=var).grid(row=0, column=0, sticky=W) 
Checkbutton(root, text="B", variable=var).grid(row=1, column=0, sticky=W) 
Checkbutton(root, text="C", variable=var).grid(row=2, column=0, sticky=W) 
Checkbutton(root, text="ABC", variable=var).grid(row=3, column=0, sticky=W) 
関連する問題