Tkinter - 如何改进“grab_set()"?方法行为? [英] Tkinter - How can I improve the "grab_set()" method behavior?

查看:30
本文介绍了Tkinter - 如何改进“grab_set()"?方法行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的代码,其中我们有一个带有两个按钮的主窗口.第一个打开一个新窗口,第二个打开一个消息框.

I wrote a simple code in which we have a main window with two buttons. the first one opens a new window, the second one opens a message box.

当我打开消息框窗口时,我无法以任何方式与主窗口进行字母交互,如果我点击它,系统会响起铃声并且消息框会闪烁.我想在另一个窗口复制相同的行为,但是如何?当我打开它时,由于 grab_set() 方法,我无法与主窗口交互,但在这种情况下,没有系统铃声播放,也没有出现 flash 样式,毕竟我仍然可以移动主窗口通过其标题栏,我不喜欢它.如何在新窗口中复制与消息框相同的行为?

when I open the message box window, I can't letterally interact with the main window in any way, if I click on it the system bell plays and the message box flashes. I would like to replicate the same behhaviour the other window, but how? when I apen it I can't interact with the main window because of the grab_set() method, but in this case no system bell plays, no flash style appears, and after all I can still move the main window via its title bar and I don't like it. how can I replicate the same behaviour seen with the message box, in the new window?

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 图像):

below you can see my software behavior in Windows 10 (it's a gif image):

http://www.imagebam.com/image/ac4a4a1347175889

推荐答案

在 Windows 中,您可以尝试:

In Windows, you can try:

  • 调用 attributes('-disabled', 1) 禁用父窗口
  • 将顶层设置为父级的临时窗口
  • 调用 wait_window() 等待顶层关闭/销毁
  • 调用 attributes('-disabled', 0) 启用父窗口
  • call attributes('-disabled', 1) to disable the parent window
  • set the toplevel as transient window of parent
  • call wait_window() to wait for the toplevel closed/destroyed
  • call attributes('-disabled', 0) to enable the parent window
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)

这篇关于Tkinter - 如何改进“grab_set()"?方法行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆