简体   繁体   English

Python - object 使用 Tkinter 没有属性错误

[英]Python - object has no attribute error using Tkinter

So I'm creating a quick little Tic-Tac-Toe game to practice with Tkinter, and I've ran into a small issue.所以我正在创建一个快速的小井字游戏来练习 Tkinter,我遇到了一个小问题。 I'm using a Window class to hold my methods and frames and one of the buttons I have in a frame has a command pointing to my "game()" method.我正在使用 Window class 来保存我的方法和框架,并且框架中的一个按钮有一个指向我的“游戏()”方法的命令。 Once I run the script, however, I get an AttributeError: 'Window' object has no attribute 'game' error.然而,一旦我运行脚本,我就会收到一个AttributeError: 'Window' object has no attribute 'game'错误。

Here's my code so far:到目前为止,这是我的代码:

from tkinter import *

class Window(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.master = master
        self.master.title("Tic-Tac-Toe")
        self.grid()

        # GUI Grid
        for row in range(2):
            if row == 0:
                self.master.grid_rowconfigure(row, weight=1)
            else:
                self.master.grid_rowconfigure(row, weight=6)
        for col in range(3):
            self.master.grid_columnconfigure(col, weight=1)

        # Game Loop
        def game(self):
            switch = 0
            game_status = True
            Frame2.button.config(status=ENABLED) 


        # Game Title
        Frame1 = Frame(self.master, bg="#424242")
        Frame1.grid(row=0, column=0, columnspan=3, sticky=W+E+N+S)
        Frame1.label = Label(Frame1, font=("Arial", 16), text="Tic-Tac-Toe", bg="#424242", fg="#FDD835")
        Frame1.button = Button(Frame1, bd=0, font=("Arial", 10), text="Start", bg="#FDD835", fg="#212121", command=self.game)
        Frame1.label.pack()
        Frame1.button.pack()

        # Game Board
        Frame2 = Frame(self.master, bg="#BDBDBD")
        Frame2.grid(row=1, column=0, columnspan=3, sticky=W+E+N+S)
        for i in range(3):
            Frame2.grid_rowconfigure(i, weight=1)
            Frame2.grid_columnconfigure(i, weight=1)
        for x in range(3):
            for y in range(3):
                Frame2.button = Button(Frame2, bd=1, state=DISABLED, font=("Arial", 18), bg="#BDBDBD", fg="#FFFFFF")
                Frame2.button.grid(row=x, column=y, sticky=W+E+N+S)



root = Tk()
root.geometry("500x500")
app = Window(root)
root.mainloop()

The button in question is Frame1.button.有问题的按钮是 Frame1.button。 I've setting the command to command=self.master.game to no avail.我已将命令设置为command=self.master.game无济于事。 I appreciate all the help!我感谢所有的帮助!

The problem is your game function is underneath the init function.问题是你的game function 在init function 下面。

And you are getting a lot of extra errors, so I completely redid your code.而且你得到了很多额外的错误,所以我完全重新编写了你的代码。

from tkinter import *

class Window(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.master = master
        self.master.title("Tic-Tac-Toe")
        self.grid()

        # GUI Grid
        for row in range(2):
            if row == 0:
                self.master.grid_rowconfigure(row, weight=1)
            else:
                self.master.grid_rowconfigure(row, weight=6)
        for col in range(3):
            self.master.grid_columnconfigure(col, weight=1)

        # Game Loop
    def game():
        global Frame2
        switch = 0
        game_status = True
        Frame2.button.config(state="normal")


# Game Title 
root = Tk()
root.geometry("500x500")
app = Window(root)
Frame1 = Frame(root, bg="#424242")
def run():
    Window.game()
Frame1.grid(row=0, column=0, columnspan=3, sticky=W+E+N+S)
Frame1.label = Label(Frame1, font=("Arial", 16), text="Tic-Tac-Toe", bg="#424242", fg="#FDD835")
Frame1.button = Button(Frame1, bd=0, font=("Arial", 10), text="Start", bg="#FDD835", fg="#212121", command=run)
Frame1.label.pack()
Frame1.button.pack()

# Game Board
Frame2 = Frame(root, bg="#BDBDBD")
Frame2.grid(row=1, column=0, columnspan=3, sticky=W+E+N+S)
for i in range(3):
    Frame2.grid_rowconfigure(i, weight=1)
    Frame2.grid_columnconfigure(i, weight=1)
for x in range(3):
    for y in range(3):
        Frame2.button = Button(Frame2, bd=1, state=DISABLED, font=("Arial", 18), bg="#BDBDBD", fg="#FFFFFF")
        Frame2.button.grid(row=x, column=y, sticky=W+E+N+S)
root.mainloop()

Hope this helps!希望这可以帮助!

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

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