简体   繁体   中英

I'm using tkinter GUI and winsound but stop function is not working

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM