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
  Update Image on Button Click the_muffin_man 2 318 Nov-05-2024, 01:29 AM
Last Post: menator01
  Tkinter: An image and label are not appearing. emont 7 2,184 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 3,194 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 1,831 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Centering and adding a push button to a grid window, TKinter Edward_ 15 10,379 May-25-2023, 07:37 PM
Last Post: deanhystad
  My Background Image Is Not Appearing (Python Tkinter) HailyMary 2 7,749 Mar-14-2023, 06:13 PM
Last Post: deanhystad
  [Tkinter] tkinter menubar not showing on mac ventura taras 1 2,789 Dec-17-2022, 02:44 PM
Last Post: Yoriz
  simple tkinter question function call not opening image gr3yali3n 5 4,866 Aug-02-2022, 09:13 PM
Last Post: woooee
  [Tkinter] Tkinter don't change the image DQT 2 2,466 Jul-22-2022, 10:26 AM
Last Post: menator01
  Can't get tkinter button to change color based on changes in data dford 4 4,357 Feb-13-2022, 01:57 PM
Last Post: dford

Forum Jump:

User Panel Messages

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