grouping radio buttons in PyQt

后端 未结 2 1929
孤独总比滥情好
孤独总比滥情好 2021-01-03 03:24
import sys
from PyQt4 import QtCore, QtGui

class Class1(QtGui.QMainWindow):
    def __init__(self):
        super(Class1, self).__init__()
        self.func()

             


        
2条回答
  •  佛祖请我去吃肉
    2021-01-03 03:38

    A QMainWindow provides a layout already, you can't simply replace that with your own. Either inherit from a plain QWidget, or create a new widget and add the layout and buttons to that.

    Your naming is confusing too, QButtonGroup isn't a layout. It doesn't actually provide any visible UI. If you need a UI element that groups buttons, you should look at QGroupBox instead.

    Here's a simple variation on what you have above:

    def func(self):
        layout=QtGui.QHBoxLayout()  # layout for the central widget
        widget=QtGui.QWidget(self)  # central widget
        widget.setLayout(layout)
    
        number_group=QtGui.QButtonGroup(widget) # Number group
        r0=QtGui.QRadioButton("0")
        number_group.addButton(r0)
        r1=QtGui.QRadioButton("1")
        number_group.addButton(r1)
        layout.addWidget(r0)
        layout.addWidget(r1)
    
        letter_group=QtGui.QButtonGroup(widget) # Letter group
        ra=QtGui.QRadioButton("a")
        letter_group.addButton(ra)
        rb=QtGui.QRadioButton("b")
        letter_group.addButton(rb)
        layout.addWidget(ra)
        layout.addWidget(rb)
    
        # assign the widget to the main window
        self.setCentralWidget(widget)
        self.show()
    

提交回复
热议问题