1

I'm using Tkinter to make a music sorting program. I've organized my data into a dictionary, where the keys are the artists and the values are the songs. I've also already been successful at creating a listbox with the artists as the different selections.

The part I can't figure out is how to make the selection from the artist listbox open up a new listbox with all the related songs (the related values from the dictionary). This is what I have so far:

    file = open("songs.txt")
    music = createdict(file)

    keys = sorted(music.keys())
    values = music.values()

    #Artist Option Listbox with Scrollbar
    root = tk.Tk()
    root.title("Artist Options")

    scrollbar = Scrollbar(root, bg = "grey")
    scrollbar.pack(side = RIGHT, fill = Y)

    def cureselet(evt):
        Lb1.see(ACTIVE)
        item = Lb1.get(Lb1.curselection())
        return item

    Lb1 = Listbox(root, selectmode = SINGLE, font = ('times', 13), width = 50, height = 15, bd = 4, selectbackground = "yellow", bg = "grey")
    Lb1.bind(curselet)

    c = 0
    for i in keys:
        Lb1.insert(END,keys[c])
        c+=1
    Lb1.pack()

    Lb1.config(yscrollcommand = scrollbar.set)
    scrollbar.config(command = Lb1.yview)

    Lb2 = Listbox(Lb1, selectmode = SINGLE, font = ('times', 13), width = 50, height = 15, bd = 4, selectbackground = "yellow", bg = "grey")

2 Answers 2

1

Have the user click a button when ready/after something is selected. Use the button callback to execute a function that will do this, with another button to close the 2nd listbox.

Sign up to request clarification or add additional context in comments.

Comments

0

I agree with user2133443, but I would like to expand on what he said.

You should create a new frame with a scroll bar and a bunch of buttons going down. Each of these buttons act as an 'option' in the option menu. You can allocate a function to each of these buttons that replaces all the labels in another scrolling frame right next it with each of the songs from that artist.

Furthermore, I recommend keeping these new object you're making in a class so it's easy to keep track of them.

If you need any help, just give me a shout.

4 Comments

I decided to make it so that there are two listboxes that open at the same time, and when an artist is selected from the first listbox, all the associated songs are shown in the second listbox. I'm just having trouble figuring out the connection. I was able to make all the listboxes, but it's the connection between the two that's giving me trouble
Although I'm gonna do what you suggested at the same time
Yeh, you see, that's the problem. I don't think that that is possible. You can't assign function to list box menu items, so there is really nothing that can be done after you select a an option. You could try a detect when there is a change in the variable that's tied to the option box, but that would require you engineer your own mainloop which is a really bad way of doing anything.
You could bind a function to when the user right clicks on the listbox item, but that sounds annoying (for the user)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.