2014-12-08 5 views
0

작은 데스크탑 응용 프로그램을 만들려고합니다. 기본 창에서 내가 네 체크 박스를 가지고, 각각의 체크 박스 (온 오프 0, 1) 값을 가진 변수가 있습니다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 및 기타 모든 체크 버튼 변수는 grid()가 반환하는 것이므로 None입니다. 체크 버튼의 경우 상태가 변경 될 때 "command ="를 사용하여 함수를 호출하십시오. 파이썬 변수로 바꾸려면 Tkinter 변수를 가져와야합니다(). 이 테스트/예제에는 두 개의 버튼이 모두 작동하지만 각 체크 버튼에는 원하는 다른 체크 버튼을 비활성화하거나 활성화하는 "프로그램 ="콜백이 최종 프로그램에 있습니다. 최소한 디버깅을 돕기 위해 몇 가지 print 서술문을 사용하십시오. 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() 
관련 문제