簡體   English   中英

Tkinter框架對象將不會網格

[英]Tkinter Frame object will not grid

嘗試網格化我在基本繪畫程序中創建的框架對象時遇到問題。

出現錯誤的實例化代碼在這里:

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 )時,使用grid方法卻出現此錯誤:

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

我很想念我缺少使用框架的關鍵部分,但是我不確定是什么。 有什么建議嗎?

您似乎希望Menu是一個類,因此使用__init__方法將其定義為此類。 但是您而是將Menu定義為一個函數,因此,您存儲在其中的所有函數都只是定義為只能在該函數中使用的代碼。 def Menu更改為def Menu class Menu ,應該沒問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM