0

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!

4
  • As the error implies, self.master doesn't have game defined as an attribute. Commented May 14, 2020 at 0:44
  • @EricJin I did self.game, and game is defined within class Window(Frame):, as shown in the code snippet. I tried self.master before and it didn't work. Commented May 14, 2020 at 0:54
  • 1
    Just unindent the lines of function game. Commented May 14, 2020 at 1:01
  • @jizhihaoSAMA Thank you, I just realized that just as you put up your comment. Thank you. Commented May 14, 2020 at 1:08

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Just realized my error as you typed your answer. Thank you for the cleanup!
No problem! @SuperHydracid Always happy to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.