2

I'm still quiet new to programming, so maybe my question in pretty easy or even stupid. As said in the title I'm trying to programm a for loop, which creates a pictured button widget for each picuture in a certain folder. This is what I have so far:

import tkinter  
from tkinter import ttk
from tkinter import PhotoImage
import os

root = tkinter.Tk()

list_files = os.listdir(".")

for file in list_files:
    if file.endswith(".gif"):
        drink = PhotoImage(file)
        print(drink)
        b1 = ttk.Button(image=drink, text="Hello", compound="right").pack()
        l1 = ttk.Label(image=drink).pack()


root.mainloop()

Now what I get is two widgets, one label displaying nothing and a button displaying Hello. In the shell it says drink1.gif, which is correct, because that's the only gif file in my standard python folder...

what have I done wrong?

3 Answers 3

3
  1. Use PhotoImage(file='path_to_file') to create image from path.
  2. When PhotoImage object is garbage-collected by Python, the label is cleared. You must save reference to drink object somewhere: l1.image = drink:
    http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm
  3. widget.pack() method return nothing.

import tkinter  
from tkinter import ttk
from tkinter import PhotoImage
import os

root = tkinter.Tk()
list_files = os.listdir(".")
for path in list_files:
    if path.endswith(".gif"):
        drink = PhotoImage(file=path)
        b1 = ttk.Button(root, image=drink, text="Hello", compound="right")
        b1.pack()
        l1 = ttk.Label(root, image=drink)
        l1.image = drink
        l1.pack()

root.mainloop()
Sign up to request clarification or add additional context in comments.

4 Comments

Perfectly working! Thank you! This panel is awesome!! is there a way to stick text with the picture and display it? something like a description? or should I just name the file by the text I want to be displayed? Is there a good explanation about garbage collection? I'm not sure whether I fully got it and I think it's rather important ;)
I don't know common way to attach text to image. You can store description in text file: my_image.gif and my_image.txt.
About garbage collection: if you load 5 images in loop (without saving references to them), only last image will be displayed.
Okay thank you I think I get it now! the idea with .txt is good aswell, thanks guys you were really helpful!!
2

PhotoImage(file) creates an image and gives it the name "drink1.gif", which is returned. If you actually want to load the file into the image, you need PhotoImage(file = file).

Comments

0

I believe you are supposed to run self.pack, according to the Zetcode tutorial.

1 Comment

Thank you, this tutorial was new to me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.