jac*_*ian 5 python class tkinter function button
我正在创建一个Python(3.4.3) - tkinter程序,我想知道是否可以def从另一个内部引用(self.get_details)class按钮的命令.我无法在任何地方找到这个问题的答案,所以我想我只是问.
例:
import tkinter as tk
...
class Widgets(tk.Frame):
def __init__(self, parent):
tk.Frame.__init___(self, parent)
self.parent = parent
self.initUI()
def initUI():
# Lots of other different tkinter widgets go here
self.button = tk.Button(command=App(get_details))
self.button.pack()
class PopUp(tk.TopLevel): ....
class App(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def get_details(self):
# Complete a function
def initUI(self):
self.parent.title("My Application")
self.style = Style()
self.style.theme_use("default")
self.pack()
self.widgets = Widgets(self)
self.widgets.pack(side="top", anchor="center", fill="both", expand=True)
if __name__ == "__main__":
root = tk.Tk()
App(root).pack(side="top", fill="both", expand=True)
root.resizable(0,0)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
所以我想要一个属于该类的按钮Widgets()来调用属于该类的命令,def get_details(self)该类App()具有在其中Widgets()打包的类.
我希望我的描述性足够,很难说出这个问题.我总是对Python有点新意.谢谢!
编辑:
正如我所建议的那样,我将其更改为self.parent.get_details()有效!然而,当我从引用的Tkinter部件Widgets()的内部类def get_details(),例如:self.button,我得到:
AttributeError: 'App' object has no attribute 'button'
Run Code Online (Sandbox Code Playgroud)
所以我尝试将按钮引用为:self.parent.button,我收到的:
AttributeError: 'tkapp' object has no attribute 'button'
Run Code Online (Sandbox Code Playgroud)
我应该如何调用/引用按钮?谢谢!
在 中App,您创建一个Widgets对象并保存对其命名的引用self.widgets。App当您这样做时,您会将对该对象的引用传递给该Widgets对象。该App实例引用在Widgets对象中保存为self.parent. 请记住, 的这两种用法self引用不同的对象:而 in App,self则引用该App对象的当前实例。而 in 中Widgets,self则指该对象的当前实例Widgets。要引用其他类中的内容,您必须使用正确的引用并遵循路径。
在一个App实例中:
self this App instance
.widgets the Widgets instance it contains
.button the button in that Widget instance
Run Code Online (Sandbox Code Playgroud)
在一个Widgets实例中:
self this Widgets instance
.parent the App object that created this Widgets object
.get_details the get_details function in the parent App object
Run Code Online (Sandbox Code Playgroud)
在下文中,我修复了代码的剩余位(...导致语法错误、Style未定义等),并提供了一个小示例,说明如何从另一个对象引用每个对象。该按钮最初显示“你好”,您可以通过单击将其更改为“再见”。
import tkinter as tk
class Widgets(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
# Lots of other different tkinter widgets go here
self.button = tk.Button(text='hello', command=self.parent.get_details)
self.button.pack()
class App(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def get_details(self):
self.widgets.button.config(text='goodbye')
def initUI(self):
self.parent.title("My Application")
self.pack()
self.widgets = Widgets(self)
self.widgets.pack(side="top", anchor="center", fill="both", expand=True)
if __name__ == "__main__":
root = tk.Tk()
App(root).pack(side="top", fill="both", expand=True)
root.resizable(0,0)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)