简体   繁体   English

Tkinter框架对象将不会网格

[英]Tkinter Frame object will not grid

Running into an issue trying to grid a framed object I created in a rudimentary paint program. 尝试网格化我在基本绘画程序中创建的框架对象时遇到问题。

The instantiation code that gets the error is here: 出现错误的实例化代码在这里:

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()

The error I receive is related to the menu=Menu() operation and is: 我收到的错误与menu=Menu()操作有关,并且是:

TypeError: Menu() missing 1 required positional argument: 'Frame'

The Menu object is here: 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)

My confusion is how that positional error occurs. 我的困惑是该位置错误是如何发生的。 When I give the menu a frame ( self ), the grid method instead I get this error: 当我给菜单加一个框架( self )时,使用grid方法却出现此错误:

AttributeError: 'NoneType' object has no attribute 'grid'

I feel liking I'm missing a key part of working with frames, but I'm not sure what. 我很想念我缺少使用框架的关键部分,但是我不确定是什么。 Suggestions? 有什么建议吗?

You seem to want Menu to be a class, and therefore defined it as such with the __init__ method. 您似乎希望Menu是一个类,因此使用__init__方法将其定义为此类。 But you instead defined Menu to be a function, therefore all functions you stored inside are just defined as code that you would only use in the function. 但是您而是将Menu定义为一个函数,因此,您存储在其中的所有函数都只是定义为只能在该函数中使用的代码。 Change the def Menu to class Menu and it should be fine. def Menu更改为def Menu class Menu ,应该没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM