Search code examples
python-3.xpyqt4qradiobutton

Can i make a group radiobutton as a group of pressed button, not as a group of circles with one point? (PyQt4)


If I use tkinter, I can set the option indicatoron = 0, and get an expected effect.

This effect can be achieved with a group of QPushButton, and some additional code, I suppose.

But is it a true way? Maybe, PyQt has an option, as tkinter?

This code gave me an expected effect from tkinter.

from tkinter import *
root = Tk()
var = IntVar()
button1 = Radiobutton(root,indicatoron=0,text=' One Button ',variable=var,value=1)
button2 = Radiobutton(root,indicatoron=0,text=' Two Button ',variable=var,value=2)
button3 = Radiobutton(root,indicatoron=0,text='Three Button',variable=var,value=3)
button1.place(x=4, y=4)
button2.place(x=4, y=30)
button3.place(x=4, y=56)
mainloop()

Solution

  • In PyQt, you can use QPushButton and a QButtonGroup:

    from PyQt4 import QtCore, QtGui
    
    class Window(QtGui.QWidget):
        def __init__(self):
            super(Window, self).__init__()
            layout = QtGui.QVBoxLayout(self)
            self.buttonGroup = QtGui.QButtonGroup(self)
            for text in 'One Two Three'.split():
                button = QtGui.QPushButton(text)
                button.setCheckable(True)
                layout.addWidget(button)
                self.buttonGroup.addButton(button)
            self.buttonGroup.buttonClicked.connect(self.handleButtons)
    
        def handleButtons(self, button):
            print('Button %s Clicked' % button.text())
    
    if __name__ == '__main__':
    
        import sys
        app = QtGui.QApplication(sys.argv)
        window = Window()
        window.show()
        sys.exit(app.exec_())