2016-07-31 105 views
0
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怪癖?這怎麼解決?Python Tkinter:如果語句不起作用

代碼應該打開一個井字棋遊戲畫面,對於player1來說,它關閉並重新打開窗口,而沒有按下之前按下的按鈕。

+0

請描述'player1 =='True''中的if'1'應該做什麼 –

+0

當按鈕被按下時,按鈕的數字(buttonNR)將被添加到player1數組中。目標是在窗口被銷燬並重新打開之後,它將再次顯示遊戲屏幕,而沒有先前按下的按鈕。 –

回答

1

'True'是一個字符串,只是刪除引號,因爲True是一個bool。

其實,只要簡單地使用

如果PLAYER1 '1':

你的情況確定。

+0

引號是我嘗試過純粹的絕望。然而,即使刪除了「真」(當然這兩個等號),它仍然顯示一個空窗口。它可能是一個錯誤,或者有可能是我可以用來解決問題的內置函數? (謝謝你的回答) –