Python 多个单选按钮组正在链接,导致奇怪的行为
我的程序根据SQL数据动态生成表单。我制作了两个单选按钮,旁边有一个QLineEdit条目。选中右侧的单选按钮后,QLineEdit将正确启用。问题来自单选按钮之间的链接并导致它们之间的独占操作。当程序启动时,它们如下所示: 然后,当我单击第一个“否”时,它会改变我期望的方式并启用QLineEdit。 现在我还想单击“Serial#:RC1”的“No”。这就是行为开始出错的地方。 单击“否”按钮并取消选择上面的所有行 如果我再次尝试单击顶行上的“否”,则第二行上的“是”将取消选择 最后,我可以单击所选单选按钮并取消选择所有内容,直到剩下一个活动单选按钮。在这一点上,我不能有更多的选择按钮,只有这一个。单击取消选择的按钮将激活该按钮,并取消选择以前激活的按钮 我从将单选按钮放入QButtonGroup的助手函数中动态生成按钮。我以为这足以阻止这种行为,但我错了。 我希望每行上的单选按钮不响应其他行上其他单选按钮的操作Python 多个单选按钮组正在链接,导致奇怪的行为,python,pyqt5,python-3.7,qradiobutton,qbuttongroup,Python,Pyqt5,Python 3.7,Qradiobutton,Qbuttongroup,我的程序根据SQL数据动态生成表单。我制作了两个单选按钮,旁边有一个QLineEdit条目。选中右侧的单选按钮后,QLineEdit将正确启用。问题来自单选按钮之间的链接并导致它们之间的独占操作。当程序启动时,它们如下所示: 然后,当我单击第一个“否”时,它会改变我期望的方式并启用QLineEdit。 现在我还想单击“Serial#:RC1”的“No”。这就是行为开始出错的地方。 单击“否”按钮并取消选择上面的所有行 如果我再次尝试单击顶行上的“否”,则第二行上的“是”将取消选择 最后,
# !/user/bin/env python
import os
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class Radio(QDialog):
def __init__(self, app):
super(Radio, self).__init__()
self.bundle_dir = os.path.dirname(__file__)
gui_path = os.path.join(self.bundle_dir, 'ui', 'radio_bt_test.ui')
self.ui = uic.loadUi(gui_path, self)
self.num_of_row = 4
self.formLayout = self.findChild(QFormLayout, "formLayout")
self.radio_bt_lineEdit_connections = dict() # to help link the radio buttons and lineEdit
self.add_rows()
self.show()
def add_rows(self):
"""
Adds pairs of radio buttons with a lineEdit to each row of the form layout
:return:
"""
for i in range(self.num_of_row):
lbl = QLabel("Label#" + str(i))
hbox = QHBoxLayout()
buttons = self.new_radio_pair()
entry = self.new_entry("Value if No")
entry.setEnabled(False)
self.radio_bt_lineEdit_connections[buttons[-1]] = entry
# adding connection to dictionary for later event handling
buttons[-1].toggled.connect(self.radio_bt_changed)
for button in buttons:
hbox.addWidget(button)
hbox.addWidget(entry)
self.formLayout.addRow(lbl, hbox)
def new_radio_pair(self, texts=('Yes', 'No')) -> list:
"""
Makes a pair of radio buttons in a button group for creating data entries in "Part" grouping on the fly
:param texts: The texts that will go on the two buttons. The more texts that are added to make more radio buttons
:return: A list with QRadioButtons that are all part of the same QButtonGroup
"""
group = QButtonGroup()
buttons = []
for text in texts:
bt = QRadioButton(text)
bt.setFont(QFont('Roboto', 11))
if text == texts[0]:
bt.setChecked(True)
group.addButton(bt)
buttons.append(bt)
return buttons
def radio_bt_changed(self) -> None:
"""
Helps the anonymous radio buttons link to the anonymous QLineEdits that are made for data fields
:return: None
"""
sender = self.sender()
assert isinstance(sender, QRadioButton)
lineEdit = self.radio_bt_lineEdit_connections[sender]
assert isinstance(lineEdit, QLineEdit)
if sender.isChecked():
lineEdit.setEnabled(True)
else:
lineEdit.setEnabled(False)
lineEdit.clear()
def new_entry(self, placeholder_text: str = "") -> QLineEdit:
"""
Makes a new QLineEdit object for creating data entries in "Part" grouping on the fly
:return: A new QLineEdit with appropriate font and size policy
"""
entry = QLineEdit()
entry.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
entry.setFont(QFont('Roboto', 11))
entry.setStyleSheet("background-color: rgb(239, 241, 243);")
# with style sheets, past anything in here between the css tags
entry.setMaxLength(15)
entry.setPlaceholderText(placeholder_text)
return entry
def main():
app = QApplication(sys.argv)
radio = Radio(app)
sys.exit(app.exec())
main()
是不是因为我声明了QButtonGroup,然后忘记了它们?垃圾收集器来清除它们是因为我没有分配变量,还是因为我遗漏了另一个问题
ui是在QtDesigner上设计的,它只是一个带有表单布局的对话框
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>491</width>
<height>382</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(219, 221, 223);</string>
</property>
<widget class="QWidget" name="formLayoutWidget">
<property name="geometry">
<rect>
<x>80</x>
<y>40</y>
<width>301</width>
<height>291</height>
</rect>
</property>
<layout class="QFormLayout" name="formLayout"/>
</widget>
</widget>
<resources/>
<connections/>
</ui>
对话
0
0
491
382
对话
背景色:rgb(219221223);
80
40
301
291
应用于使行按钮成为独占的对象是“group”,但这是一个局部变量,当新的单选对方法完成执行时,该变量将被销毁,导致它的行为与之前的想法不同
解决方案是延长生命周期,为此有几个选项,例如将其作为类的属性,将其添加到具有较长生命周期的容器或类中,或者在QObject的情况下,将另一个QObject作为具有较长生命周期的父对象传递给它(如self
),对于这种情况,这是最好的解决方案:
group = QButtonGroup(self)
group=QButtonGroup(self)
好的,我添加了允许任何人复制、粘贴和运行程序的代码。