简体   繁体   中英

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:

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

The Menu object is here:

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:

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. 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. Change the def Menu to class Menu and it should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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