我想问两个关于我的代码的问题。首先,如何将我的单选按钮的位置移动到我想要的位置(我在代码中写了它)?取消选中后如何反转我的LCD屏幕?现在,它显示了' 02'如果选中,但我想在取消选中时反转该过程。任何解决方案?
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.layout = QtGui.QVBoxLayout(self)
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, 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 = MyRadioButton()
self.radioButton_8.setText("A1")
self.radioButton_8.SetValue("02")
self.radioButton_8.toggled.connect(self.showValueFromRadioButtonToLCDNumber)
self.layout.addWidget(self.radioButton_8)
#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"))
def retranslateUi(self, Dialog):
self.radioButton_8.setText(_translate("Dialog", "A1", None))
def showValueFromRadioButtonToLCDNumber(self):
value = self.radioButton_8.GetValue()
if self.radioButton_8.isChecked():
self.lcdNumber.display(value)
答案 0 :(得分:0)
这是一个有效的例子。为了显示以前的值,您需要在更改之前兑现该值的机制。要转移radioButton我使用addSpacing()
# -*- 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.radioButton = MyRadioButton()
self.radioButton.setText("some text")
self.radioButton.SetValue("02")
self.radioButton.toggled.connect(self.showValueFromRadioButtonToLCDNumber)
self.lcdNumber = QtGui.QLCDNumber()
self.lcdNumber.display("-2")# previous value
self.layoutHorizontal = QtGui.QHBoxLayout(self)
self.layoutHorizontal.addSpacing(20)# add space before radioButton
self.layoutHorizontal.addWidget(self.radioButton)
self.layout.addLayout(self.layoutHorizontal)
self.layout.addWidget(self.lcdNumber)
self.previousValue = ""
def showValueFromRadioButtonToLCDNumber(self):
value = self.radioButton.GetValue()
if self.radioButton.isChecked():
self.previousValue = self.lcdNumber.value()# save previous value before it is changed
self.lcdNumber.display(value)
else:
self.lcdNumber.display(self.previousValue)