【问题标题】:How do I stop a button command when another button is clicked in python?在python中单击另一个按钮时如何停止按钮命令?
【发布时间】:2017-12-15 20:26:05
【问题描述】:

所以我的意图是当用户单击其中一个按钮时,其他按钮即使在单击时也不执行任何操作,因此基本上是在用户单击它们时停止按钮执行它们的命令,前提是它们有之前点击了另一个按钮,我不知道我是否表达得很好,如果我没有表达,我很抱歉,但这是我的代码:

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()

【问题讨论】:

    标签: python button tkinter click


    【解决方案1】:

    您可以将其他按钮的状态设置为DISABLED 以将它们变灰并防止点击。这将是使用跟踪其实例的子类的理想场所。

    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()
    

    另外,请注意,我更改了您的代码以更新单击的按钮,而不是在其顶部放置一个新按钮。

    【讨论】:

    • 是的,我注意到了! :) 感谢您的帮助。
    猜你喜欢
    • 2022-08-13
    • 2012-10-17
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    相关资源
    最近更新 更多