Ошибка в функции пайтон ткинтер
Господа, имеется у меня такой код для крестиков ноликов. При нажатии кнопки "Заново" выдает ошибку:
Traceback (most recent call last): File
"C:\Users\User\AppData\Local\Programs\Thonny\lib\tkinter\__init__.py",
line 1921, in __call__
return self.func(*args) File "C:\Users\User\Desktop\НЕ ЗАХОДИТЬ\крестики нолики.py", line 148, in restart
restart_btn.destroy() NameError: name 'restart_btn' is not defined
Даже global 'restart_btn' не помог, хотя обычно помогал. Вот код:
import tkinter as tk
row1 = [' ', ' ', ' ']
row2 = [' ', ' ', ' ']
row3 = [' ', ' ', ' ']
win = tk.Tk()
win.title('крестики-нолики')
win.geometry('300x435')
isnextx = False
def check_win_for_x():
if row1[0] == row1[1] == row1[2] == 'x' or row2[0] == row2[1] == row2[2] == 'x' or row3[0] == row3[1] == row3[2] == 'x' or row1[0] == row2[0] == row3[0] == 'x' or row1[1] == row2[1] == row3[1] == 'x' or row1[2] == row2[2] == row3[2] == 'x' or row1[0] == row2[1] == row3[2] == 'x' or row3[0] == row2[1] == row1[2] == 'x':
return True
else:
if row1[0] == row1[1] == row1[2] == 'o' or row2[0] == row2[1] == row2[2] == 'o' or row3[0] == row3[1] == row3[2] == 'o' or row1[0] == row2[0] == row3[0] == 'o' or row1[1] == row2[1] == row3[1] == 'o' or row1[2] == row2[2] == row3[2] == 'o' or row1[0] == row2[1] == row3[2] == 'o' or row3[0] == row2[1] == row1[2] == 'o':
return False
else:
return None
def end_game():
if check_win_for_x() == True:
label_win = tk.Label(win, text='победа крестиков', font = ('Arial', 30)).place(x = 0, y = 300)
restart_btn = tk.Button(text = 'заново', font = ('Arial', 27), command = restart).place(x = 0, y = 360)
else:
if check_win_for_x() == False:
label_lose = tk.Label(win, text='победа ноликов', font = ('Arial', 30)).place(x = 0, y = 300)
restart_btn = tk.Button(text = 'заново', font = ('Arial', 27), command = restart).place(x = 0, y = 360)
def changetextbtn1():
global isnextx
if isnextx:
btn1.config(text='❌', state='disabled', font = ('Arial', 45))
isnextx = False
row1[0] = 'x'
else:
btn1.config(text='⭕', state='disabled', font = ('Arial', 45))
isnextx = True
row1[0] = 'o'
end_game()
def changetextbtn2():
global isnextx
if isnextx:
btn2.config(text='x', state='disabled', font = ('Arial', 45))
isnextx = False
row1[1] = 'x'
else:
btn2.config(text='o', state='disabled', font = ('Arial', 45))
isnextx = True
row1[1] = 'o'
end_game()
def changetextbtn3():
global isnextx
if isnextx:
btn3.config(text='x', state='disabled', font = ('Arial', 45))
isnextx = False
row1[2] = 'x'
else:
btn3.config(text='o', state='disabled', font = ('Arial', 45))
isnextx = True
row1[2] = 'o'
end_game()
def changetextbtn4():
global isnextx
if isnextx:
btn4.config(text='x', state='disabled', font = ('Arial', 45))
isnextx = False
row2[0] = 'x'
else:
btn4.config(text='o', state='disabled', font = ('Arial', 45))
isnextx = True
row2[0] = 'o'
end_game()
def changetextbtn5():
global isnextx
if isnextx:
btn5.config(text='x', state='disabled', font = ('Arial', 45))
isnextx = False
row2[1] = 'x'
else:
btn5.config(text='o', state='disabled', font = ('Arial', 45))
isnextx = True
row2[1] = 'o'
end_game()
def changetextbtn6():
global isnextx
if isnextx:
btn6.config(text='x', state='disabled', font = ('Arial', 45))
isnextx = False
row2[2] = 'x'
else:
btn6.config(text='o', state='disabled', font = ('Arial', 45))
isnextx = True
row2[2] = 'o'
end_game()
def changetextbtn7():
global isnextx
if isnextx:
btn7.config(text='x', state='disabled', font = ('Arial', 45))
isnextx = False
row3[0] = 'x'
else:
btn7.config(text='o', state='disabled', font = ('Arial', 45))
isnextx = True
row3[0] = 'o'
end_game()
def changetextbtn8():
global isnextx
if isnextx:
btn8.config(text='x', state='disabled', font = ('Arial', 45))
isnextx = False
row3[1] = 'x'
else:
btn8.config(text='o', state='disabled', font = ('Arial', 45))
isnextx = True
row3[1] = 'o'
end_game()
def changetextbtn9():
global isnextx
if isnextx:
btn9.config(text='x', state='disabled', font = ('Arial', 45))
isnextx = False
row3[2] = 'x'
else:
btn9.config(text='o', state='disabled', font = ('Arial', 45))
isnextx = True
row3[2] = 'o'
end_game()
def restart():
global btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9
global restart_btn
btn1.destroy()
btn2.destroy()
btn3.destroy()
btn4.destroy()
btn5.destroy()
btn6.destroy()
btn7.destroy()
btn8.destroy()
btn9.destroy()
restart_btn.destroy()
btn1 = tk.Button(win, text='', command=changetextbtn1)
btn1.place(width=100, height=100, x=0, y=40)
btn2 = tk.Button(win, text='', command=changetextbtn2)
btn2.place(width=100, height=100, x=100, y=40)
btn3 = tk.Button(win, text='', command=changetextbtn3)
btn3.place(width=100, height=100, x=200, y=40)
btn4 = tk.Button(win, text='', command=changetextbtn4)
btn4.place(width=100, height=100, x=0, y=140)
btn5 = tk.Button(win, text='', command=changetextbtn5)
btn5.place(width=100, height=100, x=100, y=140)
btn6 = tk.Button(win, text='', command=changetextbtn6)
btn6.place(width=100, height=100, x=200, y=140)
btn7 = tk.Button(win, text='', command=changetextbtn7)
btn7.place(width=100, height=100, x=0, y=240)
btn8 = tk.Button(win, text='', command=changetextbtn8)
btn8.place(width=100, height=100, x=100, y=240)
btn9 = tk.Button(win, text='', command=changetextbtn9)
btn9.place(width=100, height=100, x=200, y=240)
row1 = [' ', ' ', ' ']
row2 = [' ', ' ', ' ']
row3 = [' ', ' ', ' ']
def place_buttons():
global btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9
btn1 = tk.Button(win, text='', command=changetextbtn1)
btn1.place(width=100, height=100, x=0, y=0)
btn2 = tk.Button(win, text='', command=changetextbtn2)
btn2.place(width=100, height=100, x=100, y=0)
btn3 = tk.Button(win, text='', command=changetextbtn3)
btn3.place(width=100, height=100, x=200, y=0)
btn4 = tk.Button(win, text='', command=changetextbtn4)
btn4.place(width=100, height=100, x=0, y=100)
btn5 = tk.Button(win, text='', command=changetextbtn5)
btn5.place(width=100, height=100, x=100, y=100)
btn6 = tk.Button(win, text='', command=changetextbtn6)
btn6.place(width=100, height=100, x=200, y=100)
btn7 = tk.Button(win, text='', command=changetextbtn7)
btn7.place(width=100, height=100, x=0, y=200)
btn8 = tk.Button(win, text='', command=changetextbtn8)
btn8.place(width=100, height=100, x=100, y=200)
btn9 = tk.Button(win, text='', command=changetextbtn9)
btn9.place(width=100, height=100, x=200, y=200)
place_buttons()
win.mainloop()