Как добавить значение из нескольких переключателей в ЖК-PyQT?

Можно ли в любом случае заставить pyqt lcd отображать общее значение переключателей, которые проверены? Каждой радио-кнопке присваивается одно значение, и проверка некоторых или всех из них суммирует общее значение и отображает его на ЖК-дисплее. Снятие отметки с них затем вычитает их соответствующие значения из ЖК-дисплея. Код у меня работает только для одной радиокнопки. Это не работает для нескольких переключателей. Есть ли способ заставить код работать для нескольких переключателей? Вот мой код:

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)

1 ответ

Решение

Так как вы хотите сделать математику (добавить значение, вычесть значение), вам будет проще установить значения ваших радиокнопок в виде целых чисел (не строк), поэтому я изменил это. Также я изменился self.previousValue = "" в self.lcdValue = 0 а также showValue метод:

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)
Другие вопросы по тегам