2011-01-09 12 views
1

私はtkinterで新しいので、私は初心者です..私はプログラマにしようとしていたので、私は助けが必要です。ティックタックトゥ..私は領域として心臓ボタンを使用してゲームを開始したいと思ったときに、 XまたはO(別のGIF画像)... ボタンの画像を切り替えるのに役立つ関数が必要ですが、それをやろうとするとこのエラーが出ます:AttributeError: 'Button'オブジェクトに属性がありません「画像」 これは私に問題を与えてコードの一部..ですPythonでTkinter .. Tic Tac Toeゲーム..ボタンでエラー!

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にはしようとするものののでTkのは、オブジェクト指向されていませんインターフェイスをもう少し「Pythonic」にすると、ちょっと面倒なことがあります。