简体   繁体   English

如何从另一个 class 中引用我的 Tkinter“根”实例?

[英]How do I reference my Tkinter "root" instance from within another class?

I am using the below code to create a window and raise different frames around.我正在使用下面的代码创建一个 window 并提高不同的框架。 I need to create a toplevel, but I am trying to make the Toplevel window appear within the bounds of the main window.我需要创建一个顶层,但我试图让顶层 window 出现在主 window 的范围内。 Looking around on here, I am finding the way to do this is to use wm_transient(root).环顾四周,我发现这样做的方法是使用 wm_transient(root)。 The problem is my "root" is called app, and its not accessible from within the class where I am calling these functions.问题是我的“根”被称为应用程序,它不能从我调用这些函数的 class 中访问。

So my popup function I am calling, I am trying to make my popup (toplevel) have the attribute of wm_transient, but I have to set the master to root (app in my application) and cannot figure out how to make this work.因此,我正在调用我的弹出窗口 function,我试图使我的弹出窗口(顶层)具有 wm_transient 的属性,但我必须将 master 设置为 root(我的应用程序中的应用程序)并且无法弄清楚如何使它工作。

So my underlying question is how to make the toplevel appear within the bounds of my main window, but what I would really like to know is how to make calls/reference my root window.所以我的基本问题是如何使顶层出现在我的主要 window 的范围内,但我真正想知道的是如何调用/引用我的根 window。

import tkinter as tk

class APP1(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.title('APP')
        self.geometry("552x700")
        self.resizable(False, False)

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        self.frames["MenuPage"] = MenuPage(parent=container, controller=self)
        self.frames["MenuPage"].grid(row=0, column=0, sticky="nsew")
        self.show_frame("MenuPage")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()

class MenuPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        def popup():
            x = tk.Toplevel(self)
            x.wm_transient(app)

        template = tk.Button(self, text='Popup', height=3, width=20, bg='white', font=('12'), command=lambda: popup())
        template.pack(pady=50)


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

In your specific example you already have two ways to access your "root".在您的具体示例中,您已经有两种访问“root”的方法。

First, your root is app , the instance of APP1 , and it's a global variable so it is already available everywhere.首先,您的根是app ,即APP1的实例,它是一个全局变量,因此它已经在任何地方都可用。 It's global because you define it outside the context of a function or class.它是全局的,因为您在 function 或 class 的上下文之外定义它。

Second, you pass the instance of APP1 as the controller parameter in your MenuPage class.其次,将APP1的实例作为controller参数传递到MenuPage class 中。 You're saving that as self.controller , so anywhere you need the instance of APP1 you can use self.controller .您将其保存为self.controller ,因此任何需要APP1实例的地方都可以使用self.controller

For more help understanding the code you've copied, see Switch between two frames in tkinter which contains links to many questions that have been asked about this code.有关了解您复制的代码的更多帮助,请参阅在 tkinter 中的两个帧之间切换,其中包含许多有关此代码的问题的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 class 实例中的信息继承到另一个实例中? - How do I inherit information from an instance of a class into another instance? 如何从嵌套类中引用另一个嵌套类 - How to reference another nested class from within nested class Tkinter - 如何将实例变量传递给另一个类? - Tkinter - How to pass instance variable to another class? 如何从 Python 中的 class 中创建嵌套 class 的实例? - How do I create an instance of a nested class from within that class in Python? 如何从同一个 class 中的一个字段引用另一个 Pydantic 字段? - How to reference another Pydantic field from a field within the same class? 从 tkinter 中的另一个类/文件更改根 Tk 的 state - Changing the state of root Tk from another class/file in tkinter 你如何从一个类的另一个实例调用一个类的函数? - How do you call a function of a class from another instance of a class? 如何引用类中的函数作为菜单按钮的命令? - How do I reference a function that is within the class as a command for a menu button? Python-如何从根目录加载我的自定义类方法? - Python - how do i load my custom class methods from a root directory? Python/Tkinter: root as a class / initializing instance variables in a tkinter class - Python/Tkinter: root as a class / initializing instance variables in a tkinter class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM