0

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?

Joseph
  • 586
  • 1
  • 13
  • 32
  • 1
    you could use a global variable which stores whether the function was already called. – Maximilian Peters Jun 17 '16 at 12:22
  • @Ashafix - Thanks, I will check this and see how it goes =) – Joseph Jun 17 '16 at 12:24
  • 1
    Careful with the global variable idea. Are you certain you only want the function to run once in the entire execution of the program? If the radio button is cleared (by clicking on another button in the group, for example) and then clicked again, do you want the function to run the second time around? How about if the user navigates away from that form, and later comes back to it? – nephtes Jun 17 '16 at 12:31
  • @nephtes - Thanks, yes I am certain I only want to run it once. I do infact have 2 buttons which when one is clicked, the other is cleared. But whichever one is clicked, I only need it to run once. Even if the user navigates to another form :) – Joseph Jun 17 '16 at 12:36

1 Answers1

1

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'
Bubble Hacker
  • 6,425
  • 1
  • 17
  • 24
  • Apologies, I should have mentioned in my question that I have 2 radio buttons. This works great if there's one but if the user switches between the 2 radio buttons, no messages are printed since the global variable has already been set. Is it possible to insert the global variable inside the function? – Joseph Jun 20 '16 at 15:23
  • 1
    How about setting two global variables? and then set the `if` statement to match both =] – Bubble Hacker Jun 20 '16 at 20:41
  • 1
    Anytime! happy coding =] – Bubble Hacker Jun 21 '16 at 11:29