2011-01-09 215 views
1

即時通訊新的tkinter所以我是一個begginer ..我需要幫助,因爲我試圖編程井字遊戲..我想用一個心臟按鈕作爲區域啓動遊戲當玩家點擊圖像必須改變在X或O(另一個gif圖像)... 所以我需要一個函數來幫助我切換按鈕中的圖像,但是當我嘗試這樣做時,它給了我這個錯誤:AttributeError:'Button'對象沒有屬性「圖像」 這是給我的問題的代碼的一部分。這Python中的Tkinter ..井字遊戲..錯誤與按鈕!

class Application(object): 

    def __init__(self,fnt2): 
     self.photo = PhotoImage(file="game.gif") 
     self.lab1 = Label(fnt2, text="WELCOME to the GAME") 
     self.lab1.image = self.photo 
     self.lab1['background']="#BD5151" 
     self.lab1['foreground']="#651268" 
     self.lab1.image = self.photo 
     self.lab1.pack() 

     self.lab2= Label(image=self.photo) 
     self.lab2.image= self.photo 
     self.lab2['background']="#BD5151" 
     self.lab2.pack() 
     self.imm0 = PhotoImage(file="start.gif") 
     self.imm1 = PhotoImage(file="bianco.gif") 
     self.imm2 = PhotoImage(file="ics.gif") 


     self.Ent = Button(fnt2, text="Click To Enter The GAME") 
     self.Ent['relief']="groove" 
     self.Ent['command']=self.Ent_Click 
     self.Ent.pack() 

    def Changepic_1(self): 
     imm0 = PhotoImage(file="start.gif") 
     imm1 = PhotoImage(file="bianco.gif") 
     imm2 = PhotoImage(file="ics.gif") 
     if self.Play.image == self.imm0: 
      print('ciao') 


    def Ent_Click(self): 
     fnt2 = Tk() 
     fnt2.title("play it!") 
     fnt2.resizable(False,False) 
     for r in range(3): 
      for c in range(3): 
       self.Play = Button(image = self.imm0, command=self.Changepic_1) 
       self.Play.grid(row=r,column=c) 
     fnt2.mainloop() 

回答

2

嘗試與改變它:

button.config(image=...) 

要找出它已經有圖片,您需要比較:

button.cget("image") == image.name 

(或保持其狀態的跟蹤單獨)

傳統知識不是面向對象的,所以雖然Tkinter的嘗試使界面更加「Pythonic」,它可能有點尷尬。