Could someone please tell me why this code will not close my app when the [X] at top right corner is clicked?

def shutdown():
        track.stop()
        app.destroy()
    
app.protocol("WM_DELETE_WINDOW", shutdown)

If you want to see the whole code it's on an earlier post called "Tkinter Scale".
Thanks.

Recommended Answers

All 2 Replies

Are you running program directly from file manager or terminal window.

Some Tkinter stuff is in conflict with IDLE and does not work when run from there.

Maybe you would like to add

raise SystemExit, "Bye, bye"

to guit whole application, not only the window?

Your problem is not with app.destroy(), but with track.stop()

self.track is an instance name (instance of pygame Sound class) in class SoundPanel and needs to be associated with the proper instance of class SoundPanel
like panel1.track.stop(), panel2.track.stop() etc.

I took the liberty to play around with your previous program (nice programming effort by the way!) and came up with this ...

# sound_panel2.py
# this can be a module

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk
import pygame.mixer

# create class
class SoundPanel(tk.Frame):
    def __init__(self, parent, mixer, sound_file):
        tk.Frame.__init__(self, parent)
        self.track = mixer.Sound(sound_file)
        self.track_playing = tk.IntVar()
        
        track_button = tk.Checkbutton(self, variable=self.track_playing,
            command=self.track_toggle, text=sound_file)
        track_button.pack(side='left')
        self.volume = tk.DoubleVar()
        self.volume.set(self.track.get_volume())
        volume_scale = tk.Scale(self, variable=self.volume, 
            from_=0.0, to=1.0,
            resolution=0.1, command=self.change_volume,
            label="Volume", orient='horizontal')
        volume_scale.pack(side='right')
        
    def track_toggle(self):
        if self.track_playing.get() == 1:
            self.track.play(loops = -1)
        else:
            self.track.stop()
            
    def change_volume(self, v):
        self.track.set_volume(self.volume.get())

# test the module
if __name__ == '__main__':
    # pick a sound file you have in the working folder
    # or supply full path
    soundfile1 = "Chimes.wav"
    soundfile2 = "DingDong.wav"

    # create main window
    root = tk.Tk()
    root.title("Sound Mixer")

    # create mixer
    mixer = pygame.mixer
    mixer.init()

    # create instances
    panel1 = SoundPanel(root, mixer, soundfile1)
    panel1.pack()
    panel2 = SoundPanel(root, mixer, soundfile2)
    panel2.pack()

    def shutdown():
        # self.track is a variable in class SoundPanel and 
        # needs to be associated with the proper instance
        panel1.track.stop()
        panel2.track.stop()
        root.destroy()
        
    # shut it down orderly
    root.protocol("WM_DELETE_WINDOW", shutdown)
    # run the event loop
    root.mainloop()

I prefer root over app, just a personal preference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.