简体   繁体   中英

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. 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. Once I run the script, however, I get an AttributeError: 'Window' object has no attribute 'game' error.

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. I've setting the command to command=self.master.game to no avail. I appreciate all the help!

The problem is your game function is underneath the 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!

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