python - 为什么 PyQt 中的这段代码允许我不选中所有单选按钮?

标签 python pyqt pyqt5 qradiobutton

我使用 PyQt 编写了一个迷你代码,当我注意到它允许我不选中左侧的所有按钮时,我感到很惊讶。我不想要这种行为:应该始终选择其中之一。不过,窗口的右侧可以工作。我真的不知道为什么。代码如下:

from PyQt5 import QtWidgets, QtGui, QtCore

class Win(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(Win, self).__init__(parent)
        top_layout = QtWidgets.QHBoxLayout()

        top_layout_1 = QtWidgets.QHBoxLayout()
        self.thresh_btns = [QtWidgets.QRadioButton('L1'),
                            QtWidgets.QRadioButton('L2'),
                            QtWidgets.QRadioButton('L3')]
        self.thresh_btns[0].setChecked(True)
        for btn in self.thresh_btns:
            top_layout_1.addWidget(btn)

        timestamp_groupbox = QtWidgets.QGroupBox('Timestamp')
        top_layout_2 = QtWidgets.QVBoxLayout()
        self.timestamp_current = QtWidgets.QRadioButton('Current')
        self.timestamp_current.setChecked(True)
        self.timestamp_target = QtWidgets.QRadioButton('Last')
        top_layout_2.addWidget(self.timestamp_current)
        top_layout_2.addWidget(self.timestamp_target)
        timestamp_groupbox.setLayout(top_layout_2)

        top_layout.addLayout(top_layout_1)
        top_layout.addWidget(timestamp_groupbox)
        self.setLayout(top_layout)

if __name__ == '__main__':
   app = QtWidgets.QApplication([])
   ex = Win()
   ex.show()
   app.exec_()

最佳答案

如果你想要这种行为,你必须使用QButtonGroup:

from PyQt5 import QtCore, QtGui, QtWidgets

class Win(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(Win, self).__init__(parent)
        top_layout = QtWidgets.QHBoxLayout()

        top_layout_1 = QtWidgets.QHBoxLayout()
        self.thresh_btns = [QtWidgets.QRadioButton('L1'),
                            QtWidgets.QRadioButton('L2'),
                            QtWidgets.QRadioButton('L3')]
        group_button = QtWidgets.QButtonGroup(self) # <---
        self.thresh_btns[0].setChecked(True)
        for btn in self.thresh_btns:
            top_layout_1.addWidget(btn)
            group_button.addButton(btn)             # <---

        timestamp_groupbox = QtWidgets.QGroupBox('Timestamp')
        top_layout_2 = QtWidgets.QVBoxLayout()
        self.timestamp_current = QtWidgets.QRadioButton('Current')
        self.timestamp_current.setChecked(True)
        self.timestamp_target = QtWidgets.QRadioButton('Last')
        top_layout_2.addWidget(self.timestamp_current)
        top_layout_2.addWidget(self.timestamp_target)
        timestamp_groupbox.setLayout(top_layout_2)

        top_layout.addLayout(top_layout_1)
        top_layout.addWidget(timestamp_groupbox)
        self.setLayout(top_layout)

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

关于python - 为什么 PyQt 中的这段代码允许我不选中所有单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53091180/

相关文章:

python - 在文件中的某个点追加,每次增加一个值

python - PyQT5 OpenGL 4.1 核心配置文件 - 无效的帧缓冲区操作 - Mac OS

python - PyQt 将 QWidget 的方向从右向左更改

python - 将日期元组和列表转换为 Pandas Dataframe

python - 在 Python 中如何将文件名中的某些字符列入白名单?

python - 用于 pandas 数据帧时无法看到 drop_duplicates 的影响

python - QTreeWidget如何给子项添加复选框?

python - PyQT - 将文件复制到剪贴板

crash - pyQt5 中退出时出现段错误,但 pyQt4 中没有

javascript - 使用 PyQt5 和 QWebEngineView 抓取 javascript 页面