当打开一个新的 tkinter 窗口时,我只希望用户能够点击新窗口上的按钮。他们不应该能够从作为应用程序一部分的其他窗口中单击按钮。我将如何做到这一点?
这是我的代码片段:
def exportEFS(self):
self.exportGUI = Toplevel()
Button(self.exportGUI, text='Backup', command=self.backup).pack(padx=100,pady=5)
Button(self.exportGUI, text='Restore', command=self.restore).pack(padx=100,pady=5)
def backup(self):
self.backupWindow = Toplevel()
message = "Enter a name for your Backup."
Label(self.backupWindow, text=message).pack()
self.entry = Entry(self.backupWindow,text="enter your choice")
self.entry.pack(side=TOP,padx=10,pady=12)
self.button = Button(self.backupWindow, text="Backup",command=self.backupCallBack)
self.button.pack(side=BOTTOM,padx=10,pady=10)
在此片段中,一旦打开 backupWindow,exportGUI 将保持打开状态,但用户在打开 backupWindow 时不应单击“备份”或“恢复”。
谢谢!
最佳答案
您需要在 TopLevel 窗口上调用 grab_set,以便将所有键盘和鼠标事件发送到该窗口。
def exportEFS(self):
self.exportGUI = Toplevel()
Button(self.exportGUI, text='Backup', command=self.backup).pack(padx=100,pady=5)
Button(self.exportGUI, text='Restore', command=self.restore).pack(padx=100,pady=5)
def backup(self):
self.backupWindow = Toplevel()
self.backupWindow.grab_set()
message = "Enter a name for your Backup."
Label(self.backupWindow, text=message).pack()
self.entry = Entry(self.backupWindow,text="enter your choice")
self.entry.pack(side=TOP,padx=10,pady=12)
self.button = Button(self.backupWindow, text="Backup",command=self.backupCallBack)
self.button.pack(side=BOTTOM,padx=10,pady=10)
关于Python tkinter,如何禁止点击背景按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11123185/