运行简单的Tkinter程序时发生属性错误。我已经检查了教程和相关问题,但我仍然不知道如何修复它。
import Tkinter as Tk
class Application(Tk.Frame):
def __init__(self, master=None):
Tk.Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
self.quitButton = Tk.Button(self, text='Quit',
command=self.quit)
self.quitButton.grid()
app = Application()
app.master.title('Sample application')
app.mainloop()
显示错误消息:
class Application(Tk.Frame):
AttributeError: class Tk has no attribute 'Frame'
任何人都可以帮助我吗?
答案 0 :(得分:0)
你永远不会将tk窗口传递给应用程序
import Tkinter # careful with importing it as Tk because Tk is already a sub module of Tkinter
class Application(Tkinter.Frame):
def __init__(self, master=None):
Tkinter.Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
self.quitButton = Tkinter.Button(self, text='Quit',
command=self.quit)
self.quitButton.grid()
window = Tkinter.Tk()
app = Application(master=window)
app.master.title('Sample application')
app.mainloop()