为什么我的功能不影响tkinter中的按钮? [英] Why is my function not affecting a button in tkinter?

查看:66
本文介绍了为什么我的功能不影响tkinter中的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用tkinter在python中制作Tic Tac Toe,但是我遇到了一个问题.我试图做到这一点,所以当您单击一个正方形时,它将显示该正方形上任何玩家的符号.但是,使用我当前的功能 playerCheck ,当我按下它们时什么也没有发生.我不知道为什么会这样,所以我将不胜感激.

I'm trying to make Tic Tac Toe in python using tkinter, but I'm running into an issue. I'm trying to make it so when you click on one of the squares, it displays whichever player's symbol on that square. However, with my current function playerCheck, nothing happens when I press them. I can't figure out why this is happening, so I would appreciate any help.

请记住,我还没完蛋.

import tkinter as tk

Player1 = "X"
Player2 = "O"
turn = None
playerNumber = None

def playerCheck(function):
    if turn == Player1:
        playerNumber = Player1
        function("white", playerNumber)

    elif turn == Player2:
        playerNumber = Player2
        function("white", playerNumber)

def topLeft(color, player):
    topL = tk.Button(text=player, fg="black", bg=color, height=5, width=10, command=lambda: playerCheck(topL)).grid(row=0,column=0)

def topMiddle(color, player):
    topM = tk.Button(text=player, fg="black", bg=color, height=5, width=10, command=lambda: playerCheck(topMiddle)).grid(row=0,column=1)

def topRight(color, player):
    topR = tk.Button(text=player, fg="black", bg=color, height=5, width=10, command=lambda: playerCheck(topRight)).grid(row=0,column=2)

def middleLeft(color, player):
    midL = tk.Button(text=player, fg="black", bg=color, height=5, width=10, command=lambda: playerCheck(middleLeft)).grid(row=1,column=0)

def middleMiddle(color, player):
    midM = tk.Button(text=player, fg="black", bg=color, height=5, width=10, command=lambda: playerCheck(middleMiddle)).grid(row=1,column=1)

def middleRight(color, player):
    midR = tk.Button(text=player, fg="black", bg=color, height=5, width=10, command=lambda: playerCheck(middleRight)).grid(row=1,column=2)

def bottomLeft(color, player):
    botL = tk.Button(text=player, fg="black", bg=color, height=5, width=10, command=lambda: playerCheck(bottomLeft)).grid(row=2,column=0)

def bottomMiddle(color, player):
    botM = tk.Button(text=player, fg="black", bg=color, height=5, width=10, command=lambda: playerCheck(bottomMiddle)).grid(row=2,column=1)

def bottomRight(color, player):
    botR = tk.Button(text=player, fg="black", bg=color, height=5, width=10, command=lambda: playerCheck(bottomRight)).grid(row=2,column=2)

def gameStart():
    topLeft("white", "")
    topMiddle("white", "")
    topRight("white", "")
    middleLeft("white", "")
    middleMiddle("white", "")
    middleRight("white", "")
    bottomLeft("white", "")
    bottomMiddle("white", "")
    bottomRight("white", "")
    turn = Player1

def Main():
    a = tk.Tk()
    a.title("Tick Tack Toe")
    a.geometry("250x250")
    gameStart()

    a.mainloop()
Main()

推荐答案

playerCheck()的参数是一个按钮,而不是一个函数.您应该更改按钮的文本和颜色,而不要尝试调用它.

The argument to playerCheck() is a button, not a function. You should change the button's text and color, not try to call it.

def playerCheck(button):
    global turn
    button['text'] = turn
    button['fg'] = "white"
    if turn == Player1:
        turn = Player2
    else:
        turn = Player1

这篇关于为什么我的功能不影响tkinter中的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆