I have 2 radio buttons. The first one, radio1
, is connected to a function func()
and inside this function connects a push button, pushButton
, to another function print_me()
.
This is the code stripped down:
radio = self.dockwidget.radioButton
radio.clicked.connect(func)
def func():
# Connect pushButton to "print_me" function
connect = self.dockwidget.pushButton
connect.clicked.connect(print_me)
def print_me():
print 'Connected'
When the user clicks radio1
and then pushButton
, a message is printed. Problem is if the user clicks radio
10 times and then pushButton
, the message is also printed 10 times. Or if the user clicks radio1
, then radio2
, and back to radio1
, it will still print the message twice.
Is there a way to prevent this so that it will only print the message once when either radio button is clicked?
Have a global variable and set it to either True
or False
. Then add an if
statement to your code (In this example the global variable name is clicked_radio_button
):
radio = self.dockwidget.radioButton
radio.clicked.connect(func)
clicked_radio_button = False #Set global variable
def func():
if clicked_radio_button == False:
# Connect pushButton to "print_me" function
connect = self.dockwidget.pushButton
connect.clicked.connect(print_me)
clicked_radio_button = True #Set global variable to True
else:
pass #Do nothing if already pressed
def print_me():
print 'Connected'