Basile
Basile

Reputation: 19

Windows error sound plays a second time when closing the error window

I have a working python program that searches through a .pak file and modifies certain values, according to the user's choices, copies it while assigning it a name, and generates it in the directory. I'm using checkboxes for the user to select between different options. Depending on what is checked, the program modifies different values in the template file after copying it and assigning it a name that the user chose.

I have 2 possible errors that the user can trigger when pressing the 'Generate File' button: 1- The user has not named his file 2- The user has not checked any checkboxes

Using messagebox.showerror and winsound.PlaySound inside an if, it is very easy to code. However, I have ran into a small issue that I cannot seem to get around. The error sound is played when the user presses the generate button and runs into one of the 2 possible errors, but for some reason it also plays when closing the error window that pops up.

Here is the simplified code:

import tkinter as tk
import winsound
from tkinter import messagebox

...
def writePak(self):
        
        file_name = self.entry_text.get()
        if not file_name:
            messagebox.showerror('Error', 'Please name your file!')
            winsound.PlaySound("SystemHand", winsound.SND_ALIAS)
        else:
...
any_checked = False
...
              if not any_checked:
                   os.remove(newFileName)
                   messagebox.showerror('Error', 'Please select at least one option!')
                   winsound.PlaySound("SystemHand", winsound.SND_ALIAS)
                   return

This happens with both error windows.

As I am new to coding (this is my first program), I didn't really know what to do with that specific issue. I tried to talk to ChatGPT but it had trouble understanding my problem and its answers were generally pretty mediocre.

Upvotes: 0

Views: 76

Answers (1)

Pragmatic_Lee
Pragmatic_Lee

Reputation: 491

messagebox.showerror plays its own sound.

You don’t need winsound.Playsound - that’s where the 2nd sound is coming from.

Upvotes: 1

Related Questions