如何从pyqt5中的单选按钮获取选中的组单选的ID

时间:2019-02-01 11:54:58

标签: pyqt5 python-3.6

有没有办法获得分组单选按钮的价值? 此代码对我不起作用:

self.pb1.clicked.connect(lambda : self.rbtn_1_state(self.rb1_1.ischecked(),self.rb1_2.ischecked()))

def rbtn_1_state(self, rb1_1_chk,rb1_2_chk):
    print("radio button 1 function is called")
    if rb1_1_chk:
        rb1_state=[1,0]
    if rb1_2_chk:
        rb1_state = [0, 1]
    else:
        rb1_state = [0, 0]

1 个答案:

答案 0 :(得分:0)

尝试一下:

from PyQt5 import QtGui, QtCore, QtWidgets

class Window(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.groupBox = QtWidgets.QGroupBox() 

        self.rb1_1 = QtWidgets.QRadioButton("rb1_1", self.groupBox)
        self.rb1_1.setChecked(True)
        self.rb1_2 = QtWidgets.QRadioButton("rb1_2", self.groupBox)

        self.hLayout = QtWidgets.QHBoxLayout()
        self.hLayout.addWidget(self.rb1_1)
        self.hLayout.addWidget(self.rb1_2)

        self.lb1 = QtWidgets.QLabel()

        self.pb1 = QtWidgets.QPushButton("pb1")
        self.pb1.clicked.connect(lambda : self.rbtn_1_state(
                                      self.rb1_1.isChecked(),
                                      self.rb1_2.isChecked()))

        self.vLayout = QtWidgets.QVBoxLayout(self)
        self.vLayout.addWidget(self.lb1)
        self.vLayout.addLayout(self.hLayout)
        self.vLayout.addWidget(self.pb1)

    def rbtn_1_state(self, rb1_1_chk, rb1_2_chk):
        self.lb1.setText("rb1_1-> {} , rb1_2-> {}".format(rb1_1_chk, rb1_2_chk))


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    qt_app = Window()
    qt_app.show()
    sys.exit(app.exec_())

enter image description here