Python tkinter,如何禁止点击背景按钮

标签 python button background window tkinter

当打开一个新的 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/

相关文章:

python - PyGame 循环,每个循环中创建的 Sprite 不更新

python - 使用 jwt 进行 FastAPI 身份验证,但不使用 OAuth2 - 是否可以自定义内置 OAuth2PasswordBearer?

xcode - 如何使 scrollView 自动滚动?

c# - 通过在 C# GUI 中按下按钮调用 C++ DLL

html - 位置 :relative breaking background-clip: text

ios - 更改导航栏图像 - 不起作用

Python-从字符串中获取括号元组列表

python - ECLIPSE 中 pydev 交互 session 期间光标位置错误

c# - 向按钮控件添加自定义属性 C#

python - PyQt:重置或删除 QTreeWidgetItem 的背景颜色