Python-Tkinter, for-loop to create pictured button Python-Tkinter, for-loop to create pictured button tkinter tkinter

Python-Tkinter, for-loop to create pictured button


  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 ttkfrom tkinter import PhotoImageimport osroot = 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()


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).


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