Python Forum
[Tkinter] AttributeError: 'tuple' object has no attribute 'replace'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] AttributeError: 'tuple' object has no attribute 'replace'
#1
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
Reply
#2
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/dial...filedialog Wrote:multiple - when true, selection of multiple items is allowed
Reply
#3
there is also filedialog.askopenfilename - note the singular name
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
# 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'
Reply
#5
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"?
Reply
#6
# 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
Reply
#7
Come on, you need to try and debug the problems yourself. The error is pretty self-explanatory.
Reply
#8
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  'NoneType' object has no attribute 'get' zunebuggy 8 1,244 Oct-13-2023, 06:39 PM
Last Post: zunebuggy
  tkinter AttributeError: 'GUI' object has no attribute pfdjhfuys 3 1,455 May-18-2023, 03:30 PM
Last Post: pfdjhfuys
  [Kivy] Windows 10: AttributeError: 'WM_PenProvider' object has no attribute 'hwnd' mikepy 1 2,247 Feb-20-2023, 09:26 PM
Last Post: deanhystad
  [Tkinter] Can't update label in new tk window, object has no attribute tompranks 3 3,468 Aug-30-2022, 08:44 AM
Last Post: tompranks
  [PyGUI] [Solved]Replace attribute for Floats Extra 5 4,383 Jun-10-2022, 05:44 PM
Last Post: deanhystad
  AttributeError: 'NoneType' object has no attribute 'get' George87 5 15,149 Dec-23-2021, 04:47 AM
Last Post: George87
  [PyQt] AttributeError: 'NoneType' object has no attribute 'text' speedev 9 11,246 Sep-25-2021, 06:14 PM
Last Post: Axel_Erfurt
  [Tkinter] AttributeError: '' object has no attribute 'tk' Maryan 2 14,446 Oct-29-2020, 11:57 PM
Last Post: Maryan
  [Kivy] AttributeError: 'NoneType' object has no attribute 'bind' faszination_92 2 6,191 Apr-12-2020, 07:01 PM
Last Post: Larz60+
  AttributeError: '_tkinter.tkapp' object has no attribute 'place_forget' edphilpot 5 9,110 Dec-20-2019, 09:52 PM
Last Post: joe_momma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020