1

I have a simple question about the bell sounds in Tkinter. How can I add one of them when my progress bar is completed or when I open a message box window with the filedialog.asksaveasfilename() function?

stovfl
  • 14,998
  • 7
  • 24
  • 51
TurboC
  • 777
  • 1
  • 5
  • 19
  • Does this answer your question? [how-would-i-go-about-playing-an-alarm-sound-in-python](https://stackoverflow.com/questions/4006709) – stovfl May 28 '20 at 13:34

1 Answers1

6

You can use the method bell() to trigger a system bell sound.

Here is an example how you can play it when the progress bar completes.

import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()

def work():
    if progressbar['value'] == 0:
        but1['state'] = 'disabled'
    if progressbar['value'] >= progressbar['maximum']:
        root.bell()
        but1['state'] = 'normal'
        progressbar['value'] = 0
        return
    progressbar['value'] += 1
    root.after(100, work)

progressbar = ttk.Progressbar(root, length=200, maximum=10, value=0)
progressbar.grid(row=1)

but1 = ttk.Button(root, text='Start', command=work)
but1.grid(row=2)

root.mainloop()

Similarly, with the help of this example, I hope you can implement it with message pop-ups as well.


The Tkinter bell() is the default sound of the operating system, to change the bell sound in the Tkinter application change the system default alert sound. For example,

  • on macOS, one can do it by choosing Apple menu > System Preferences, click Sound, then click Sound Effects.

enter image description here

Saad
  • 3,340
  • 2
  • 10
  • 32
  • ciao Saad! you have been very clear and thank you so much for having replied with an example. your code works, but I have just realized that you can't play the bell if you lanch your script via CLI. for example, in my code, to play the bell, I had to change the extension of my script from ".py" to ".pyw" (my operating system is Windows 10 Enterprise x64"). – TurboC May 28 '20 at 13:15
  • now I have another question, how can I choose a different kind of bell? the default bell doesn't match my scopes – TurboC May 28 '20 at 13:33
  • @TurboC: I ran the code through the command line with and without `.pyw` extension and it is working as it should **though I'm using macOS **. Tkinter bell sound rings the default system alert sound, if you change your system alert sound, it will change in Tkinter apps as well. Like I said as I'm on a mac and I can confirm changing system default sound will indeed change the beep sound in Tkinter apps. – Saad May 28 '20 at 13:33
  • mm, I see, probably in Windows Tkinter has a different behaviour. but, anyway, so it means that if I want to change the sound raised in "bell()", Tkinter makes me to change the system sound in my operating system? isn't there a simpler way to change the "bell sound"? maybe choosing a different one from the "bell()" function without touch the system settings? – TurboC May 28 '20 at 14:19
  • @TurboC: I don't think if there is a native way of changing bell sound just for the Tkinter applications, but instead you can use other modules like `pygame` or `winsound` to change sounds with a `.mp3` file to achieve your goal. [Have a look at this question and its answers.](https://stackoverflow.com/questions/4006709/how-would-i-go-about-playing-an-alarm-sound-in-python) – Saad May 28 '20 at 14:25
  • @TurboC: I added some useful information to my answer, check it if this solves your issue and mark this post as *answered* by [accepting](https://stackoverflow.com/help/someone-answers) the best answer accordingly. – Saad May 30 '20 at 09:40
  • 1
    ciao Saad, sorry for the late but I was still testing my code. yeah, your answer is good but at the end, as you adviced me, I chosed to use winsound. It's esier because I can choose all system's sounds that I want (all of them in Windows are placed in "C:\Windows\Media"). however using this module means to be dipendent by the system, but.. I will solve this issue when I will complete it. thanks for your support ;) – TurboC May 31 '20 at 11:47