Akarui Kage
Akarui Kage

Reputation: 3

Tkinter: Create an arbitrary amount of buttons

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)

Upvotes: 0

Views: 129

Answers (1)

Mike - SMT
Mike - SMT

Reputation: 15236

Change 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

To 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 i=i: playDL(i)).grid(row=i, column=0)
            i = i + 1

You placed your grid manage inside of your lambda function instead of outside the button widget.

As tobias pointed out in the comments you need to use i=i in your lambda in order for your values to be accurate for each button or all of them will have the last value in the loop.

Upvotes: 1

Related Questions