按下按钮后如何禁用Tkinter框架?

时间:2019-11-24 15:22:00

标签: python user-interface events tkinter frames

对于更大的应用程序,我有一个Tk框架,单击鼠标右键会生成该框架。我希望窗口的所有焦点都指向该框架(如果用户单击父级上的按钮,则什么也没有发生),然后当框架被破坏时,我希望它恢复正常。我创建了一个最小的示例来演示我尝试执行的操作,但它没有用。

我只是希望在单击“禁用外部”后禁用“根”按钮(打个招呼)。

import tkinter as tk

class app(tk.Frame):

    def __init__(self, parent,*args,**kwargs):
        tk.Frame.__init__(self, parent,*args,**kwargs)
        self.parent=parent

        toggler = tk.Button(self,text='DISABLE OUTER',command=self.toggleMain)
        toggler.pack(padx=20,pady=20)    

    def toggleMain(self):

        status= self.parent.grab_status()
        if not status:
            print('Outer Is Disabled (Hi Should not work)')
            self.parent.grab_set_global()
        else:
            print('Outer is Enabled (Hi should work)')
            self.parent.grab_release()

root = tk.Tk()

app = app(root,bg='red')
app.pack()


def SayHi():
    print('Hi There')

f=tk.Button(root,text='Say Hi',command=SayHi)
f.pack()

root.mainloop()

1 个答案:

答案 0 :(得分:1)

我不确定您要做什么,但是如果您只想禁用显示“ Say Hi”的按钮,则将其禁用。

import tkinter as tk


class app(tk.Frame):

    def __init__(self, parent,*args,**kwargs):
        tk.Frame.__init__(self, parent,*args,**kwargs)
        self.parent=parent

        toggler = tk.Button(self,text='DISABLE OUTER',command=self.toggleMain)
        toggler.pack(padx=20,pady=20)

    def toggleMain(self):

        status= self.parent.grab_status()
        if not status:
            print('Outer Is Disabled (Hi Should not work)')
            self.grab_set()
        else:
            print('Outer is Enabled (Hi should work)')
            self.grab_release()

root = tk.Tk()

app = app(root,bg='red')
app.pack()


def SayHi():
    print('Hi There')

f=tk.Button(root,text='Say Hi',command=SayHi)
f.pack()

root.mainloop(