取消选中单选按钮 - PyQt4 [英] uncheck radiobutton - PyQt4

查看:83
本文介绍了取消选中单选按钮 - PyQt4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此代码示例中:

from PyQt4.QtGui import QDialog, QPushButton, QRadioButton, QHBoxLayout, QApplication, QButtonGroup
import sys

class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent=None)

        button = QPushButton('Button')
        self.radiobutton1 = QRadioButton('1')
        self.radiobutton2 = QRadioButton('2')
        #self.group = QButtonGroup()
        #self.group.addButton(self.radiobutton1)
        #self.group.addButton(self.radiobutton2)       
        #self.group.setExclusive(False)

        layout = QHBoxLayout()
        layout.addWidget(button)
        layout.addWidget(self.radiobutton1)
        layout.addWidget(self.radiobutton2)
        self.setLayout(layout)

        button.clicked.connect(self.my_method)

    def my_method(self):
        self.radiobutton1.setChecked(False)
        self.radiobutton2.setChecked(False)

app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

当按钮被点击时,我希望选中的单选按钮未被选中,但这从未发生过.如果我取消注释注释行并运行代码,那么我可以取消选中单选按钮.但是出现了另一个问题.因为该组不是独占的,所以我可以将两个单选按钮设置为检查不得发生的事情.

When the button clicked I expect the selected radioButton to be unchecked, but that never happens. If I uncomment the comment lines and run the code, then I can uncheck radioButtons. But another problem occurs. Because the group is not exclusive, I can set both radioButtons checked something that must not happens.

我应该怎么做才能在一次只能选择一个按钮的情况下取消选中按钮?

What should I do to be able to unckeck the buttons while only one button at a time can be selected?

推荐答案

这感觉像是作弊,但确实有效:

This feels like cheating, but it works:

import sys
import PyQt4.QtGui as QtGui

class Form(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        button = QtGui.QPushButton('Button')
        button.clicked.connect(self.my_method)

        self.radiobutton1 = QtGui.QRadioButton('1')
        self.radiobutton2 = QtGui.QRadioButton('2')

        layout = QtGui.QHBoxLayout()
        layout.addWidget(button)
        layout.addWidget(self.radiobutton1)
        layout.addWidget(self.radiobutton2)
        self.setLayout(layout)

        self.group = QtGui.QButtonGroup()
        self.group.addButton(self.radiobutton1)
        self.group.addButton(self.radiobutton2)       

    def my_method(self):
        self.group.setExclusive(False)        
        self.radiobutton1.setChecked(False)
        self.radiobutton2.setChecked(False)
        self.group.setExclusive(True)

app = QtGui.QApplication(sys.argv)
form = Form()
form.show()
app.exec_()

正如您所指出的,当设置 self.group.setExclusive(False) 时,您可以取消切换两个单选按钮.

As you've pointed out, when self.group.setExclusive(False) is set, you can untoggle both radio buttons.

self.group.setExclusive(True)时,只能设置一个单选按钮.

And when self.group.setExclusive(True), only one radio button can be set.

所以 my_method 只需调用 self.group.setExclusive(False) 以便它可以取消设置两个单选按钮,然后重置 self.group.setExclusive(True).

So my_method simply calls self.group.setExclusive(False) so it can unset both radio buttons, then resets self.group.setExclusive(True).

附注.我认为 parent 不应该在这一行设置为 None :

PS. I think parent should not be set to None on this line:

super(Form, self).__init__(parent = None)

因为如果一个非平凡的 parent 被发送到 Form,你可能希望将该 parent 传递给 QDialog.__init__.

since if a non-trivial parent is sent to Form, you would probably want to pass that parent on to QDialog.__init__.

这篇关于取消选中单选按钮 - PyQt4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆