简体   繁体   中英

Make a button behave differently when pressed a second time (Python)

Say, if I have a button that plays a sound, can I make it so that if you press it a second time it stops the music?

I use Python v.2.7, Easy Eclipse as IDE and wxFormBuilder for the windows.

Here's my code:

import gui
import wx
import wx.media
import pygame
import tkFileDialog

class MainFrame( gui.GUI_MainFrame):
    def __init__( self, parent ): #Definerar KunddatabasMainFrame
        pygame.init()
        gui.GUI_MainFrame.__init__( self, parent ) #Initierar MainFrame f�nstret
        self.sound1=pygame.mixer.Sound('beat1.wav')
        self.sound2=pygame.mixer.Sound('beat2.wav')
        self.recording = False

    def evtBrowse1(self, evt):
        tkFileDialog.askopenfilename()

    def evtSoOne(self, evt):
        self.sound1.play(loops=-1)

I would use a global boolean variable and toggle it each time the button is pressed. The function that is called on the button press would then do different things depending on the state of this variable.

You can of course extend this to an int variable if you want to handle many more cases.

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