Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么不是';我的抽签方法不起作用吗?_Python_Image_If Statement_Button - Fatal编程技术网

Python 为什么不是';我的抽签方法不起作用吗?

Python 为什么不是';我的抽签方法不起作用吗?,python,image,if-statement,button,Python,Image,If Statement,Button,我看了一段视频,一个家伙在玩他的游戏,效果很好。我正在用同样的方法检查赢家,但什么都没有发生。我用图像代替字母,但这真的有区别吗。有什么建议吗 from tkinter import* #Window stuff window = Tk() window.title("Tic Tac Toe") window.configure(background = "black") window.geometry("400x400") #Variables global clickable pl

我看了一段视频,一个家伙在玩他的游戏,效果很好。我正在用同样的方法检查赢家,但什么都没有发生。我用图像代替字母,但这真的有区别吗。有什么建议吗

from tkinter import*

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

#Variables
global clickable
playerXturn = True
ticker = 0 

#Display X or O
def buttonClicked(c) : 
    global playerXturn
    if playerXturn == True :
        buttonList[c]["image"] = picX
        buttonList[c]["state"] = DISABLED
        playerXturn = False
        labelTurn ["text"] = "O's turn"

    elif clickable[c] == "" : 
        buttonList[c]["image"] = picO
        buttonList[c]["state"] = DISABLED
        playerXturn = True
        labelTurn ["text"] = "X's turn"
#Info for user
    global ticker
    ticker += 1
    if ticker == 9 :
        labelNewGame["text"] = "GAME OVER"
        labelNewGame["bg"] = "red" 
        labelTurn["text"] = ""
        labelTurn["bg"] = "black"

#Three in a row
    elif (button1["image"] == picX and button2["image"] == picX and button3["image"] == picX or
          button4["image"] == picX and button5["image"] == picX and button6["image"] == picX or
          button7["image"] == picX and button8["image"] == picX and button9["image"] == picX or
          button1["image"] == picX and button4["image"] == picX and button7["image"] == picX or
          button2["image"] == picX and button5["image"] == picX and button8["image"] == picX or
          button3["image"] == picX and button6["image"] == picX and button9["image"] == picX or
          button1["image"] == picX and button5["image"] == picX and button9["image"] == picX or
          button3["image"] == picX and button5["image"] == picX and button7["image"] == picX) :
        print ("X wins")

    elif (button1["image"] == picO and button2["image"] == picO and button3["image"] == picO or
          button4["image"] == picO and button5["image"] == picO and button6["image"] == picO or
          button7["image"] == picO and button8["image"] == picO and button9["image"] == picO or
          button1["image"] == picO and button4["image"] == picO and button7["image"] == picO or
          button2["image"] == picO and button5["image"] == picO and button8["image"] == picO or
          button3["image"] == picO and button6["image"] == picO and button9["image"] == picO or
          button1["image"] == picO and button5["image"] == picO and button9["image"] == picO or
          button3["image"] == picO and button5["image"] == picO and button7["image"] == picO) :
        print ("O wins") 


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

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

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

#Extra labels and buttons
labelMenu = Label (window, text = "Menu and Info", height = 2, width = 24, bg = "yellow") 
labelMenu.grid (row = 4, column = 4) 
labelTurn = Label (window, text = "X goes first", height = 2, width = 10)  
labelTurn.grid (row = 6, column = 4)
labelNewGame = Label (window, text = "Continue Playing", height = 2, width = 14,)     
labelNewGame.grid (row = 5, column = 4)

window.mainloop() 

看起来你的问题是当你

buttonList[c]["image"] = picX
buttonList[c][“image”]采用的真实值是字符串“pyimage1”,而picX是tkinter.PhotoImage类型。有鉴于此,它应该解决你的问题,如果不是比较的东西,如

button1["image"] == picX
你应该这样做

 buttonN["image"] == str(picX)

也可以对picO执行同样的操作。

您可以扩展Button类,添加一个属性,指示它处于什么状态(“X”、“O”或“blank”),这将简化检查。