简体   繁体   English

Python Tkinter - 如何在 tk.toplevel 类中获取信息

[英]Python Tkinter - how to get informations in a tk.toplevel class

Hello i'm having issues about get acces of functions or variables in a tk.Toplevel class because he wants a parent.您好,我在获取 tk.Toplevel 类中的函数或变量的访问权限方面遇到问题,因为他想要一个父级。 So how can i get informations from that class?那么我怎样才能从那个类中获取信息呢?

I've two .py files:我有两个 .py 文件:

the first is the frame core:首先是框架核心:

import tkinter as tk

class Test2(tk.Toplevel): 
    def __init__(self, parent):
        super().__init__(parent)
        #[...]
        self.createwidg()

    def createwidg(self):
        #[...]
        pass
        
    def examplefunction(self):
        #[...]
        return True

class Test1(tk.Tk): 
    def __init__(self):
        super().__init__()
        #[...]
        self.createwidg()

    def createwidg(self):
        #[...]
        pass
        
    def openwindow(self):
        window=Test2(self)
        window.grab_set()

the second one is where I will mainloop the frame and get access of these informations.第二个是我将主循环框架并获取这些信息的地方。

So how can I access a function, examplefunction, that's in tk.Toplevel but out the tk.Tk?那么我怎样才能访问一个函数,examplefunction,它在 tk.Toplevel 但在 tk.Tk 之外?

This has nothing to do with tkinter.这与 tkinter 无关。 You need to save a reference to window , and then you can access it like any other python object.您需要保存对window的引用,然后您可以像访问任何其他 python 对象一样访问它。

class Test1(tk.Tk):
    ...
    def openwindow(self):
        self.window=Test2(self)
        self.window.grab_set()

    def call_examplefunction(self):
        self.window.examplefunction()

If you need to do this outside of the scope of Test1 , you need to save a reference to the instance of Test1 :如果您需要在Test1的范围之外执行此操作,则需要保存对Test1实例的引用:

test1 = Test1(...)
...
test1.call_examplefunction()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM