我想制作一个GUI,但出现以下错误:self.frame.grid(row = 0,column = 0,sticky =“ nsew”)AttributeError:'function'对象没有属性'grid'

问题描述 投票:0回答:1

很抱歉,我第一次使用stackoverflow帖子时,这个问题的布局。我想为井字游戏(游戏菜单)制作一个GUI。并有能力将按钮放置在GUI中我想要的任何位置,因此我使用了Grid。

import tkinter as tk

LARGE_FONT= ("Verdana", 12)

class SeaofBTCapp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        container.pack(side="top", fill="both", expand=True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (MainWindow, Game, Difficulty):
            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class MainWindow(tk.Tk):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Welcom to TIC TAC TOE", font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        button1 = tk.Button(self, text="Start",
                            command=lambda: controller.show_frame(Game))
        button1.pack()

        button2 = tk.Button(self, text="Diffeculty",
                            command=lambda: controller.show_frame(Difficulty))
        button2.pack()

        button3 = tk.Button(self, text="Quit", command=self.Quit)
        button3.pack()

        label1 = tk.Label(self, text="Made by VindictaOG")
        label1.pack()

    def Quit(self):
            exit()


class Game(tk.Tk):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        button1 = tk.Button(self, text="New Game")
        button1.pack()

        button2= tk.Button(self, text="Back to homescreen",
                           command=lambda: controller.show_frame(MainWindow))
        button2.pack()


class Difficulty(tk.Tk):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        button1 = tk.Button(self, text="1V1", command=lambda: controller.show_frame(MainWindow))
        button1.pack()

        button2 = tk.Button(self, text="Back to homescreen",
                            command=lambda: controller.show_frame(Game))
        button2.pack()

gui = SeaofBTCapp() 
gui.mainloop()

但是当我使用网格时,出现此错误:

Traceback (most recent call last):
  File "/home/ivar/PycharmProjects/J1B2Afvink6/BKE.py", line 82, in <module>
    gui = SeaofBTCapp()
  File "/home/ivar/PycharmProjects/J1B2Afvink6/BKE.py", line 27, in __init__
    frame.grid(row=0, column=0, sticky="nsew")
TypeError: wm_grid() got an unexpected keyword argument 'row'

我尝试使用pack进行了测试,但是那行不通,有人知道如何解决此问题吗?

python-3.x class tkinter grid
1个回答
0
投票

对于MainWindow,Game和Difficulty,应继承自tk.Tk,而不是tk.Tk。此外,未定义StartPage。 @ acw1668

使用(tk.Frame,tk.Tk)代替了(tk.Tk)解决了问题。

© www.soinside.com 2019 - 2024. All rights reserved.