【问题标题】:Tkinter Frame object will not gridTkinter Frame 对象不会网格化
【发布时间】: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__)。

标签: python tkinter widget


【解决方案1】:

您似乎希望Menu 成为一个类,因此使用__init__ 方法将其定义为这样。但是您将Menu 定义为一个函数,因此您存储在其中的所有函数都只是定义为您只会在函数中使用的代码。把def Menu改成class Menu就可以了。

【讨论】:

  • 欢迎您!你能在我的回答中验证我的答案吗?
猜你喜欢
  • 1970-01-01
  • 2015-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-02
  • 1970-01-01
  • 2020-01-01
  • 2020-03-11
相关资源
最近更新 更多