When you press e it will play the sound, which is fine. But when you try to press it while the sound is active it waits until the sound is done to play it again. I need the sounds to be played at the same time.
import tkinter
from playsound import playsound
root = tkinter.Tk()
def key_pressed(event):
playsound('sound.wav')
root.bind("<Key>", key_pressed)
root.mainloop()
Try this:
import tkinter
from playsound import playsound
root = tkinter.Tk()
def key_pressed(event):
playsound('sound.wav', block=False)
root.bind("<Key>", key_pressed)
root.mainloop()
Related
I want to type something in the user_text entry box while the play_audio function is running
I tried the following code:
from tkinter import *
from playsound import playsound
root = Tk()
def play_audio():
playsound('audio.mp3')
play_audio_button = Button(root, text='Play audio', command=play_audio)
user_text = Entry(root)
play_audio_button.pack()
user_text.pack(padx=10, pady=10)
mainloop()
but it doesn't let me do anything while the audio is playing in the background. It only lets me type after the audio is finished.
I also tried doing the same thing withouth tkinter and it works:
def play_audio():
playsound('audio.mp3')
play_audio()
play_audio_input = input('Your text: \n')
It does let me type while the audio is playing in the background that way.
So how can I get it to work in tkinter?
playsound can run sound in the background, you should use threads if you need to loop the sound or something more than just running a single sound file.
def play_audio():
playsound('audio.mp3', block=False)
if you want to loop the sound you don't need multiprocessing, the threading module is perfectly usable for running tasks in the background, which will run the audio in another thread, leaving the main thread to run your GUI.
import threading
def play_audio():
while True:
playsound('audio.mp3')
play_audio_button = Button(root, text='Play audio', command=lambda: threading.Thread(play_audio).start())
I've been editing this mp3 player, but this error keeps apearing
from tkinter import *
from tkinter import messagebox
import playsound
#making the window
window = Tk()
window.title("button")
window.geometry("350x450+500+200")
#making the song fuction
def song():
playsound ("lights.mp3")
# making texbox
def InitialMessage():
name = name_Tf.get()
mood = mood_Tf.get()
#mood happy
if mood == "happy":
return messagebox.showinfo("message",
f"""Hi! {name}, wellcome.
may I intrest you with some music?""",
#making the song executer
button1 = Button(messagebox,
text="yes",
command=song,
font= ("Comic Sans", 15 )))
I don't have python 2 on the pc anymore, I uninstalled it, I have tried reinstaling playsound, as well as change the interpreter to the recomended, I dunno what to do anymore, please help
Try importing playsound with PIP
pip install playsound
And include in your code
from playsound import playsound
playsound('/path/to/a/sound/file/you/want/to/play.mp3')
from tkinter import *
import winsound
from winsound import PlaySound, SND_FILENAME, SND_LOOP, SND_ASYNC
root = Tk()
root.configure(background='light green')
def playing():
winsound.PlaySound('alarm', winsound.SND_FILENAME)
def stop_sound():
PlaySound(None, SND_FILENAME)
Button(root,text="playing ",font=("Helvetica 15"),command=playing).pack(pady=20)
Button(root,text="Stop",font=("Helvetica 15"),command=stop_sound).pack(pady=20)
root.mainloop()
Fix Updated
In your playing function
You can replace winsoud.PlaySound and winsound.SND_FILENAME with PlaySound and SND_FILENAME.
The complete code will look like this
from tkinter import *
from winsound import PlaySound, SND_FILENAME, SND_LOOP, SND_ASYNC
root = Tk()
root.configure(background='light green')
def playing(): PlaySound('alarm', SND_FILENAME)
def stop_sound(): PlaySound(None, SND_FILENAME)
Button(root,text="playing ",font=("Helvetica 15"),command=playing).pack(pady=20)
Button(root,text="Stop",font=("Helvetica 15"),command=stop_sound).pack(pady=20)
root.mainloop()
Description
The idea is same. The program doesn't know what winsound is since we are not importing it. But the functions we are calling are imported. So no need to prefix them with winsound.
Fix
Include this line with other imports
import winsound
Description
From looking at the code you are not importing the 'winsound' module. You are including from it by not the module itself.
Note
When asking question on StackOverflow kindly share your exceptions/log messages so people can help you easily
Iam building a project tkinter.
Here my Code
from tkinter import *
screen=Tk()
screen.geometry('500x300+400+200')
def play():
from playsound import playsound
playsound("music.mp3")
screen.title("Simple project")
btn=Button(screen,text='play',width=20,command=play).place(x=340,y=260)
screen.mainloop()
And when I click button "play" then tkinter screen is Not responding.
So how can I fix it.
Thank you
You can use threading module.
from tkinter import *
from threading import Thread
screen=Tk()
screen.geometry('500x300+400+200')
def play():
from playsound import playsound
playsound("music.mp3")
def start_play():
t=Thread(target=play)
t.daemon=True
t.start()
screen.title("Simple project")
btn=Button(screen,text='play',width=20,command=start_play).place(x=340,y=260)
screen.mainloop()
I am having trouble with my program. If I pressed the button, the timer will be activated. After a few seconds, the app should open. But it doesn't work. How can I make it work? Sorry for my English. I'm from Indonesia.
Here is the code:
from tkinter import *
import os
import time
root = Tk()
my_menu = Menu(root)
root.config(menu=my_menu)
root.title("Tkinter window")
root.geometry("400x400")
def startapp():
t = 3
if time.sleep(t):
os.startfile("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe")
StartButton=Button(root, text='Start', command=startapp)
StartButton.pack()
root.mainloop()
Very simple answer!!!
def startapp():
t = 3.0
time.sleep(t)
os.startfile("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe")
time.sleep() is a function that adding delay! So you don't have to check it using another if condition...