Just to get this clear before I begin.
I have the same problem as outlined in this question: Tkinter: creating an arbitrary number of buttons/widgets
but the accepted answer doesn't work for me.
This is my code:
import os
try:
from tkinter import *
except ImportError:
from Tkinter import *
from subprocess import call
root = Tk()
root.wm_attributes("-fullscreen", "true")
root.config(background = "#FFFFFF")
backIMG = PhotoImage(file="b.gif")
usbIMG = PhotoImage(file="u.gif")
usbAuswahlIMG = PhotoImage(file="ua.gif")
downloadsIMG = PhotoImage(file="d.gif")
downloadsAuswahlIMG =
PhotoImage(file="da.gif")
menuFrame = Frame(root, width=200, height=600, bg="#FFFF00")
menuFrame.grid(row=0, column=0, padx=10, pady=3)
def goBack():
#os.system('python gui.py')
root.destroy()
def selectUSB():
downloadsButton.config(image=downloadsIMG)
usbButton.config(image=usbAuswahlIMG)
def selectDownloads():
usbButton.config(image=usbIMG)
downloadsButton.config(image=downloadsAuswahlIMG)
i = 0
buttons = dict()
for file in os.listdir('/home/pi/Downloads'):
if file.endswith(".mp3") or file.endswith(".wav"):
buttons[i] = Button(contentFrame, text=file, width=60, font=("Sans", 15), command=lambda a=i: playDL(a).grid(row=i, column=0))
i = i + 1
print()
def playDL(index):
print (index)
usbButton = Button(menuFrame, image=usbIMG,
command=selectUSB)
usbButton.pack()
downloadsButton = Button(menuFrame, image=downloadsIMG,
command=selectDownloads)
downloadsButton.pack()
stopButton = Button(menuFrame, image=backIMG,
command=goBack)
stopButton.pack()
contentFrame = Frame(root, width=760, height=594, bg='#FFFFFF')
contentFrame.grid(row=0, column=1, padx=13, pady=3)
root.mainloop()
On the other post, one suggested, to add this code here:
buttons = dict()
for k in range(len(info)):
buttons[k] = Button(top, text=info[k], command=lambda a=k: my_function(buttons[a]))
which I've done and changed it up here and there to fit my code.
The problem is that now the buttons don't show up on the content Frame at all. It still runs though the loop but no buttons to be seen. At first I just had this:
for file in os.listdir('/home/pi/Downloads'):
if file.endswith(".mp3") or file.endswith(".wav"):
Button(contentFrame, text=file, width=60, font=("Sans", 15), command=lambda: playDL(i).grid(row=i, column=0))
i = i + 1
and all the buttons did the same nothingeness, but were visible.
I started programming in python yesterday but i am quiet familiar with Java and Visual C# so I have a basic understandment of what I'm doing (sort of)