Python Forum
[Tkinter] AttributeError: 'tuple' object has no attribute 'replace' - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] AttributeError: 'tuple' object has no attribute 'replace' (/thread-28687.html)



AttributeError: 'tuple' object has no attribute 'replace' - linuxhacker - Jul-30-2020

from   tkinter  import   *
import    pygame
from   tkinter    import   filedialog

root  =   Tk()
root.title('bynet  mp3 player')

root.geometry("500x300")

#   Initialze   Pygame  Mixer
pygame.mixer.init()



#Add   Song   Function
def   add_song():
	       song   =   filedialog.askopenfilenames(initialdir='audio/',   title="Choose  A  Song",   filetypes=(("mp3  Files",  "*.mp3"),   ))
	       
           #strip    out   the  directory   info    and  .mp3     extension   from   the   song   name  
	       song   =    song.replace("serverlow@serverlow:~/music/audio",   "")
	       song   =    song.replace(".mp3",    "")

           #   Add  song   to   listbox
	       song_box. insert(END,   song)



#   Create  Playlist    Box
song_box   =   Listbox(root,  bg="black",  fg="green",  width=60)
song_box.pack(pady=20)

#   Define  Player   Control   Buttons   Images
back_btn_img   =   PhotoImage(file='image/back50.png')
forward_btn_img   =      PhotoImage(file='image/forward50.png')
play_btn_img   =      PhotoImage(file='image/play50.png')  
pause_btn_img   =      PhotoImage(file='image/pause50.png')
stop_btn_img   =      PhotoImage(file='image/stop50.png')

#  Create  Player   Control   Frames
controls_frame   =    Frame(root)
controls_frame.pack()

#   Create   Player   Control   Buttos   
back_button   =    Button(controls_frame,  image=back_btn_img,   borderwidth=0)
forward_button     =  Button(controls_frame,  image=forward_btn_img,  borderwidth=0)
play_button   =    Button(controls_frame,  image=play_btn_img,    borderwidth=0)
pause_button   =    Button(controls_frame,  image=pause_btn_img,    borderwidth=0)
stop_button   =       Button(controls_frame,  image=stop_btn_img,    borderwidth=0)

back_button.grid(row=0,   column=0 ,   padx=10)
forward_button.grid(row=0,   column=1,     padx=10)
play_button.grid(row=0,   column=2,    padx=10)
pause_button.grid(row=0,   column=3,    padx=10)
stop_button.grid(row=0,   column=4,   padx=10)

#   Create   Menu
my_menu   =   Menu(root)
root.config(menu=my_menu)

#   Add  Add   Song    Menu
add_song_menu   =    Menu(my_menu)
my_menu.add_cascade(label="Add   Song",  menu=add_song_menu)
add_song_menu.add_command(label="Add  One  Song  To   Playlist",   command=add_song)

root.mainloop()
----------------------------------------------------------------
Error:
serverlow@serverlow:~/music$ python3 music.py pygame 1.9.6 Hello from the pygame community. https://www.pygame.org/contribute.html Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python3.8/tkinter/__init__.py", line 1883, in __call__ return self.func(*args) File "music.py", line 20, in add_song song = song.replace("serverlow@serverlow:~/music/audio", "") AttributeError: 'tuple' object has no attribute 'replace



RE: AttributeError: 'tuple' object has no attribute 'replace' - Yoriz - Jul-30-2020

From the error it looks to me that filedialog.askopenfilenames returns a tuple of strings
After the line
song   =   filedialog.askopenfilenames(initialdir='audio/',   title="Choose  A  Song",   filetypes=(("mp3  Files",  "*.mp3"),   ))
try adding print(song) to see what it contains.

It may return a single string if you set the following option to False
https://docs.python.org/3.9/library/dialog.html#module-tkinter.filedialog Wrote:multiple - when true, selection of multiple items is allowed



RE: AttributeError: 'tuple' object has no attribute 'replace' - buran - Jul-30-2020

there is also filedialog.askopenfilename - note the singular name


RE: AttributeError: 'tuple' object has no attribute 'replace' - linuxhacker - Jul-30-2020

# Play selected song
def play():
song = song_box.get(ACTIVE)
song = f'serverlow@serverlow:~/music/audio/{song}.mp3'

pygame.mixer.music.load(song )
pygame.mixer.music.play(loops=0)
-------------------------------------------------------
serverlow@serverlow:~/music$ python3 music.py
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
return self.func(*args)
File "music.py", line 31, in play
pygame.mixer.music.load(song )
pygame.error: Couldn't open 'serverlow@serverlow:~/music/audio//home/serverlow/music/pm3/___ Ben How - ______4 ______.mp3'


RE: AttributeError: 'tuple' object has no attribute 'replace' - deanhystad - Jul-30-2020

Might want to look at the path information you get back from song_box. Is that path valid when appended to "serverlow@serverlow:~/music/audio/{song}.mp3"?


RE: AttributeError: 'tuple' object has no attribute 'replace' - linuxhacker - Aug-01-2020

# Add Many songs to play
add_song_menu.add_command(label="Add Many songs To Playlist", command=add_many_song)

---------------------------------------------------
serverlow@serverlow:~/music$ python3 music.py
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "music.py", line 107, in <module>
add_song_menu.add_command(label="Add Many songs To Playlist", command=add_many_song)
NameError: name 'add_many_song' is not defined


RE: AttributeError: 'tuple' object has no attribute 'replace' - ndc85430 - Aug-01-2020

Come on, you need to try and debug the problems yourself. The error is pretty self-explanatory.


RE: AttributeError: 'tuple' object has no attribute 'replace' - linuxhacker - Aug-08-2020

(Aug-01-2020, 04:26 AM)ndc85430 Wrote: Come on, you need to try and debug the problems yourself. The error is pretty self-explanatory.
Thank you.