2014-07-16 5 views
1

라디오 버튼을 선택했을 때 값을 지정하고 싶습니다.pyqt의 라디오 버튼에 값을 지정하는 방법은 무엇입니까?

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

그러나 이것은 작동하지 않습니다. 어떤 해결책?

은 UPDATE : 내 코드로 편집 한 :

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) 

그러나, 라디오 버튼에 대한 내 원래의 텍스트 'A1'은 이제없는 및 검사 때 내 번호는 여전히 내 LCD에 표시되지 않습니다. 왜 그런가?

업데이트 : 그런 뭔가 내 코드를 편집 :

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) 

을 그리고 지금은이 오류가 : 나는 또한 당신이 나에게 주어지고있는 코드를 시도

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' 

을 그런 오류는 없지만 코드의 LCD에 값 02가 표시되지 않습니다.

+0

은 : setValue의하지 SetValue는 될 것으로 생각된다. 코드 당신이 그것을 보여주기를 원하면 코드 2를 보여 주겠다. 02 : 그 값을'self.radioButton_8.SetValue (02)'대신에'self.radioButton_8.SetValue ("02")'문자열로 설정하라. 내가 EDIT2에서 내 대답에 업데이 트했습니다,하지만 당신은 내가 EDIT1에서 언급 한 적이 있다면 – Aleksandar

+0

EDIT2에서 코드를 실행하려고 시도하고 그것이 self.radioButton.SetValue ("02")인지 확인했지만 여전히 작동하지 않습니다. 라디오 버튼을 선택하면 아무 반응이 없습니다. – Viv91

+0

오! 나는이 줄'self.pushButton_7.clicked.connect (self.showValueFromRadioButtonToLCDNumber)'를 삭제하고'self.radioButton.toggled.connect (self.showValueFromRadioButtonToLCDNumber)'를 추가했다. 그러나 라디오 버튼을 선택하지 않은 상태에서 체크 표시를 해제하면 LCD 디스플레이를 되돌릴 수 있습니까? – Viv91

답변

1

radioButton 옆에 텍스트 값을 의미합니까?

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

EDIT1 : 그렇다면, 여기에 솔루션입니다 당신이 필요로 라디오 버튼 값 필드가 없습니다. 그러나 원래 QRadioButton을 상속 한 자체 radiobButton 클래스를 작성하고 해당 필드를 추가 할 수 있습니다. 예 :

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 

과 같이 사용 : 두 번째 업데이트 오류에 대한

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_()) 
+0

아니요, 제가 말하고자하는 것은 함수와 같은 라디오 버튼에 값을 추가하고 싶다는 것입니다. 라디오 버튼을 체크했을 때 LcdNumber 위젯에 값이 '02'로 나타나길 원합니다. 이것에 대한 해결책은 무엇입니까? – Viv91

+0

편집 내 대답 – Aleksandar

+0

에서 먼저 GetValue()'로 그것을 읽으려면 라디오 버튼의 값을 설정해야 편집보십시오. 내 대답은 ** EDIT2 **의 작동 예제를 참조하십시오.setText에 관해서는, 당신의'_translate' 메쏘드가 무엇인지 모른다. – Aleksandar

관련 문제