'NameError: name 'check' is not defined, but it is defined
It says check isn't defined, but i did, right? I am planing on a Tic Tac Toe game with a GUI so it looks better than just console. Im new to object-oriented programing, if there are good guides out there, please link them. Also i need an idea for my player (spieler), to not use it globaly
import tkinter as tk
root = tk.Tk()
root.title("<Tic_Tac_Toe>")
board = [["","",""],["","",""],["","","",]]
class Commands():
def __init__(self, spieler):
board = [["","",""],["","",""],["","","",]]
self.spieler = spieler
def check(self):
if self.board[self.location[0]][self.location[1]] == "":
self.board[self.location[0]] = self.spieler
print(self.board)
return(self.board)
def change00(self):
location = [0,0]
new_board = check()
self.board = new_board
# same for button 01, 02,....
class Tic_Tac_Toe(tk.Frame):
def __init__(self, main):
tk.Frame.__init__(self, main)
self.commands = Commands("x")
self.setup_gui()
def setup_gui(self):
self.pack(fill="both", expand=True)
self.configure(bg="white")
self.button00 = tk.Button(self, text="1", command = self.commands.change00)
self.button01 = tk.Button(self, text="2")
self.button02 = tk.Button(self, text="3")
self.button10 = tk.Button(self, text="4")
self.button11 = tk.Button(self, text="5")
self.button12 = tk.Button(self, text="6")
self.button20 = tk.Button(self, text="7")
self.button21 = tk.Button(self, text="8")
self.button22 = tk.Button(self, text="9")
self.button00.grid(row=0, column=0, sticky="nsew")
self.button01.grid(row=0, column=1, sticky="nsew")
self.button02.grid(row=0, column=2, sticky="nsew")
self.button10.grid(row=1, column=0, sticky="nsew")
self.button11.grid(row=1, column=1, sticky="nsew")
self.button12.grid(row=1, column=2, sticky="nsew")
self.button20.grid(row=2, column=0, sticky="nsew")
self.button21.grid(row=2, column=1, sticky="nsew")
self.button22.grid(row=2, column=2, sticky="nsew")
self.rowconfigure(index=0, weight=1)
self.rowconfigure(index=1, weight=1)
self.rowconfigure(index=2, weight=1)
self.columnconfigure(index=0, weight=1)
self.columnconfigure(index=1, weight=1)
self.columnconfigure(index=2, weight=1)
run_app = Tic_Tac_Toe(root)
root.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
