python - pyqt4: How to change button event without adding a new event? -


i have click button want change events when button pressed. minimal version of existing code looks this:

# event happens first time def first_event(self):     button.settext("second event")     button.clicked.connect(second_event)  # event happens second time def second_event(self):     button.settext("first event")     button.clicked.connect(first_event)  button = qtgui.qpushbutton("first event") button.clicked.connect(first_event) 

unfortunately, instead of changing event happens, adds , event clicked signal, meaning following happens:

first button press - calls first_event

second button press - calls first_event , second_event

third button press - calls first_event twice , second_event

etc...

my desired behavior have button change functions when pressed, resulting behavior be:

first button press - calls first_event

second button press - calls second_event

third button press - calls first_event

etc...

is there way make changes click event instead of adding new one? there way remove events after fact?

i found way separate old events signals using disconnect() method. here edited version accomplishes wanted do:

# event happens first time def first_event(self):     button.settext("second event")     button.clicked.disconnect()     button.clicked.connect(second_event)  # event happens second time def second_event(self):     button.settext("first event")     button.clicked.disconnect()     button.clicked.connect(first_event)  button = qtgui.qpushbutton("first event") button.clicked.connect(first_event) 

it's worth noting instead of doing this, did ekhumoro mentioned in comments, , created wrapper function flag record current state, , call first_event() or second_event() based on value of flag.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -