Python Forum
Problem about image and button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem about image and button
#1
Hi,
I am doing a project on Tkinter, and I am facing 2 problems :

1- When I define a class and insert an image, it doesn't work. But, when I just define the frame, not with class, it works very well. Btw, it isn't a problem with where the image is saved.

2 - And here is a question now. I have a menu page and I want to make that if one button is clicked, it shows a new widget several frames later. I tried something that you can see in my code but it isn't working.

It would be awesome if you can help me.

Thanks a lot and have a nice day

Here is the code :
For question 1 :
The images aren't working but the text and button is working :
class Brittachoix(Frame):
    def __init__(self,master):
        Frame.__init__(self,master)
        tableau = PhotoImage(file="tableau.png").zoom(4) 
        Canvasbritta = Canvas(self, width=largeur, height=hauteur, bg="#FFFFFF")
        TABLEAU = Canvasbritta.create_image(540,340, image=tableau)
        txt = Canvasbritta.create_text(540,570,text="Bonjour", font="Arial 28 italic", fill="red")
        Button(self, text='Femme suivante', command=lambda: master.switch_frame(Shirleychoix)).pack()
        Canvasbritta.pack()
        self.pack()
But when I put it out of the class, it is working !

BittaPerry = Frame(bvfenetre, bg="#FFFFFF")
largeur = 1080 
hauteur = 680 
tableau = PhotoImage(file="tableau.png").zoom(4) 
Canvasbritta = Canvas(BittaPerry, width=largeur, height=hauteur, bg="#FFFFFF")
TABLEAU = Canvasbritta.create_image(540,340, image=tableau)
txt = Canvasbritta.create_text(540,570,text="Britta Perry", font="Arial 28 italic", fill="red")
Canvasbritta.pack()
BittaPerry.pack(expand=YES)
2 - That is what I tried to do but it isn't working :

class Anniechoix(Frame):
        def __init__(self,master):
            Frame.__init__(self,master)
            Canvannie = Canvas(self, width=largeur, height=hauteur, bg="#FFFFFF")
            # TABLEAU = Canvannie.create_image(540,340, image=tableau)
            txt = Canvannie.create_text(540,570,text=nom[6], font="Arial 28 italic", fill="red")
            if i==1:
                Suivant = Button(self, text='Personnage suivant', command=lambda:master.switch_frame(Jeffchoix))
                Suivant.place(x=960,y=600)
            self.pack()
And previously, in the functino to switch to Anniechoix, I said destroy the previous, open the new one and i = 1 :

class SampleApp(Tk):
    def __init__(self):
        Tk.__init__(self)
        self._frame = None
        self.switch_frame(Bienvenue)
        
    def switch_frame(self, frame_class):
        new_frame = frame_class(self)
        if self._frame is not None : 
            self._frame.destroy()
        self._frame = new_frame
        self._frame.pack()
        i = 1
Reply
#2
I don't know if this is the problem, but you did not do this in your class.
BittaPerry.pack(expand=YES)  # is self.pack() in the class Brittachoix
Reply
#3
Thanks for your answer but this wasn't the problem.

And btw, I solved the question 2, but I still have no idea of the problem for the first question
Reply
#4
You need to keep the image variable. I think tableau, the image, was destroyed when the local variable tableau no longer exists. I tested the code below and it works for me
class Brittachoix(Frame):
    def __init__(self, root):
        super().__init__(root)
        self.tableau = PhotoImage(file="image.png").zoom(4) 
        Canvasbritta = Canvas(self, width=largeur, height=hauteur, bg="#FFFFFF")
        TABLEAU = Canvasbritta.create_image(540,340, image=self.tableau)
        txt = Canvasbritta.create_text(540,570,text="Bonjour", font="Arial 28 italic", fill="red")
        Canvasbritta.pack()
        self.pack()
Reply
#5
I was not able to get the tk.PhotoImage to work in a class. I went the PIL route. Couple extra lines but worked in the class.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#6
(Apr-27-2020, 09:31 AM)deanhystad Wrote: You need to keep the image variable. I think tableau, the image, was destroyed when the local variable tableau no longer exists. I tested the code below and it works for me
class Brittachoix(Frame):
    def __init__(self, root):
        super().__init__(root)
        self.tableau = PhotoImage(file="image.png").zoom(4) 
        Canvasbritta = Canvas(self, width=largeur, height=hauteur, bg="#FFFFFF")
        TABLEAU = Canvasbritta.create_image(540,340, image=self.tableau)
        txt = Canvasbritta.create_text(540,570,text="Bonjour", font="Arial 28 italic", fill="red")
        Canvasbritta.pack()
        self.pack()

Thanks a lot, it is indeed working !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] canvas image problem DPaul 4 6,257 Nov-24-2021, 07:06 AM
Last Post: DPaul
  [Tkinter] Why does the image for the button not appear? finndude 4 2,047 Oct-21-2021, 06:41 PM
Last Post: deanhystad
  problem with radio button crook79 3 3,627 Aug-12-2021, 02:30 PM
Last Post: deanhystad
  [Tkinter] image inside button rwahdan 4 6,886 Jul-12-2021, 08:49 PM
Last Post: deanhystad
  tkinter showing image in button rwahdan 3 5,517 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter button image Nick_tkinter 4 3,956 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  tkinter python button position problem Nick_tkinter 3 3,482 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  [Tkinter] Button click problem using OOP JohnB 5 3,521 Oct-21-2020, 12:43 PM
Last Post: JohnB
  Problem with Submit button Tkinter Reldaing 2 3,603 Jan-05-2020, 01:58 AM
Last Post: balenaucigasa
  [PyQt] Problem how to click a button inside a group box? mart79 2 3,375 Aug-05-2019, 01:21 PM
Last Post: mart79

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020