2014-12-08 127 views
0

我正在嘗試創建一個小桌面應用程序。在主窗口我有四個複選框,每個複選框具有值變量(1 0爲關,對):禁用Tkinter中其他複選框的複選框

random_cards = IntVar() 
random_stacks = IntVar() 
flip_cards = IntVar() 
wildcard = IntVar() 

randomize_cards_checkbutton = Checkbutton(text="Randomize cards", variable=random_cards).grid(row=0, column=0, in_=options, sticky=W) 
randomize_stacks_checkbutton = Checkbutton(text="Randomize stacks", variable=random_stacks).grid(row=1, column=0,in_=options, sticky=W) 
wildcard_checkbutton = Checkbutton(text="Wildcard", variable=wildcard).grid(row=2, column=0, in_=options, sticky=W) 
flip_cards_checkbutton = Checkbutton(text="Flip cards", variable=flip_cards).grid(row=3, column=0, in_=options, sticky=W) 

我想要的行爲是,如果wildcard_checkbutton是上,兩個複選框randomize_cards_checkbuttonrandomize_stacks_checkbutton是禁用(灰色),反之亦然。現在我不知道如何使這個功能運行「所有時間」

def check_checkbuttons(random_cards, random_stacks, wildcard): 
    if wildcard == 1: 
     randomize_cards_checkbutton.configure(state=DISABLED) 
     randomize_stacks_checkbutton.configure(state=DISABLED) 
    elif random_cards == 1 or random_stacks == 1: 
     wildcard_checkbutton.configure(state=DISABLED) 

:我寫的小功能這一點。我如何實現它,所以這個功能一直在被檢查?

回答

0

首先,randomize_cards_checkbutton和所有其他checkbutton變量等於None,因爲這是grid()返回的值。對於checkbutton,當狀態改變時使用「command =」來調用函數。請注意,您必須獲取()Tkinter變量才能將其變成Python變量。任何兩個按鈕都可用於此測試/示例,但每個checkbutton在最終程序中都會有一個「command =」回調,它將禁用/啓用您希望的其他任何checkbutton。至少,使用一些打印語句來幫助您調試。 print語句會告訴你什麼是None,通配符是PY_VAR,而不是整數等。

def cb_check(): 
    if random_cards.get(): 
     randomize_stacks_checkbutton.config(state=DISABLED) 
    else: 
     randomize_stacks_checkbutton.config(state=NORMAL) 

top=Tk() 
random_cards = IntVar() 
random_stacks = IntVar() 
flip_cards = IntVar() 
wildcard = IntVar() 

randomize_cards_checkbutton = Checkbutton(top, text="Randomize cards", 
           variable=random_cards, command=cb_check) 
randomize_cards_checkbutton.grid(row=0, column=0, sticky=W) 

randomize_stacks_checkbutton = Checkbutton(top, text="Randomize stacks", 
           variable=random_stacks, bg="lightblue", 
           disabledforeground="gray") 
randomize_stacks_checkbutton.grid(row=1, column=0, sticky=W) 

top.mainloop() 
相關問題