I am making a MP3 Player by using tkinter python. I need to use file dialog to select songs from that into the playlist. But when I use `
import tkinter as tk
from tkinter import filedialog as fd
import pygame
root = tk.Tk()
root.title('MP3 Player')
root.geometry('500x300')
#Initialize Pygame Mixer
pygame.mixer.init()
# Add One Song function
def addOneSong():
song = fd.askopenfile
print(song)
# Create a Playlist Box
songBox = tk.Listbox(root, bg="black", fg="green", width=60)
songBox.pack(pady=20)
# Define Player control Buttons
backButtonImg = tk.PhotoImage(file="Images/back50.png")
forwardButtonImg = tk.PhotoImage(file="Images/forward50.png")
playButtonImg = tk.PhotoImage(file="Images/play50.png")
pauseButtonImg = tk.PhotoImage(file="Images/pause50.png")
stopButtonImg = tk.PhotoImage(file="Images/stop50.png")
# Create Player Controls Frame
controlFrame = tk.Frame(root)
controlFrame.pack()
# Create Player Controls Buttons
backButton = tk.Button(controlFrame, image=backButtonImg, borderwidth=0)
forwardButton = tk.Button(controlFrame, image=forwardButtonImg, borderwidth=0)
playButton = tk.Button(controlFrame, image=playButtonImg, borderwidth=0)
pauseButton = tk.Button(controlFrame, image=pauseButtonImg, borderwidth=0)
stopButton = tk.Button(controlFrame, image=stopButtonImg, borderwidth=0)
backButton.grid(row=0, column=0, padx=10)
stopButton.grid(row=0, column=1, padx=10)
playButton.grid(row=0, column=2, padx=10)
pauseButton.grid(row=0, column=3, padx=10)
forwardButton.grid(row=0, column=4, padx=10)
# Create Menu
myMenu = tk.Menu(root)
root.config(menu=myMenu)
# Add Add Song Menu
addSongMenu = tk.Menu(myMenu)
myMenu.add_cascade(label="Add Songs", menu=addSongMenu)
addSongMenu.add_command(label="Add One Songs to Play", command=addOneSong)
root.mainloop()
`
In the above code
from tkinter import filedialog as fd
this create a error
as::
Traceback (most recent call last):
File "f:\Python - Tkinter\50MP3Player.py", line 2, in <module>
from tkinter import filedialog as fd
File "C:\Users\abhay\AppData\Local\Programs\Python\Python311\Lib\tkinter\filedialog.py", line 21, in <module> from tkinter import (
ImportError: cannot import name 'TOP' from 'tkinter' (C:\Users\abhay\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py)
PS F:\Python - Tkinter>
Pls Help to so that I can use file dialog
I tried different ways to import file dialog but all present the same error
I wasn't able to recreate your error, but when you are calling "fd.askopenfile()" you don't completely call it or provide any argument values. The below correction works for me. I also included a supplemental link if you need additional information.
https://www.geeksforgeeks.org/python-askopenfile-function-in-tkinter/