【问题标题】:Python Tkinter: If-statement not workingPython Tkinter:如果语句不起作用
【发布时间】:2016-07-31 05:04:32
【问题描述】:
from tkinter import *

def callbackX(button, win, buttonNR):

    print("Pressed button", buttonNR)
    player1.append(buttonNR)
    win.destroy()
    gameScreen()

def gameScreen():

    win = Tk()
    #f = Frame(win)
    if '1' in player1 == 'True':
        b1 = Button(win, text="X", command=lambda: callbackX(b1, win, '1'))
        b1.grid(row=0, column=0)
    if '2' in player1 == 'True':
        b2 = Button(win, text="X", command=lambda: callbackX(b2, win, '2'))
        b2.grid(row=0, column=1)
    if '3' in player1 == 'True':
        b3 = Button(win, text="X", command=lambda: callbackX(b3, win, '3'))
        b3.grid(row=0, column=2)
    if '4' in player1 == 'True':
        b4 = Button(win, text="X", command=lambda: callbackX(b4, win, '4'))
        b4.grid(row=1, column=0)
    if '5' in player1 == 'True':
        b5 = Button(win, text="X", command=lambda: callbackX(b5, win, '5'))
        b5.grid(row=1, column=1)
    if '6' in player1 == 'True':
        b6 = Button(win, text="X", command=lambda: callbackX(b6, win, '6'))
        b6.grid(row=1, column=2)
    if '7' in player1 == 'True':
        b7 = Button(win, text="X", command=lambda: callbackX(b7, win, '7'))
        b7.grid(row=2, column=0)
    if '8' in player1 == 'True':
        b8 = Button(win, text="X", command=lambda: callbackX(b8, win, '8'))
        b8.grid(row=2, column=1)
    if '9' in player1 == 'True':
        b9 = Button(win, text="X", command=lambda: callbackX(b9, win, '9'))
        b9.grid(row=2, column=2)


player1 = []; player2 = []

gameScreen()

该程序似乎无法识别满足的 if 语句标准。这是某种 Tkinter 怪癖吗?如何解决这个问题?

该代码应该为 player1 打开一个井字游戏屏幕,该屏幕会关闭并重新打开窗口,而没有之前按下的按钮。

【问题讨论】:

  • 请描述if '1' in player1 == 'True'应该做什么
  • 当一个按钮被按下时,按钮的编号 (buttonNR) 将被添加到 player1 数组中。目标是在窗口被销毁并重新打开后,它会再次显示游戏画面,而没有之前按下的按钮。

标签: python if-statement button tkinter tic-tac-toe


【解决方案1】:

'True' 是一个字符串,只需去掉引号,因为 True 是一个布尔值。

其实只是简单的使用

如果播放器 1 中有“1”:

在你的情况下没问题。

【讨论】:

  • 引用是我出于绝望而尝试的。然而,即使在删除“真”(当然还有两个等号)之后,它仍然显示一个空窗口。这可能是一个错误还是我可以使用一个内置函数来解决这个问题? (感谢您的回答)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-26
  • 2013-06-20
  • 2013-09-04
  • 1970-01-01
相关资源
最近更新 更多