简体   繁体   中英

How to group buttons in pyQT

I am new to QT and would like to find a way to group buttons together

self.button1 = QtGui.QPushButton("button1")
self.button2 = QtGui.QPushButton("button2")

# how would I group the button together

I tried searching on PyQt documentation but couldn't find the grouping information

Any direction would be appreciated

Use QDialogButtonBox :

    self.buttonBox = QtGui.QDialogButtonBox(Dialog)
    self.buttonBox.setGeometry(QtCore.QRect(30, 240, 341, 32))
    self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
    self.buttonBox.addButton(self.button1, QtGui.QDialogButtonBox.ActionRole)
    self.buttonBox.addButton(self.button2, QtGui.QDialogButtonBox.ActionRole)

This site may help.

Here is a suggestion, create a class like this:

class MyButtonGroup(QtCore.QObject):
    trigger = QtCore.pyqtSignal(bool)

    def addButton(self, button):
        button.clicked.connect(self.trigger.emit)

    def removeButton(self, button):
        button.clicked.disconnect(self.trigger.emit)

group = MyButtonGroup()
button1 = QtGui.QPushButton("button1")
button2 = QtGui.QPushButton("button2")
group.addButton(button1)
group.addButton(button2)
group.trigger.connect(do_something)

This should provide the functionality you wish. You can extend the class as you please.

EDIT: Minimilistic working example that works with PySide or PyQt4:

import sys

lower_argv = [s.lower() for s in sys.argv]
if 'pyside' in lower_argv:
    from PySide import QtCore, QtGui
    QtCore.pyqtSignal = QtCore.Signal
    QtCore.pyqtSlot = QtCore.Slot
    print 'Using PySide'
else:
    from PyQt4 import QtCore, QtGui
    print 'Using PyQt4'

class MyButtonGroup(QtCore.QObject):
    trigger = QtCore.pyqtSignal((),(bool,))

    def addButton(self, button):
        button.clicked.connect(self.trigger.emit)

    def removeButton(self, button):
        button.clicked.disconnect(self.trigger.emit)


class MyWindow(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self, None)

        self.group = MyButtonGroup()
        button1 = QtGui.QPushButton("button1")
        button2 = QtGui.QPushButton("button2")
        self.group.addButton(button1)
        self.group.addButton(button2)
        self.group.trigger.connect(self.do_something)

        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(button1)
        layout.addWidget(button2)

    def do_something(self, x=False):
        print 'hello'


if __name__ == '__main__':
    app = QtGui.QApplication([])
    window = MyWindow()
    window.show()
    app.exec_()

You should use layouts. Here good video tutorial about grouping buttons. Click

It's for C++ but rewrite code is very easy. In Python it will look like:

self.hbox = QtGui.QHBoxLayout()
self.hbox.addWidget(self.button1) 
self.hbox.addWidget(self.button2) 
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QLabel, QPushButton,QButtonGroup
import sys
from PyQt5 import QtCore
 
 
 
class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.title = "PyQt5 QButton Group"
        self.top = 200
        self.left = 500
        self.width = 400
        self.height = 300
        self.setWindowTitle(self.title)
        self.setWindowIcon(QtGui.QIcon("icon.png"))
        self.setGeometry(self.left, self.top, self.width, self.height)
        hbox = QHBoxLayout()
        self.label = QLabel(self)
        self.label.setFont(QtGui.QFont("Sanserif", 15))
        hbox.addWidget(self.label)
        self.buttongroup = QButtonGroup()
        #self.buttongroup.setExclusive(False)
        self.buttongroup.buttonClicked[int].connect(self.on_button_clicked)
        button1 = QPushButton("Python")
        self.buttongroup.addButton(button1, 1)
        button1.setFont(QtGui.QFont("Sanserif", 15))
        button1.setIcon(QtGui.QIcon("pythonicon.png"))
        button1.setIconSize(QtCore.QSize(40, 40))
        hbox.addWidget(button1)

        button2 = QPushButton("Java")
        self.buttongroup.addButton(button2, 2)
        button2.setFont(QtGui.QFont("Sanserif", 15))
        button2.setIcon(QtGui.QIcon("java.png"))
        button2.setIconSize(QtCore.QSize(40,40))
        hbox.addWidget(button2)

        button3 = QPushButton("C++")
        self.buttongroup.addButton(button3, 3)
        button3.setFont(QtGui.QFont("Sanserif", 15))
        button3.setIcon(QtGui.QIcon("cpp.png"))
        button3.setIconSize(QtCore.QSize(40, 40))
        hbox.addWidget(button3)
        self.setLayout(hbox)
        self.show()
 
 
    def on_button_clicked(self, id):
        # for button in self.buttongroup.buttons():
        if self.buttongroup.button(id == 1):
            self.label.setText(self.btn2()+ " " + self.btn3() + " Was Clicked ")
            
        elif self.buttongroup.button(id == 2):
            self.label.setText(self.btn1()+ " " + self.btn3() + " Was Clicked ")
            
        elif self.buttongroup.button(id == 3):
            self.label.setText(self.btn2()+ " " + self.btn1() + " Was Clicked ")
           

    def btn1(self):
        global n1

        n1 = 'Python clicked'
        sending_btn = self.buttongroup.sender()
        if (sending_btn== "Java"):
            n1 = n1 + n2
        elif (sending_btn== "C++"):
            n1 = n1 + n3
        return n1

    def btn2(self):
        global n2
        n2 = "Java clicked"
        sending_btn = self.buttongroup.sender()
        if (sending_btn== "Python"):
            n2 = n2 + n1
        elif (sending_btn== "C++"):
            n2 = n2 + n3
        return n2

    def btn3(self):
        global n3
        n3 = "C++ clicked"
        sending_btn = self.buttongroup.sender()
        if (sending_btn== "Python"):
            n3 = n3 + n1
        elif (sending_btn== "Java"):
            n3 = n3 + n2
        return n3
 
if __name__ == "__main__":
    App = QApplication(sys.argv)
    window = Window()
    sys.exit(App.exec())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM