我编写了一个简单的代码,其中我们有一个主窗口和两个按钮。第一个窗口打开一个新窗口,第二个窗口打开一个消息框。
当我打开消息框窗口时,我不能以任何方式与主窗口进行字母交互,如果我点击它,系统铃声就会播放,消息框就会闪烁。我想复制另一个窗口的同样的行为,但如何复制?当我使用它时,由于grab_set()
方法,我不能与主窗口交互,但是在这种情况下,没有播放系统钟,没有闪存样式出现,毕竟我仍然可以通过标题栏移动主窗口,我不喜欢它。如何复制在新窗口中的消息框中看到的相同行为?
from tkinter import *
from tkinter import ttk, messagebox
class MainWindow:
def __init__(self):
self.parent=Tk()
self.parent.title("Main Window")
self.parent.configure(background="#f0f0f0")
self.parent.geometry("300x200+360+200")
self.NewWindowButton=ttk.Button(self.parent, text="Open the new Window", command=lambda: NewWindow(self.parent))
self.MsgBoxButton=ttk.Button(self.parent, text="Open a Message Box", command=lambda: messagebox.showerror("Error", "Error"))
self.NewWindowButton.pack()
self.MsgBoxButton.pack()
self.parent.mainloop()
class NewWindow:
def __init__(self, parent):
self.window, self.parent=Toplevel(parent), parent
self.window.title("New Window")
self.window.configure(background="#f0f0f0")
self.window.geometry("300x200+360+200")
self.window.resizable (width=False, height=False)
self.window.grab_set()
def main():
app=MainWindow()
if __name__=="__main__":
main()
下面您可以看到我在Windows 10中的软件行为(这是一个gif映像):
发布于 2020-06-17 01:53:55
在Windows中,您可以尝试:
禁用父窗口的closed/destroyed
attributes('-disabled', 1)
wait_window()
的临时窗口,以等待toplevel
attributes('-disabled', 0)
启用父窗口H 211F 212
class NewWindow:
def __init__(self, parent):
try:
parent.attributes('-disabled', 1) # disable the parent
self.window, self.parent = Toplevel(parent), parent
self.window.title("New Window")
self.window.configure(background="#f0f0f0")
self.window.geometry("300x200+360+200")
self.window.resizable (width=False, height=False)
self.window.transient(parent)
self.window.grab_set()
parent.wait_window(self.window) # wait for current window to close
finally:
# enable the parent
parent.attributes('-disabled', 0)
https://stackoverflow.com/questions/62417582
复制相似问题