Python Forum
Create image on a Toplevel using tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create image on a Toplevel using tkinter
#1
I'm using tkinter to create a login system. The main window goes as:

my_window=tkinter.Tk()
my_window.title('Welcome To Login System 1.0 !')
my_window.geometry('639x591')

my_canvas=tkinter.Canvas(my_window, height=639, width=591)
welcome_image=ImageTk.PhotoImage(Image.open('welcome2.png'))
image=my_canvas.create_image(0,0, anchor='nw',image=welcome_image)
my_canvas.pack(side='top')
and it works pretty fine.
But when I try to load an image on a Toplevel, the sub-window just keeps blank no matter how I change the pic:

signup_window=tkinter.Toplevel(my_window)
signup_window.geometry('500x400')
signup_window.title('Sign up now')

signup_canvas = tkinter.Canvas(signup_window, height=500, width=400)
signup_image = ImageTk.PhotoImage(Image.open('signup_bg.png'))
image2 = signup_canvas.create_image(0, 0, anchor='nw', image=signup_image)
signup_canvas.pack(side='top')
Don't know what goes wrong. I use the PIL module to load the image.
Reply
#2
Instead of using PIL I used tkinter.PhotoImage(file='imagefile') . When I ran your code I got two windows with two images. Maybe your signup_bg.png image file is bad.
Reply
#3
(Jun-03-2020, 12:00 PM)deanhystad Wrote: Instead of using PIL I used tkinter.PhotoImage(file='imagefile') . When I ran your code I got two windows with two images. Maybe your signup_bg.png image file is bad.
I fixed it by adding one line:
global signup_image
Guess it's because of the garbage collection force or something.
Reply
#4
I don't think garbage collection is the problem here. You get the garbage collection problem when you create an image in a function or method and use a local variable to reference the image. When the function exits all the local variables are deleted, there is nothing referencing the image, and it becomes available for garbage collection. Your code is not doing that. Your variables are not getting deleted, you image variable continues to reference your image, and your image should not get garbage collected.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter weird image displaying behaviour pyDream 2 1,016 Oct-18-2025, 11:15 AM
Last Post: deanhystad
  [Tkinter] Help create scrollbar in chatbot with tkinter on python fenec10 6 7,285 Aug-28-2025, 10:38 AM
Last Post: fazanbabar82
  [Tkinter] passing value from tkinter entry to create xml document iqbshaik 7 3,569 Aug-28-2025, 10:32 AM
Last Post: david568
  Tkinter: An image and label are not appearing. emont 7 5,967 Jul-28-2025, 09:40 AM
Last Post: enablespindle
  Trying to access a method from my Toplevel Form avmvldr 1 1,015 Jul-09-2025, 11:59 PM
Last Post: deanhystad
  Wont create Image from function the_muffin_man 10 4,934 Jul-10-2024, 06:20 PM
Last Post: AdamHensley
  pass a variable between tkinter and toplevel windows janeik 9 9,010 Oct-05-2023, 04:22 AM
Last Post: janeik
  My Background Image Is Not Appearing (Python Tkinter) HailyMary 2 13,907 Mar-14-2023, 06:13 PM
Last Post: deanhystad
  simple tkinter question function call not opening image gr3yali3n 5 8,771 Aug-02-2022, 09:13 PM
Last Post: woooee
  [Tkinter] Tkinter don't change the image DQT 2 3,862 Jul-22-2022, 10:26 AM
Last Post: menator01

Forum Jump:

User Panel Messages

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