tkinter 窗口中的grab_set [英] grab_set in tkinter window

查看:93
本文介绍了tkinter 窗口中的grab_set的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到许多用于 tkinter 模式窗口的grab_set() 示例,但我无法让它在我的应用程序中工作.我正在创建第二个窗口作为我的设置"窗口,该窗口从主应用程序的菜单中调用.

I've seen many examples of grab_set() being used for modal windows for tkinter but I can't get it to work for my application. I am creating a second window as my 'Settings' window which is called from the Menu of the main application.

示例:

import tkinter as tk

class Main(tk.Tk):

    def __init__(self,*args, **kwargs):
        tk.Tk.__init__(self,*args, *kwargs)

        button = tk.Button(self,text="second window", command=lambda:Settings())
        button.pack()


class Settings(tk.Tk):

    def __init__(self,*args, **kwargs):
        tk.Tk.__init__(self,*args, *kwargs)
        button = tk.Button(self,text="quit", command=lambda: quit())
        button.pack()
        self.grab_set()

if __name__ == "__main__":
    app = Main()
    app.mainloop()

现在我仍然可以点击设置"按钮来创建尽可能多的 Settings 实例,只要电脑允许.在第二个窗口先关闭之前,如何限制主应用程序窗口的可点击性?

Right now I can still click the 'Settings' button to create as many instances of Settings as the pc would allow. How do I restrict clickability to the main application window until the second one is closed first?

推荐答案

这里是一个超级简单的例子,说明如何使用 Toplevel 打开另一个窗口,以及如何在主窗口中编辑内容顶级窗口.

Here is a super simple example of how you can open another window using Toplevel and how you can edit stuff on the main window from the Toplevel window.

它非常基本,但它应该是一个足够好的例子来说明在 tkinter 中打开新窗口需要什么.

Its very basic but it should be a good enough example to illustrate what is required in tkinter to open new window.

更新:添加了 Bryan 在评论中指出的 grab_set() 方法.

UPDATE: Added the grab_set() method as pointed out by Bryan in the comments.

grab_set() 方法根据 文档 将此应用程序的所有事件路由到此小部件.

The grab_set() method according to the documentation routes all events for this application to this widget.

注意:这将类似于最小、完整和可验证示例.它是尽可能少的代码,既能说明问题,又能测试.

Note: This would be along the lines of a Minimal, Complete, and Verifiable example. It is the smallest possible bit of code to get the point across while also being testable.

from tkinter import *


class GUI(Frame):


    def __init__(self, master, *args, **kwargs):
        Frame.__init__(self, master, *args, **kwargs)

        self.master = master
        self.my_frame = Frame(self.master)
        self.my_frame.pack()

        self.button1 = Button(self.master, text="Open New Window", command = self.open_toplevel_window)
        self.button1.pack()

        self.text = Text(self.master, width = 20, height = 3)
        self.text.pack()
        self.text.insert(END, "Before\ntop window\ninteraction")

    def open_toplevel_window(self):
        self.top = Toplevel(self.master)
        #this forces all focus on the top level until Toplevel is closed
        self.top.grab_set() 

        def replace_text():
            self.text.delete(1.0, END)
            self.text.insert(END, "Text From\nToplevel")

        top_button = Button(self.top, text = "Replace text in main window",
                            command = replace_text)
        top_button.pack()


if __name__ == "__main__":
    root = Tk()
    app = GUI(root)
    root.mainloop()

以下是为 Toplevel 使用单独类的示例:

Here is an example when using a separate class for the Toplevel:

from tkinter import *


class GUI(Frame):


    def __init__(self, master, *args, **kwargs):
        Frame.__init__(self, master, *args, **kwargs)

        self.master = master
        self.my_frame = Frame(self.master)
        self.my_frame.pack()

        self.button1 = Button(self.master, text="Open New Window",
                              command = open_toplevel_window)
        self.button1.pack()

        self.text = Text(self.master, width = 20, height = 3)
        self.text.pack()
        self.text.insert(END, "Before\ntop window\ninteraction")

class open_toplevel_window(Toplevel):


    def __init__(self, *args, **kwargs):
        Toplevel.__init__(self, *args, **kwargs)
        self.grab_set()

        def replace_text():
            app.text.delete(1.0, END)
            app.text.insert(END, "Text From\nToplevel")

        top_button = Button(self, text = "Replace text in main window",
                            command = replace_text)
        top_button.pack()


if __name__ == "__main__":
    root = Tk()
    app = GUI(root)
    root.mainloop()

这篇关于tkinter 窗口中的grab_set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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