I am relative new to python and was hoping to create a button with a icon in the side dynamically using tkinter, to do that I have a list dictionary with all the button preloaded with the information I need and using a for loop to create and store the button.
butlist_sidebar_ld = [{"name":"Reports" , "icon":Path(mainpath+imagepath+ "\icon_reports.png") , "link":placeholder},
{"name":"Stocks" , "icon":Path(mainpath+imagepath+"\icon_stocks.png") , "link":placeholder},
{"name":"Core Setup" , "icon":Path(mainpath+imagepath+"\icon_settings.png") , "link":placeholder}]
then I am using a for loop to create a button and storing it in a dictionary. The problem is that when I try to use add the image to the button within the for loop all the button breaks (not clickable) and the image is not displayed except for the last one.
The code I have tried to use so far
for button_info in butlist_sidebar_ld:
# Create a photoimage object
photo = PhotoImage(file = button_info.get("icon"))
ph_image = photo.subsample(35,35)
#Create the button
button_list[button_info.get("name")] = Button(root,text = button_info.get("name"), compound=LEFT , command= button_info.get("link") , padx = 150 , pady = 30)
button_list[button_info.get("name")].config(image = ph_image)
button_list[button_info.get("name")].pack()
for loop:
button_list[button_info.get("name")] = Button(root,text = button_info.get("name"), compound=LEFT , command= button_info.get("link") , padx = 150 , pady = 30, image = PhotoImage(file = button_info.get("icon")).subsample(35,35))
button_list[button_info.get("name")].pack()
I have also tried to declare the photoimage within the .config function but the same issue prevails.
What i was expecting to occur was the image would be displayed, along side with the text , but only the text is dispalyed.
It also seems inconsistent as when I was trying to use break point to figure out what the issue was sometime the button would load correctly if the time to load the button took a long time ( waiting for the debugger to continue), adding time.sleep(2) did not works as well
I would like to know what I am doing wrong any help would greatly be appreciated, Thanks.