I want to make a GUI but i get this error: self.frame.grid(row=0, column=0, sticky="nsew") AttributeError: 'function' object has no attribute 'grid'

huangapple go评论107阅读模式
英文:

I want to make a GUI but i get this error: self.frame.grid(row=0, column=0, sticky="nsew") AttributeError: 'function' object has no attribute 'grid'

问题

抱歉,以下是您的代码的翻译部分:

抱歉关于这个问题的布局有些混乱这是我第一次在stackoverflow上发帖我想要为井字棋创建一个GUI游戏菜单)。并且希望能够在GUI中的任何位置放置按钮因此我使用了Grid布局

```python
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="欢迎来到井字棋", font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        button1 = tk.Button(self, text="开始游戏",
                            command=lambda: controller.show_frame(Game))
        button1.pack()

        button2 = tk.Button(self, text="难度选择",
                            command=lambda: controller.show_frame(Difficulty))
        button2.pack()

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

        label1 = tk.Label(self, text="由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="新游戏")
        button1.pack()

        button2 = tk.Button(self, text="返回主菜单",
                            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="1对1", command=lambda: controller.show_frame(MainWindow))
        button1.pack()

        button2 = tk.Button(self, text="返回主菜单",
                            command=lambda: controller.show_frame(Game))
        button2.pack()

gui = SeaofBTCapp()
gui.mainloop()

但是当我使用Grid布局时,我收到以下错误:

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布局,但那不起作用,有人知道如何修复这个问题吗?

英文:

Sorry about the layout of this question, my first time working with stackoverflow posts.
I want to make a GUI for Tic Tac Toe (Game menu). And have the abillity to put buttons where ever i want in the GUI so i used Grid.

import tkinter as tk
LARGE_FONT= (&quot;Verdana&quot;, 12)
class SeaofBTCapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side=&quot;top&quot;, fill=&quot;both&quot;, 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=&quot;nsew&quot;)
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=&quot;Welcom to TIC TAC TOE&quot;, font=LARGE_FONT)
label.pack(pady=10, padx=10)
button1 = tk.Button(self, text=&quot;Start&quot;,
command=lambda: controller.show_frame(Game))
button1.pack()
button2 = tk.Button(self, text=&quot;Diffeculty&quot;,
command=lambda: controller.show_frame(Difficulty))
button2.pack()
button3 = tk.Button(self, text=&quot;Quit&quot;, command=self.Quit)
button3.pack()
label1 = tk.Label(self, text=&quot;Made by VindictaOG&quot;)
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=&quot;New Game&quot;)
button1.pack()
button2= tk.Button(self, text=&quot;Back to homescreen&quot;,
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=&quot;1V1&quot;, command=lambda: controller.show_frame(MainWindow))
button1.pack()
button2 = tk.Button(self, text=&quot;Back to homescreen&quot;,
command=lambda: controller.show_frame(Game))
button2.pack()
gui = SeaofBTCapp() 
gui.mainloop()

But when i use grid i get this error:

Traceback (most recent call last):
File &quot;/home/ivar/PycharmProjects/J1B2Afvink6/BKE.py&quot;, line 82, in &lt;module&gt;
gui = SeaofBTCapp()
File &quot;/home/ivar/PycharmProjects/J1B2Afvink6/BKE.py&quot;, line 27, in __init__
frame.grid(row=0, column=0, sticky=&quot;nsew&quot;)
TypeError: wm_grid() got an unexpected keyword argument &#39;row&#39;

I tried it with pack but that would not work, does someone know how to fix this?

答案1

得分: 0

应该从tk.Frame继承,而不是从tk.Tk继承MainWindow、Game和Difficulty。另外,StartPage未定义。@acw1668

使用(tk.Frame, tk.Tk)而不仅仅是(tk.Tk)解决了问题。

英文:

Should inherit from tk.Frame instead of tk.Tk for MainWindow, Game and Difficulty. Also StartPage is not defined. @acw1668

Using (tk.Frame, tk.Tk) instead of just (tk.Tk) solved the problem.

huangapple
  • 本文由 发表于 2020年1月7日 00:03:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定