Python Forum
tkinter showing image in button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter showing image in button
#1
Hi,

I am trying to show an image in a button. the approch is to read the path from a file and that path is in the last line. I am able to read the path from the file but i can't add it to the button. It gives error it can't find the path.

  # find the last line for the photo
    with open(username1, 'rb') as f:
        f.seek(-2, os.SEEK_END)
        while f.read(1) != b'\n':
            f.seek(-2, os.SEEK_CUR)
        last_line = f.readline().decode()

    photo = PhotoImage(file=last_line)
    photoimage = photo.subsample(1, 2)

    Label(session_start, text="Welcome  " + username1).pack()
    Label(session_start, text="").pack()
    Label(session_start, text="").pack()
    Button(session_start, image=photoimage).pack(pady="150")
Note:
There was a new line break "\n" that I needed to take out. now I can see the space of the photo in the form but no image and yes i did pack()

Attached Files

Thumbnail(s)
   
Reply
#2
Looks like there is a linefeed at the end of your filename.
Reply
#3
(Jun-16-2021, 01:49 AM)deanhystad Wrote: Looks like there is a linefeed at the end of your filename.

I am new to programming with GUI. Do you mean extra line? I removed it and now no errors, just empty image box.
Reply
#4
Try altering to the following code to keep a reference to the image otherwise the image can get garbage collected.
btn = Button(session_start, image=photoimage)
btn.pack(pady="150")
btn.photoimage = photoimage
rwahdan likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] image inside button rwahdan 4 441 Jul-12-2021, 08:49 PM
Last Post: deanhystad
  How to redraw an existing image to a different image TkInter zazas321 6 494 Jul-08-2021, 07:44 PM
Last Post: deanhystad
  [Tkinter] image in label not showing? rwahdan 2 586 Jun-25-2021, 10:27 AM
Last Post: rwahdan
  tkinter button image Nick_tkinter 4 1,092 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  how to resize image in canvas tkinter samuelmv30 2 4,147 Feb-06-2021, 03:35 PM
Last Post: joe_momma
  tkinter python button position problem Nick_tkinter 3 964 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  [Tkinter] Tkinter Textbox only showing one character Xylianth 1 628 Jan-29-2021, 02:59 AM
Last Post: Xylianth
  TKinter restarting the mainloop when button pressed zazas321 7 2,837 Jan-26-2021, 06:38 AM
Last Post: zazas321
  tkinter touchscreen scrolling - button press makes unwanted scrolling nanok66 1 1,003 Dec-28-2020, 10:00 PM
Last Post: nanok66
  [Tkinter] showing return from button on main screen blazejwiecha 4 864 Nov-22-2020, 04:33 PM
Last Post: blazejwiecha

Forum Jump:

User Panel Messages

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