Python-Tkinter, for-loop to create pictured button
- Use
PhotoImage(file='path_to_file')
to create image from path. - When
PhotoImage
object is garbage-collected by Python, the label is cleared. You must save reference todrink
object somewhere:l1.image = drink
:
http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm 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()