无论如何我可以让pyqt lcd显示被检查的单选按钮的总值吗?每个单选按钮都分配了一个值,并检查其中的一些或全部将累加总值并将其显示在lcd中。取消选中它们将从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)
答案 0 :(得分: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)