【发布时间】:2018-04-23 15:22:14
【问题描述】:
在尝试对我在基本绘图程序中创建的框架对象进行网格化时遇到问题。
得到错误的实例化代码在这里:
from tkinter import *
from Menu import Menu
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self,master)
self.grid()
self.createWidgets()
def createWidgets(self):
#Imports each of the frames from the collection from the various widgets
menu=Menu()
menu.grid(row=0,column=0,columnspan=2)
app=Application()
app.master.title=('Sample Application')
app.mainloop()
我收到的错误与menu=Menu() 操作有关,是:
TypeError: Menu() missing 1 required positional argument: 'Frame'
Menu 对象在这里:
from tkinter import *
import CommandStack
def Menu(Frame):
def __init__(self, master=None):
Frame.__init__(self,master)
self.createWidgets()
def createWidgets(self):
self.new=Button(self,command=CommandStack.new())
self.save=Button(self,command=CommandStack.save())
self.save.grid(row=0,column=1)
self.load=Button(self,command=CommandStack.load())
self.load.grid(row=0,column=2)
我的困惑是位置错误是如何发生的。当我给菜单一个框架(self)时,我得到了这个错误:
AttributeError: 'NoneType' object has no attribute 'grid'
我觉得我错过了使用框架的关键部分,但我不确定是什么。有什么建议吗?
【问题讨论】:
-
您打算将
Menu用作类还是函数?您已将其定义为函数 (def),但将其实现为类 (__init__)。