[英]Tkinter: How to deiconify a popup window (and give it focus)?
我在 Ubuntu 20.04 上并努力使iconify()
和deiconify()
为弹出窗口 window 工作。 目前弹出 window 在图标化后不会去图标deiconify()
。 这是一个最小的例子:
import tkinter as tk
class PopupWindow(tk.Toplevel):
""" Show summary
"""
def __init__(self, root):
super().__init__(master=root)
self.root = root
self.geometry("600x400")
self.title("Summary")
self.protocol("WM_DELETE_WINDOW", self.close_window)
button = tk.Button(
self, text="Close", command=self.close_window)
button.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
self.focus_force()
self.grab_set()
def close_window(self):
self.iconify()
#self.withdraw()
#self.transient()
#self.root.grab_set()
#self.root.focus_force()
class MainWindow(tk.Tk):
""" The main window
"""
def __init__(self):
tk.Tk.__init__(self)
self.popup_window = None
self.configure_window()
def open_popup_window(self):
""" Opens the popup window
"""
if self.popup_window is None:
self.popup_window = PopupWindow(self)
else:
self.popup_window.deiconify()
#self.popup_window.lift()
self.popup_window.focus_force()
#self.popup_window.grab_set()
def configure_window(self):
""" Configure the main window
"""
self.geometry("600x400")
self.title("Cinema bookkeeper")
self.bind_all('<Control-w>', lambda e: self.destroy())
button = tk.Button(
self, text="Open popup window", command=self.open_popup_window)
button.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
def main():
window = MainWindow()
window.mainloop()
main()
当我在第一次图标化后第二次单击“打开弹出窗口”按钮时,没有任何反应。 预期的行为是弹出 window 将重新出现并获得焦点。 如果我使用grab_set()
我可以让它工作,但我不想让弹出 window 模态,这样两个 windows 仍然可以同时接收输入。
grab_set
方法将应用程序的所有事件路由到指定的小部件。
由于您在弹出窗口 window 上调用了self.grab_set()
,因此主 window 将拒绝接受任何事件(从不单击按钮开始),因此程序不起作用。
删除它将使其工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.