[英]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.