[英]calling a method from tkinter class
I'm very new on Python and trying to develop a fake data generator software for data scientists to be able to learn faster. 我是Python的新手,正在尝试开发一种伪造的数据生成器软件,以使数据科学家能够更快地学习。 My question is about how we can call a method from a sub method between tkinter object classes. 我的问题是关于如何从tkinter对象类之间的子方法调用方法。 I have root class which creates root window and toplevel class which creates toplevel (sub-window). 我有创建根窗口的根类和创建顶层(子窗口)的顶层类。 My sub-window class needs to update a widget in root window and I couldn't have done it. 我的子窗口类需要在根窗口中更新窗口小部件,而我做不到。
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()
In your specific case, you would call the function based off of the global myApp
class: 在您的特定情况下,您将基于全局myApp
类调用该函数:
class my_sub_window():
def update_root(self):
myApp.dosomething()
Though, it's generally not a good idea to rely on global variables like this. 但是,通常依赖这样的全局变量并不是一个好主意。 The most common solution is to pass the instance of Menu
to the my_sub_window
class. 最常见的解决方案是将Menu
的实例传递给my_sub_window
类。
Example: 例:
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.