1

I want to assign a value to a radiobutton when it is checked.

def radiobutton8(self):
    if self.radioButton_8.isChecked():
        self.radioButton_8 = 02

However, this cannot work. Any solution?

UPDATE: My code has been edited to be:

class MyRadioButton(QtGui.QRadioButton):
    def __init__(self):
        super(MyRadioButton, self).__init__()
        self.value = None

    def SetValue(self, val):
        self.value = val

    def GetValue(self):
        return self.value

class UserTool(QtGui.QDialog):
    def setup(self, Dialog):
                          ...
        self.radioButton_8.toggled.connect(self.radiobutton8)

    def retranslateUi(self, Dialog):
                          ...
        self.radioButton_8 = MyRadioButton()
        self.radioButton_8.setText(_translate("Dialog", "A1", None))

def radiobutton8(self):
    if self.radioButton_8.isChecked():
        value = self.radioButton_8.setValue("02")
        self.lcdNumber.display(value)

However, my original text 'A1' for the radio button is now missing and my number still does not appear on my lcd when checked. Any idea why?

UPDATE: I edited my code to be something like that:

class MyRadioButton(QtGui.QRadioButton):
    def __init__(self):
        super(MyRadioButton, self).__init__()
        self.value = None

    def SetValue(self, val):
        self.value = val

    def GetValue(self):
        return self.value

class UserTool(QtGui.QDialog):
    def setup(self, Dialog):
                  ...
        self.lcdNumber = QtGui.QLCDNumber(Dialog)
        self.lcdNumber.setGeometry(QtCore.QRect(590, 10, 71, 23))
        self.lcdNumber.setFrameShadow(QtGui.QFrame.Raised)
        self.lcdNumber.setObjectName(_fromUtf8("lcdNumber"))
        self.lcdNumber.setStyleSheet("* {background-color: black; color: white;}")
        self.lcdNumber.display('00')

        self.radioButton_8 = QtGui.QRadioButton(Dialog)
        self.radioButton_8.setGeometry(QtCore.QRect(460, 10, 82, 17))
        self.radioButton_8.setChecked(False)
        self.radioButton_8.setAutoExclusive(False)
        self.radioButton_8.setObjectName(_fromUtf8("radioButton_8")) 
        self.radioButton_8 = MyRadioButton()
        self.radioButton_8.setText("A1")
        self.radioButton_8.setValue(02)
        self.radioButton_8.toggled.connect(self.showValueFromRadioButtonToLCDNumber)

    def showValueFromRadioButtonToLCDNumber(self):
        value = self.radioButton_8.GetValue()
        if self.radioButton_8.isChecked():
            self.lcdNumber.display(value)

And then now I have this error:

Traceback (most recent call last):
  File "C:/Users/Vivien Phua/Documents/Python Scripts/Rs232.py", line 303, in handleOpenWidget
    self.popup = UserTool()
  File "C:/Users/Vivien Phua/Documents/Python Scripts/Rs232.py", line 39, in __init__
    self.setup(self)
  File "C:/Users/Vivien Phua/Documents/Python Scripts/Rs232.py", line 153, in setup
    self.radioButton_8.setValue(02)
AttributeError: 'MyRadioButton' object has no attribute 'setValue'

I have also tried the code you have given me and there is no such error, but the LCD in your code does not show the value 02.

6
  • For your second UPDATE error: it is suppose to be SetValue, not setValue. Code I gave you shows 2, if you want it to show 02 set that value as string: self.radioButton_8.SetValue("02") instead of self.radioButton_8.SetValue(02) . I've updated that in my answer in EDIT2, but if you take a look I've also mentioned it in EDIT1
    – Aleksandar
    Jul 17, 2014 at 8:40
  • I tried to run your code in EDIT2 and made sure it is self.radioButton.SetValue("02") but it still doesn't work. Nothing happens when I checked the radio button.
    – Viv91
    Jul 18, 2014 at 2:21
  • Oh it worked! I deleted this line self.pushButton_7.clicked.connect(self.showValueFromRadioButtonToLCDNumber) and added self.radioButton.toggled.connect(self.showValueFromRadioButtonToLCDNumber) and it worked. However is there anyway way to revert the LCD display if radio button is checked then unchecked?
    – Viv91
    Jul 18, 2014 at 3:02
  • showValueFromRadioButtonToLCDNumber will be called every time you check or uncheck radiobutton. So you can set LCD if self.radioButton_8.isChecked() and revert it otherwise. You can simply clear it or you will need some logic to save previous value of LCD if you want to show previous value.
    – Aleksandar
    Jul 18, 2014 at 6:24
  • if you have problems doing it, post it as a new question
    – Aleksandar
    Jul 18, 2014 at 7:41

1 Answer 1

1

Do you mean text value next to radioButton? if so, here is solution:

def radiobutton8(self):
    if self.radioButton_8.isChecked():
        self.radioButton_8.setText("02")

EDIT1: radioButton doesn't have value field as you need. However, you can write your own radiobButton class that inherits original QRadioButton, and add that field. Example:

class MyRadioButton(QtGui.QRadioButton):
    def __init__(self):
        super(MyRadioButton, self).__init__()
        self.value = None

    def SetValue(self, val):
        self.value = val

    def GetValue(self):
        return self.value

and use it like this:

self.radioButton_8 = MyRadioButton()
self.radioButton_8.setText("some text")
...
def radiobutton8(self):
    if self.radioButton_8.isChecked():
        self.radioButton_8.SetValue("02")# or .SetValue(2) if you want it to be integer

EDIT2

# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import Qt

class MyRadioButton(QtGui.QRadioButton):
    def __init__(self):
        super(MyRadioButton, self).__init__()
        self.value = None

    def SetValue(self, val):
        self.value = val

    def GetValue(self):
        return self.value

class Widget(QtGui.QWidget):
    def __init__(self):
        super(Widget, self).__init__()
        self.layout = QtGui.QVBoxLayout(self)

        self.pushButton_7 = QtGui.QPushButton("if RB is checked, show it's value to LCD")
        self.pushButton_7.setGeometry(QtCore.QRect(220, 650, 75, 23))
        self.pushButton_7.clicked.connect(self.showValueFromRadioButtonToLCDNumber)

        self.radioButton = MyRadioButton()
        self.radioButton.setText("some text")
        self.radioButton.SetValue("02")# somewhere in code first set value to radio button

        self.lcdNumber = QtGui.QLCDNumber()

        self.layout.addWidget(self.pushButton_7)
        self.layout.addWidget(self.radioButton)
        self.layout.addWidget(self.lcdNumber)

    def showValueFromRadioButtonToLCDNumber(self):
        value = self.radioButton.GetValue()
        if self.radioButton.isChecked():
            self.lcdNumber.display(value)

if __name__ == '__main__':
    app = QtGui.QApplication([])
    w = Widget()
    w.show()
    sys.exit(app.exec_())
5
  • No, what I mean is I want to add a value to the radio button like a function. I want the value to appear on LcdNumber widget to show the number '02' when I check the radio button. Any solution for this?
    – Viv91
    Jul 16, 2014 at 9:51
  • take a look at Edit in my answer
    – Aleksandar
    Jul 16, 2014 at 10:23
  • first you need to set value of radio button in order to read it with GetValue(). Take a look at working example in EDIT2 in my answer. As for setText, I don't know what your _translate method do
    – Aleksandar
    Jul 17, 2014 at 7:11
  • The _translate is just a command generated by PyQt4 when i generated the Python script after dragging and dropping the widgets on my UI. I tried your suggestion but there is no need for a pushbutton. Instead, I edited mine to become like this shown above.
    – Viv91
    Jul 17, 2014 at 7:47
  • setText method needs string (QString) as input parametar, so ensure that _translate returns that type
    – Aleksandar
    Jul 17, 2014 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.