[Tkinter-discuss] Lost images on tabs ttk.notebook

Michael Lange klappnase at web.de
Tue Jan 19 11:42:50 EST 2016


Hi,

On Tue, 19 Jan 2016 15:56:00 +0100
ingo <ingoogni at gmail.com> wrote:

> I'm trying to create a simple tabbed editor and merged two examples 
> found on the web. One of those is the one with the closing images on
> the tabs. In the proces of understanding I somehowe lost the images on
> the tabs but not the fuctionality of clicking on the right position of
> the tabs.
> 
> What is going wrong?

Looks like your image data is being garbage-collected.
This is a very common pitfall in Tkinter.

This is the problematic part of your example code:

> 
>      def _create_panel(self):
> 
>          imgdir = os.path.join(os.path.dirname(__file__), 'img')
>          i1 = PhotoImage("img_close", file=os.path.join(imgdir, 
> 'close.gif'))
>          i2 = PhotoImage("img_closeactive", file=os.path.join(imgdir, 
> 'close_active.gif'))
>          i3 = PhotoImage("img_closepressed", file=os.path.join(imgdir, 
> 'close_pressed.gif'))

Here you fail to keep references to the PhotoImage objects, so immediately
after the _create_panel() function is done, the image data will be
garbage-collected.
To avoid this you can change your code like this:

     def _create_panel(self):

         imgdir = os.path.join(os.path.dirname(__file__), 'img')
         self.i1 = PhotoImage("img_close",\
                   file=os.path.join(imgdir, 'close.gif'))
         self.i2 = PhotoImage("img_closeactive",\
                   file=os.path.join(imgdir, 'close_active.gif'))
         self.i3 = PhotoImage("img_closepressed",\
                   file=os.path.join(imgdir, 'close_pressed.gif'))
         (...)

You may look here for a brief explanation on this topic:
http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm

Best regards

Michael

.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

If I can have honesty, it's easier to overlook mistakes.
		-- Kirk, "Space Seed", stardate 3141.9


More information about the Tkinter-discuss mailing list