1

Is there anyway I can make pyqt lcd display the total value of the radio buttons that are checked? Each radio button is assigned one value and checking some or all of them will add up the total value and display it in the lcd. Unchecking them will then subtract their respective values from the lcd display. The code I have only works for one radio button. It does not work for multiple radio buttons. Is there any way to make the code work for multiple radio buttons? Here is my code:

import sys
from PyQt4 import QtCore, QtGui

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 __init__(self, parent = None):
        super(UserTool, self).__init__()
        self.layoutVertical = QtGui.QVBoxLayout(self)
        self.layout = QtGui.QHBoxLayout(self)
        self.layout.setContentsMargins(0, 0, 0, 0)
        self.layout.setSpacing(5)
        self.layout.setAlignment(QtCore.Qt.AlignTop)
        self.setup(self)        

    def setup(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        self.resize(688, 677)

        self.lcdNumber = QtGui.QLCDNumber(Dialog)
        self.lcdNumber.setGeometry(QtCore.QRect(590, 30, 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_2 = MyRadioButton()
        self.layout.addWidget(self.radioButton_2)
        self.radioButton_2.setText("A7")
        self.radioButton_2.SetValue("80")
        self.radioButton_2.toggled.connect(self.showValue)
        #self.radioButton_2 = QtGui.QRadioButton(Dialog)
        #self.radioButton_2.setGeometry(QtCore.QRect(160, 10, 82, 17))
        self.radioButton_2.setChecked(False)
        self.radioButton_2.setAutoExclusive(False)
        self.radioButton_2.setObjectName(_fromUtf8("radioButton_2"))

        self.radioButton_3 = MyRadioButton()
        self.layout.addWidget(self.radioButton_3)
        self.radioButton_3.setText("A6")
        self.radioButton_3.SetValue("40")
        self.radioButton_3.toggled.connect(self.showValue)
        #self.radioButton_3 = QtGui.QRadioButton(Dialog)
        #self.radioButton_3.setGeometry(QtCore.QRect(210, 10, 81, 17))
        self.radioButton_3.setChecked(False)
        self.radioButton_3.setAutoExclusive(False)
        self.radioButton_3.setObjectName(_fromUtf8("radioButton_3"))

        self.radioButton_4 = MyRadioButton()
        self.layout.addWidget(self.radioButton_4)
        self.radioButton_4.setText("A5")
        self.radioButton_4.SetValue("20")
        self.radioButton_4.toggled.connect(self.showValue)
       #self.radioButton_4 = QtGui.QRadioButton(Dialog)
       #self.radioButton_4.setGeometry(QtCore.QRect(260, 10, 41, 17))
        self.radioButton_4.setChecked(False)
        self.radioButton_4.setAutoExclusive(False)
        self.radioButton_4.setObjectName(_fromUtf8("radioButton_4"))

        self.radioButton_5 = MyRadioButton()
        self.layout.addWidget(self.radioButton_5)
        self.radioButton_5.setText("A4")
        self.radioButton_5.SetValue("10")
        self.radioButton_5.toggled.connect(self.showValue)
       #self.radioButton_5 = QtGui.QRadioButton(Dialog)
       #self.radioButton_5.setGeometry(QtCore.QRect(310, 10, 82, 17))
        self.radioButton_5.setChecked(False)
        self.radioButton_5.setAutoExclusive(False)
        self.radioButton_5.setObjectName(_fromUtf8("radioButton_5"))

        self.radioButton_6 = MyRadioButton()
        self.layout.addWidget(self.radioButton_6)
        self.radioButton_6.setText("A3")
        self.radioButton_6.SetValue("08")
        self.radioButton_6.toggled.connect(self.showValue)
       #self.radioButton_6 = QtGui.QRadioButton(Dialog)
       #self.radioButton_6.setGeometry(QtCore.QRect(360, 10, 82, 17))
        self.radioButton_6.setChecked(False)
        self.radioButton_6.setAutoExclusive(False)
        self.radioButton_6.setObjectName(_fromUtf8("radioButton_6"))


        self.radioButton_7 = MyRadioButton()
        self.layout.addWidget(self.radioButton_7)
        self.radioButton_7.setText("A2")
        self.radioButton_7.SetValue("04")
        self.radioButton_7.toggled.connect(self.showValue)
       #self.radioButton_7 = QtGui.QRadioButton(Dialog)
       #self.radioButton_7.setGeometry(QtCore.QRect(410, 10, 82, 17))
        self.radioButton_7.setChecked(False)
        self.radioButton_7.setAutoExclusive(False)
        self.radioButton_7.setObjectName(_fromUtf8("radioButton_7"))     

        self.radioButton_8 = MyRadioButton()
        self.layout.addWidget(self.radioButton_8)
        self.radioButton_8.setText("A1")
        self.radioButton_8.SetValue("02")
        self.radioButton_8.toggled.connect(self.showValue)
        #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.layoutVertical.addLayout(self.layout)
        self.layout.addWidget(self.lcdNumber)
        self.previousValue = ""

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)             

    def showValue(self):
        #I tried to get it working for 2 radio buttons first but could not.
        value7 = self.radioButton_7.GetValue()
        value8 = self.radioButton_8.GetValue()
        if self.radioButton_7.isChecked():
            self.previousValue = self.lcdNumber.value()
            self.lcdNumber.display(value7) 
        else:
            self.lcdNumber.display(self.previousValue)

        if self.radioButton_8.isChecked():
            self.previousValue = self.lcdNumber.value()
            self.lcdNumber.display(value8)
        else:
            self.lcdNumber.display(self.previousValue)
Viv91
  • 45
  • 2
  • 8

1 Answers1

1

Since you want to do math (add value, subtract value) it is easier for you to set values of your radio buttons as integers (not string), so I've changed that. Also I've changed self.previousValue = "" to self.lcdValue = 0 and showValue method:

class UserTool(QtGui.QDialog):
    def __init__(self, parent = None):
        super(UserTool, self).__init__()
        self.layoutVertical = QtGui.QVBoxLayout(self)
        self.layout = QtGui.QHBoxLayout(self)
        self.layout.setContentsMargins(0, 0, 0, 0)
        self.layout.setSpacing(5)
        self.layout.setAlignment(QtCore.Qt.AlignTop)
        self.setup(self)        

    def setup(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        self.resize(688, 677)

        self.lcdNumber = QtGui.QLCDNumber(Dialog)
        self.lcdNumber.setGeometry(QtCore.QRect(590, 30, 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_2 = MyRadioButton()
        self.layout.addWidget(self.radioButton_2)
        self.radioButton_2.setText("A7")
        self.radioButton_2.SetValue(80)
        self.radioButton_2.toggled.connect(self.showValue)
        #self.radioButton_2 = QtGui.QRadioButton(Dialog)
        #self.radioButton_2.setGeometry(QtCore.QRect(160, 10, 82, 17))
        self.radioButton_2.setChecked(False)
        self.radioButton_2.setAutoExclusive(False)
        self.radioButton_2.setObjectName(_fromUtf8("radioButton_2"))

        self.radioButton_3 = MyRadioButton()
        self.layout.addWidget(self.radioButton_3)
        self.radioButton_3.setText("A6")
        self.radioButton_3.SetValue(40)
        self.radioButton_3.toggled.connect(self.showValue)
        #self.radioButton_3 = QtGui.QRadioButton(Dialog)
        #self.radioButton_3.setGeometry(QtCore.QRect(210, 10, 81, 17))
        self.radioButton_3.setChecked(False)
        self.radioButton_3.setAutoExclusive(False)
        self.radioButton_3.setObjectName(_fromUtf8("radioButton_3"))

        self.radioButton_4 = MyRadioButton()
        self.layout.addWidget(self.radioButton_4)
        self.radioButton_4.setText("A5")
        self.radioButton_4.SetValue(20)
        self.radioButton_4.toggled.connect(self.showValue)
       #self.radioButton_4 = QtGui.QRadioButton(Dialog)
       #self.radioButton_4.setGeometry(QtCore.QRect(260, 10, 41, 17))
        self.radioButton_4.setChecked(False)
        self.radioButton_4.setAutoExclusive(False)
        self.radioButton_4.setObjectName(_fromUtf8("radioButton_4"))

        self.radioButton_5 = MyRadioButton()
        self.layout.addWidget(self.radioButton_5)
        self.radioButton_5.setText("A4")
        self.radioButton_5.SetValue(10)
        self.radioButton_5.toggled.connect(self.showValue)
       #self.radioButton_5 = QtGui.QRadioButton(Dialog)
       #self.radioButton_5.setGeometry(QtCore.QRect(310, 10, 82, 17))
        self.radioButton_5.setChecked(False)
        self.radioButton_5.setAutoExclusive(False)
        self.radioButton_5.setObjectName(_fromUtf8("radioButton_5"))

        self.radioButton_6 = MyRadioButton()
        self.layout.addWidget(self.radioButton_6)
        self.radioButton_6.setText("A3")
        self.radioButton_6.SetValue(8)
        self.radioButton_6.toggled.connect(self.showValue)
       #self.radioButton_6 = QtGui.QRadioButton(Dialog)
       #self.radioButton_6.setGeometry(QtCore.QRect(360, 10, 82, 17))
        self.radioButton_6.setChecked(False)
        self.radioButton_6.setAutoExclusive(False)
        self.radioButton_6.setObjectName(_fromUtf8("radioButton_6"))


        self.radioButton_7 = MyRadioButton()
        self.layout.addWidget(self.radioButton_7)
        self.radioButton_7.setText("A2")
        self.radioButton_7.SetValue(4)
        self.radioButton_7.toggled.connect(self.showValue)
       #self.radioButton_7 = QtGui.QRadioButton(Dialog)
       #self.radioButton_7.setGeometry(QtCore.QRect(410, 10, 82, 17))
        self.radioButton_7.setChecked(False)
        self.radioButton_7.setAutoExclusive(False)
        self.radioButton_7.setObjectName(_fromUtf8("radioButton_7"))     

        self.radioButton_8 = MyRadioButton()
        self.layout.addWidget(self.radioButton_8)
        self.radioButton_8.setText("A1")
        self.radioButton_8.SetValue(2)
        self.radioButton_8.toggled.connect(self.showValue)
        #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.layoutVertical.addLayout(self.layout)
        self.layout.addWidget(self.lcdNumber)
        self.lcdValue = 0

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)  


    def showValue(self):
        sender = self.sender()
        if sender.isChecked():
            self.lcdValue += sender.GetValue()
            self.lcdNumber.display(self.lcdValue) 
        else:
            self.lcdValue -= sender.GetValue()
            self.lcdNumber.display(self.lcdValue)
Aleksandar
  • 3,541
  • 4
  • 34
  • 57
  • I got this error back: `Traceback (most recent call last): File "C:/Users/Vivien Phua/Documents/Python Scripts/Rs232.py", line 307, in showValue self.lcdValue += sender.GetValue() TypeError: unsupported operand type(s) for +=: 'int' and 'str' Traceback (most recent call last): File "C:/Users/Vivien Phua/Documents/Python Scripts/Rs232.py", line 310, in showValue self.lcdValue -= sender.GetValue() TypeError: unsupported operand type(s) for -=: 'int' and 'str'` – Viv91 Jul 21 '14 at 09:12
  • you did dot changed type to int as I've told in my answer. on every place where you set value to radio button set integer, not string. For example: instead of `self.radioButton_2.SetValue("80")` , set it like this: `self.radioButton_2.SetValue(80)` – Aleksandar Jul 21 '14 at 09:15
  • ohh sorry I missed that out. Yes its now working, thanks so much! – Viv91 Jul 21 '14 at 09:20
  • if you need your lcd to display zeros in front of number change this line `self.lcdNumber.display(self.lcdValue)` to `self.lcdNumber.display("000000" + str(self.lcdValue))` in both places in showValue method – Aleksandar Jul 21 '14 at 09:22