Python Forum
[Tkinter] Button Plays Random .MP3 File From Folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Button Plays Random .MP3 File From Folder
#1
I have been trying to figure this out for a while, and can't find a solution to why it won't work. I'm pretty new, and this is probably an easy fix, but this code:
from tkinter import *
import pygame
import os
import random

root = Tk()
root.title('Hello')
root.geometry('500x400')


pygame.init()
pygame.mixer.init()


randomfile = random.choice(os.listdir("D:/Desktop/Plugin"))


def play():
	pygame.mixer.music.load(randomfile)
	pygame.mixer.music.play(loop=0)

startButton = Button(root, text="Start", font=("Helvetica", 32), command=play)
startButton.pack(pady=20)

def stop():
	pygame.mixer.music.stop()

stopButton = Button(root, text="Stop", command=stop)
stopButton.pack(pady=20)

root.mainloop()
Will always give me the error: pygame.error: Couldn't open 'audio2.mp3' I have 3 audio files in the folder and can't figure out why this wouldnt work.
Larz60+ write Feb-05-2021, 09:15 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Reply


Messages In This Thread
Button Plays Random .MP3 File From Folder - by blufacebaby - Feb-05-2021, 01:33 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,066 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] how to input a random entry with each button press? nadavrock 1 6,447 Jun-17-2019, 05:28 AM
Last Post: Yoriz
  [WxPython] Any dialog that allow user to select file OR folder? buran 3 4,285 Apr-03-2019, 06:33 PM
Last Post: Yoriz
  Simple Button click on image file to create action? jpezz 4 6,961 Mar-27-2019, 10:08 PM
Last Post: jpezz
  Python, Tkinter. How to search a specific file in a folder Roo 3 3,460 Feb-13-2019, 10:41 AM
Last Post: Larz60+
  simple file button Epilepsy 1 2,566 May-22-2018, 06:22 AM
Last Post: Barrowman

Forum Jump:

User Panel Messages

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