Python 如何将true的变量更改为false并返回true?

Python 如何将true的变量更改为false并返回true?,python,variables,boolean,Python,Variables,Boolean,我正在做一个tic tac toe游戏,我需要根据轮到谁把图像改成x或o。我有一个变量playerxtrn为真,当它为真时,程序将显示一个x。如何将其更改为false,以便显示o,然后将其更改为true,以便再次显示x from tkinter import* global clickable playerXturn = True def buttonClicked(c) : if playerXturn == True : buttonList[c]["image"

我正在做一个tic tac toe游戏,我需要根据轮到谁把图像改成x或o。我有一个变量playerxtrn为真,当它为真时,程序将显示一个x。如何将其更改为false,以便显示o,然后将其更改为true,以便再次显示x

from tkinter import*
global clickable
playerXturn = True

def buttonClicked(c) : 
    if playerXturn == True :
        buttonList[c]["image"] = picX
    elif clickable[c] == "" : 
        buttonList[c]["image"] = picO


window = Tk()
window.title("Tic Tac Toe")
window.configure(background = "black")
window.geometry("400x400")

picX = PhotoImage (file = "x.gif") 
picO = PhotoImage (file = "o.gif")
picBlank = PhotoImage (file = "sw.gif") 

button1 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(0))    
button1.grid (row = 0, column = 0) 
button2 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(1))    
button2.grid (row = 0, column = 1) 
button3 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(2)) 
button3.grid (row = 0, column = 2) 
button4 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(3))  
button4.grid (row = 1, column = 0) 
button5 = Button (window, text = "", image = picBlank,  command = lambda: buttonClicked(4)) 
button5.grid (row = 1, column = 1)
button6 = Button (window, text = "", image = picBlank,  command = lambda: buttonClicked(5))  
button6.grid (row= 1, column = 2) 
button7 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(6)) 
button7.grid (row = 2, column = 0) 
button8 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(7))  
button8.grid (row = 2, column = 1) 
button9 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(8)) 
button9.grid (row = 2, column = 2) 

buttonList = [button1, button2, button3, button4, button5, button6, button7, button8, button9]
clickable = ["", "", "", "", "", "", "", "", ""]

window.mainloop() 

可以使用运算符切换变量

a_boolean = True
print a_boolean
>>> True

a_boolean = not a_boolean
print a_boolean
>>> False
not运算符反转布尔值

def buttonClicked(c):
    global playerXturn
    if playerXturn:
        buttonList[c]["image"] = picX
    elif clickable[c] == "" : 
        buttonList[c]["image"] = picO
    playerXturn = not playerXturn

但是我怎样才能让它变回真的呢?只要再做一次同样的事情。@ray,再做一次。它将True更改为False,将False更改为True。它表示在赋值之前引用了局部变量“playerxtorn”。在将该变量添加到函数之前,您没有收到该错误吗?