Yasmim Rodrigues
Yasmim Rodrigues

Reputation: 13

Error: "'Listbox' object is not callable"" using the ACTIVE

I'm trying to make songs in a playlist of a listbox become active when i select they, but i get a error "File "c:/Users/User/Documents/Python-testes/teste-2.py", line 21, in play filenames = Playlist_box(ACTIVE) TypeError: 'Listbox' object is not callable"

this is my code:

from tkinter import Listbox, Tk
from tkinter import Label
from tkinter import Button
from tkinter import filedialog
from tkinter.constants import ACTIVE, END
from pygame import mixer
import pygame

def play_song():
    filenames = list(filedialog.askopenfilenames(initialdir = "C:/Python/Playlist/", title = 
   "Please select a file", filetypes=(("Mp3 Files", "*.mp3"),)))
    for song in filenames:
        song = song.split("/")
        song = song[-1]
        Playlist_box.insert(END,song)


def play():
    filenames = Playlist_box(ACTIVE)
    filenames = f'C:/Python/Playlist/{filenames}.mp3'
    mixer.music.load(filenames)
    mixer.music.play(loops=0)
    


root = Tk()
root.title('test')
label = Label(root, 
    text="choose the song").pack()

Playlist_box = Listbox(root, bg="black", fg="green", width=60)
Playlist_box.pack()

Button(root, text="choose your songs", command=play_song).pack()
Button(root, text="Play music", padx=12, bg="black", fg="white", command= play).pack()

root.mainloop()

Can someone show me whats wrong? it's not 'ACTIVE' or it's something with the "filenames"?

Upvotes: 1

Views: 369

Answers (2)

user15801675
user15801675

Reputation:

The actual answer is :

Playlist_box.get(ACTIVE)

According to this post:

An item becomes active after you click on it—which means after your ListboxSelect method returns. So, you're printing out whatever was active before this click (meaning, generally, what you clicked last time).

Playlist_box.get(ACTIVE)

Therefore this will play the previously selected song.

So what you actually need is:

Playlist_box.get(Playlist_box.curselection())

Upvotes: 1

PCM
PCM

Reputation: 3011

This line

def play():
    filenames = Playlist_box(ACTIVE)

should be -

def play():
    filenames = Playlist_box.get(ACTIVE)

Upvotes: 1

Related Questions