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

时间:2016-01-07 20:08:39

标签: python variables boolean

我正在制作一个tic tac toe游戏,我需要将图像更改为“x”或“o”,具体取决于轮到谁了。我有一个变量playerXturn是真的,当它为真时程序将显示“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() 

2 个答案:

答案 0 :(得分:1)

// property style @property (nonatomic, strong, null_resettable) NSString *name; // pointer style + (NSArray<NSView *> * _Nullable)interestingObjectsForKey:(NSString * _Nonnull)key; // these two are equivalent! @property (nonatomic, strong, nullable) NSString *identifier1; @property (nonatomic, strong) NSString * _Nullable identifier2; 运算符反转布尔值。

puts "\e[0;31m your text here \e[0m\n"

答案 1 :(得分:-2)

您可以使用not运算符切换变量。

a_boolean = True
print a_boolean
>>> True

a_boolean = not a_boolean
print a_boolean
>>> False