2017-12-15 5 views
0

내 의도는 사용자가 버튼 중 하나를 클릭 할 때 나머지 버튼은 클릭해도 아무 것도 수행하지 않으므로 기본적으로 사용자가 클릭했을 때 버튼을 클릭 할 때 버튼이 명령을 수행하지 못하도록하는 것입니다. 이전에 다른 버튼에, 나는 내가 dind't 경우 미안 해요 잘 수 있도록 자신을 표현 알고 있지만 여기 내 코드입니다하지 않습니다 당신은 그들을 회색 DISABLED에 다른 버튼의 상태를 설정할 수 있습니다파이썬에서 다른 버튼을 클릭하면 어떻게 버튼 명령을 중지 할 수 있습니까?

from tkinter import * 
import random  

screen = Tk()  

ticket = random.randint(1,3)  

def test(): 
    def test1(): 
     if ticket == button1: 
      button_1 = Button(screen, text="RIP", fg="white", bg="red", width=15, height=2) 
      button_1.grid(row=0, column=0, sticky="w") 
     else: 
      button_2 = Button(screen, text="+1", fg="white", bg="green", width=15, height=2) 
      button_2.grid(row=0, column=0, sticky="w") 
    def test2(): 
     if ticket == button2: 
      button_3 = Button(screen, text="RIP", fg="white", bg="red", width=15, height=2) 
      button_3.grid(row=0, column=1, sticky="w") 
     else: 
      button_4 = Button(screen, text="+1", fg="white", bg="green", width=15, height=2) 
      button_4.grid(row=0, column=1, sticky="w") 
    def test3(): 
     if ticket == button3: 
      button_5 = Button(screen, text="RIP", fg="white", bg="red", width=15, height=2) 
      button_5.grid(row=0, column=2, sticky="w") 
     else: 
      button_6 = Button(screen, text="+1", fg="white", bg="green", width=15, height=2) 
      button_6.grid(row=0, column=2, sticky="w")  

    ticket = random.randint(1,3)  

    button1 = Button(screen, text="1", fg="white", bg="blue", width=15, height=2, command=test1) 
    button1.grid(row=0, column=0, sticky="w") 
    button1 = 1  

    button2 = Button(screen, text="2", fg="white", bg="blue", width=15, height=2, command=test2) 
    button2.grid(row=0, column=1, sticky="w"+"e"+"n"+"s") 
    button2 = 2  

    button3 = Button(screen, text="3", fg="white", bg="blue", width=15, height=2, command=test3) 
    button3.grid(row=0, column=2, sticky="e") 
    button3 = 3  

button1 = Button(screen, text="START", fg="black", bg="orange", width=25, height=2, command=test) 
button1.grid(row=1, columnspan=3, sticky="w"+"e"+"n"+"s")  

screen.mainloop() 

답변

4

클릭 수를 방지합니다. 이것은 인스턴스를 추적하는 서브 클래스를 사용하기에 이상적인 장소입니다.

from tkinter import * 
import random 

screen = Tk() 

class MykoButton(Button): 
    instances = [] 

    def __init__(self, master=None, **kwargs): 
     super().__init__(master, command=self.run, **kwargs) 
     self.instances.append(self) 

    def run(self): 
     for button in self.instances: 
      if button is not self: 
       button.config(state=DISABLED) 
     if random.randint(1,3) == 1: 
      self.config(text="RIP", fg="white", bg="red") # update the button 
     else: 
      self.config(text="+1", fg="white", bg="green") 

def test(): 
    for i in range(3): 
     button = MykoButton(screen, text="1", fg="white", bg="blue", width=15, height=2) 
     button.grid(row=0, column=i) 

button1 = Button(screen, text="START", fg="black", bg="orange", width=25, height=2, command=test) 
button1.grid(row=1, columnspan=3, sticky="w"+"e"+"n"+"s") 

screen.mainloop() 

또한 코드 위에 새 버튼을 추가하는 대신 클릭 한 버튼을 업데이트하도록 변경했습니다.

+0

그래, 주목! :) 당신의 도움을 주셔서 감사합니다. – Naji

관련 문제