如何修改重置按钮以使其正常工作?

问题描述 投票:-1回答:1

我正在创建井字游戏。在此,我创建了一个重置​​按钮,该按钮引用了重置功能。在该功能中,我重置了棋盘和stop_game的值。当我单击游戏中间的重置按钮时,它可以正常工作。但是,当某人获胜,然后我单击“重置”按钮时,它只是重置棋盘,但是在按棋盘按钮时,它什么也没做。请帮助我解决这个问题。

from tkinter import *
from copy import deepcopy
import random


game = Tk()
game.title("TIC TAC TOE")
game.geometry("450x500")
#game.configure(bg = '#b3b3b3')

player = 'X'
stop_game = False

def callback(r, c):
    global player

    if player == 'X' and states[r][c] == 0 and stop_game == False:
        board[r][c].configure(text = 'X', fg = '#f64c72')
        states[r][c] = 'X'
        player = 'O'

    if player == 'O' and states[r][c] == 0 and stop_game == False:
        board[r][c].configure(text = 'O', fg = '#f64c72')
        states[r][c] = 'O'
        player = 'X'

    checkWinner()


def checkWinner():
    global stop_game

    win_color = '#3b5b5b'
    for i in range(3):
        if states[i][0] == states[i][1] == states[i][2] != 0:
            board[i][0].config(bg = win_color)
            board[i][1].config(bg = win_color)
            board[i][2].config(bg = win_color)
            stop_game = True

    for i in range(3):
        if states[0][i] == states[1][i] == states[2][i] != 0:
            board[0][i].config(bg = win_color)
            board[1][i].config(bg = win_color)
            board[2][i].config(bg = win_color)
            stop_game = True

    if states[0][0] == states[1][1] == states[2][2] != 0:
        board[0][0].configure(bg = win_color)
        board[1][1].configure(bg = win_color)
        board[2][2].configure(bg = win_color)
        stop_game = True

    if states[2][0] == states[1][1] == states[0][2] != 0:
        board[2][0].configure(bg = win_color)
        board[1][1].configure(bg = win_color)
        board[0][2].configure(bg = win_color)
        stop_game = True


f = [[0, 0, 0],
    [0, 0, 0],
    [0, 0, 0]]
board = [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]
states = [[0, 0, 0],
         [0, 0, 0],
         [0, 0, 0]]

def reset():
    for i in range(3):
        for j in range(3):
            board[i][j].configure(text = ' ', fg = '#ffda30', bg = "#242582")
            states[i][j] = 0
    stop_game = False



for i in range(3):
    for j in range(3):
        f[i][j] = Frame(game, width = 150, height = 150)
        f[i][j].propagate(False)
        f[i][j].grid(row = i, column = j, sticky = "nsew", padx = 1, pady = 1)
        board[i][j] = Button(f[i][j], font = ("Helvatica", 70), bg = "#242582", fg = "#ffda30",
                        command = lambda    r = i, c = j: callback(r, c))
        board[i][j].pack(expand = True, fill = BOTH)




reset_game = Button(text = "Reset the game!", font = ("Helvatica", 13), bg = "#ffda30", fg = "#000000", 
               command = lambda    :reset())
reset_game.grid(row = 3, column = 0, columnspan = 2, sticky = 'nsew')




quit_game = Button(text = "Quit game!", font = ("Helvatica", 13), bg = "#ffda30", fg = "red", 
                   command = lambda    :game.destroy())
quit_game.grid(row = 3, column = 2, sticky = 'nsew')


game.resizable(False, False)
game.mainloop()
python tkinter reset tic-tac-toe
1个回答
1
投票

您需要在stop_game函数内部将Reset()声明为全局变量。

© www.soinside.com 2019 - 2024. All rights reserved.