由于大量错误,您的代码不会运行,但这一行肯定没有做您认为它正在做的事情:
self.frame.pack
要调用pack函数,必须包含
()
,例如:
self.frame.pack()
您会问您的代码是否是实现这一点的最佳方法。我认为你走在了正确的道路上,但我会改变一些事情。以下是我将如何构建代码。这只是创建“迷你框架”,它不做任何其他事情:
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.entry = tk.Entry(self)
self.submit = tk.Button(self, text="Submit", command=self.on_submit)
self.entry.pack(side="top", fill="x")
self.submit.pack(side="top")
def on_submit(self):
symbol = self.entry.get()
stock = Stock(self, symbol)
stock.pack(side="top", fill="x")
class Stock(tk.Frame):
def __init__(self, parent, symbol):
tk.Frame.__init__(self, parent)
self.symbol = tk.Label(self, text=symbol + ":")
self.value = tk.Label(self, text="123.45")
self.symbol.pack(side="left", fill="both")
self.value.pack(side="left", fill="both")
root = tk.Tk()
Example(root).pack(side="top", fill="both", expand=True)
root.mainloop()