![](/img/trans.png)
[英]Calling a method from a class from another file giving weird error in tkinter
[英]calling a method from tkinter class
我是Python的新手,正在嘗試開發一種偽造的數據生成器軟件,以使數據科學家能夠更快地學習。 我的問題是關於如何從tkinter對象類之間的子方法調用方法。 我有創建根窗口的根類和創建頂層(子窗口)的頂層類。 我的子窗口類需要在根窗口中更新窗口小部件,而我做不到。
enter code here
class Menu():
def __init__(self,master):
....
def openwindow(self):
secondwindow = my_sub_window(root)
def dosomething(self):
....
class my_sub_window():
def __init__(self, master):
update_root()
def update_root(self):
dosomething() # How can I call dosomething method in Menu() class?
root = Tk()
myApp = Menu(root)
root.mainloop()
在您的特定情況下,您將基於全局myApp
類調用該函數:
class my_sub_window():
def update_root(self):
myApp.dosomething()
但是,通常依賴這樣的全局變量並不是一個好主意。 最常見的解決方案是將Menu
的實例傳遞給my_sub_window
類。
例:
class Menu():
def openwindow(self):
secondwindow = my_sub_window(root)
...
class my_sub_window():
def __init__(self, root):
self.root = root
def update_root(self):
self.root.dosomething()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.