0
\$\begingroup\$

I tried making a music player with tkinter in python but there's an error about module not callable but I'm not able to fix it. Can someone help?

import tkinter as Tk
import pygame 
import os


def __init__(self,root):
    self.root = root
    # title of the window
    self.root.title("MusicPlayer")
    # window geometry
    self.root.geometry("1000x200+200+200")
    # initiating pygame
    pygame.init()
    # initiating pygame mixer
    pygame.mixer.init()
    # declaring track variable
    self.track = StringVar()
    # Declaring status variable
    self.status = StringVar() 

    #creating the track frames for song label & status label
    trackframe = LabelFrame(self.root,text='Song Track',font=('times new roman',15,'bold'),bg='Navyblue',fg='white',bd='5',relief=GROOVE)
    trackframe.place(x=0,y=0,width=600,height=100)
    # inserting song track label
    songtrack = Label(trackframe,textvariable=self.track,width=20,font=('time new roman',24,'bold'),bg='orange',fg='gold').grid(row=0,column=0,padx=10,pady=5)
    # inserting status label
    trackstatus = Label(trackframe,textvariable=self.status,font=('times new roman',24,'bold'),bf='orange',fg='gold').grid(row=0,column=1,padx=10,pady=5)

    # creating button frame
    buttonframe = LabelFrame(self.root,text='control panel',font=('times new roman',15,'bold'),bg='grey',fg='white',bd=5,relief=GROOVE)
    buttonframe.place(x=0,y=100,widht=600,height=100)
    # inserting play button
    playbtn = Button(buttonframe, text='PLAYSONG',command=self.playsong,width=10,height=1,font=('times new roman',16,'bold'),fg='navyblue',bg='pink').grid(row=0,cloumn=0,padx=10,pady=5)
    # inserting pause button
    #this might need to be renamed
    playbtn = Button(buttonframe,text='PAUSE',command=self.pausesong,widht=8,height=1,font=('times new roman',16,'bold'),fg='navyblue',bg='pink').grid(row=0,cloumn=1,padx=10,pady=5)
    # inserting unpause button
    playbtn = Button(buttonframe,text='UNPAUSE',command=self.unpausesong,widht=10,height=1,font=('times new roman',16,'bold'),fg='navyblue',bg="pink").grid(row=0,column=2,padx=10,pady=5)
    # Inserting Stop Button
    playbtn = Button(buttonframe,text="STOPSONG",command=self.stopsong,width=10,height=1,font=("times new roman",16,"bold"),fg="navyblue",bg="pink").grid(row=0,column=3,padx=10,pady=5)

    # creating playlist frame
    songsframe = LabelFrame(self.root, text="Song Playlist",font=('times new roman',15,'bold'),bg='grey',fg='white',bd=5,relief=GROOVE)
    songsframe.place(x=600,y=0,wodht=400,height=200)
    # inserting scrollbar
    scrol_y = Scrollbar(songsframe, orient=VERTICAL)
    # Inserting playlist listbox
    self.playlist = Listbox(songsframe, yscrollcommand=scrol_y.set, selectbackground='gold',selectmode=SINGLE,font=('times new roman',12,'bold'),bg='silver',fg='navyblue',bd=5,relief=GROOVE)
    # applying scrollbar to listbox
    scrol_y.pack(side=RIGHT,fill=Y)
    scrol_y.config(command=self.playlist.yview)
    self.playlist.pack(fill=BOTH)
    # changing directory for fethcing songs
    os.chdir('D:\python\music_player\songs')
    # FETCHING SONGS
    songstracks = os.listdir()
    # inserting songs into playlist
    for track in songtracks:
        self.playlist.insert(END,track)

def playsong(self):
    # displaying selected song title
    self.track.set(self.playlist.get(ACTIVE))
    # displaying status
    self.status.set('-Playing')
    # loading selected song
    pygame.mixer.music.load(self.playlist.get(ACTIVE))
    # PLAYING selected song
    pygame.mixer.music.play()

def stopsong(self):
    # displaying status
    self.status.set('-Stopped')
    # stopped song
    pygame.mixer.music.stop()

def pausesong(self):
    # displaying status
    self.status.set('-Paused')
    # paused song
    pygame.mixer.music.pause()

def unpausesong(self):
    # it will display the status
    self.status.set('-Playing')
    # playing back song
    pygame.mixer.music.unpause()

root = Tk() # In order to create an empty window
# Passing Root to MusicPlayer Class
MusicPlayer(root)

root.mainloop()
```
\$\endgroup\$
3
\$\begingroup\$

imported tkinter as Tk

root = Tk() # In order to create an empty window

You imported tkinter under the name Tk, which coincidentally matches with the Tk() class of the tkinter module. By doing Tk() you are basically calling tkinter(), which is not a valid method or function.

I suggest you use import tkinter as tk instead.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.