M
M
Maxim0452020-01-11 14:35:40
Python
Maxim045, 2020-01-11 14:35:40

Why doesn't an image appear on a button created with tkinter in Python?

The create_albums function should iterate through all the image locations that are in the sqlite database, creating buttons with those images. But this does not happen, the buttons remain empty. I don't understand what is wrong?

from tkinter import *
from PIL import ImageTk
import sqlite3

root = Tk()
root.minsize(300,300)

con = sqlite3.connect('tagsdatabase.db')
cur = con.cursor()

def create_albums():
    albums = []

    for album_f in cur.execute('SELECT Cover_Path FROM covers_db'):
        albums.append(album_f[0])

    for album_s in albums:

        image = ImageTk.PhotoImage(file = str(album_s))

        album_button = Button(root,
                              image = image,
                              width = 120, height = 120,
                              command = lambda: print('click'))
        album_button.pack()

create_albums()

root.mainloop()

5e19b29065a12981444385.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SuckMyPython, 2020-01-11
@Maxim045

Add album_button.image = image before placing the button

***
    for album_s in albums:
        image = ImageTk.PhotoImage(file = str(album_s))
        album_button = Button(root,
                              image = image,
                              width = 120, height = 120,
                              command = lambda: print('click'))
        album_button.image = image
        album_button.pack()
create_albums()
***

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question