簡體   English   中英

Python Tkinter - 如何在 tk.toplevel 類中獲取信息

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

您好,我在獲取 tk.Toplevel 類中的函數或變量的訪問權限方面遇到問題,因為他想要一個父級。 那么我怎樣才能從那個類中獲取信息呢?

我有兩個 .py 文件:

首先是框架核心:

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

第二個是我將主循環框架並獲取這些信息的地方。

那么我怎樣才能訪問一個函數,examplefunction,它在 tk.Toplevel 但在 tk.Tk 之外?

這與 tkinter 無關。 您需要保存對window的引用,然后您可以像訪問任何其他 python 對象一樣訪問它。

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

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

如果您需要在Test1的范圍之外執行此操作,則需要保存對Test1實例的引用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM