从其他方法禁用按钮状态

时间:2014-03-31 11:22:57

标签: python tkinter

我是python和tkinter的新手,因此在使用其他方法启用按钮状态时遇到问题,如下所示:

def init():
 p1Buzz = Button(root, text = "P1 BUZZ", command = buzzed)
 p1A = Button(root, text = "A")
 p1A.config(state='disabled')

def buzzed():
 #how can you enable p1A here again

任何帮助表示赞赏

1 个答案:

答案 0 :(得分:2)

使用class&然后在self的帮助下,您可以拨打p1A&将其状态设置为normal或仅将其设为Global(不推荐)&然后设置为normal。我为你制作了一个示例程序:

class Abc:

    def __init__(self,root):
        p1Buzz = Button(root, text = "P1 BUZZ", command = self.buzzed)
        p1Buzz.pack()
        self.p1A = Button(root, text = "A")
        self.p1A.config(state='disabled')
        self.p1A.pack()

    def buzzed(self):
        self.p1A.config(state='normal')

if __name__ == '__main__':
    root = Tk()
    myapp = Abc(root)
    root.mainloop()

或使用global

from Tkinter import *
p1A=Button

def init(root):
    global p1A
    p1Buzz = Button(root, text = "P1 BUZZ", command = buzzed)
    p1Buzz.pack()
    p1A = Button(root, text = "A")
    p1A.config(state='disabled')
    p1A.pack()

def buzzed():
    p1A.config(state='normal')

root=Tk()
init(root)
root.mainloop()