Im new at tkinter so im a begginer.. I need help because i was trying to programma Tic Tac Toe .. i wanted to start the game with a heart button as zone the when the player click on that the image has to change in X or O (another gif image)…
So i need a function to help me to switch the image in the button but when i tryed to do it it gave me this error: AttributeError: ‘Button’ object has no attribute ‘image’
this is the part of the code that gives me the problem..
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()
Try changing it with:
To find out what image it already has, you’ll need to compare:
(Or keep track of its state separately)
Tk isn’t object oriented, so although Tkinter tries to make the interface a bit more “Pythonic”, it can be a bit awkward.