简体   繁体   中英

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. So how can i get informations from that class?

I've two .py files:

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?

This has nothing to do with tkinter. You need to save a reference to window , and then you can access it like any other python object.

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.call_examplefunction()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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