Search code examples
python-3.xduplicatesradio-buttonqt-designer

Python 3 / QT Designer - Radio Buttons showing duplicate values in console


I'm using QT designer to try and make a couple of radio buttons with a label that shows which button I've selected in a variable called URL.

I've got the following code so far :

self.radioButton.toggled.connect(self.myradioButton1_function)
self.radioButton_2.toggled.connect(self.myradioButton1_function)

def myradioButton1_function(self):
    staging = 'https://staging/URL'
    live= 'https://live/URL'

    if self.radioButton.isChecked()==True:
        URL=staging
    if self.radioButton_2.isChecked()==True:
        URL=live

    self.label.setText("URL is : " +str(URL))
    print(URL)

The label display works fine and switches perfectly between live and staging but the problem is with the variable in Python console, when switching between the two buttons - this prints the variable multiple times e.g.

https://staging/URL  
https://live/URL  
https://live/URL  
https://staging/URL  
https://staging/URL  
https://live/URL  
https://live/URL 

I want to use the URL variable in another function so need this to store 1 value on selection of a radio button, can you please advise? Many Thanks.


Solution

  • I fixed this by changing the toggled to clicked e.g.

    self.radioButton.clicked.connect(self.myradioButton1_function)
    self.radioButton_2.clicked.connect(self.myradioButton1_function)