如何在pyQT中对按钮进行分组

时间:2014-05-29 02:49:00

标签: python pyqt

我是QT的新手,想找到一种将按钮组合在一起的方法

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

# how would I group the button together

我尝试搜索PyQt文档但找不到分组信息

任何方向都将受到赞赏

4 个答案:

答案 0 :(得分:1)

使用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)

site可能会有所帮助。

答案 1 :(得分:1)

这是一个建议,创建一个这样的类:

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)

这应该提供您想要的功能。您可以随意扩展课程。

编辑:与PySide或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_()

答案 2 :(得分:0)

您应该使用布局。 这里是关于分组按钮的好视频教程。Click

它适用于C ++,但重写代码非常简单。 在Python中它看起来像:

self.hbox = QtGui.QHBoxLayout()
self.hbox.addWidget(self.button1) 
self.hbox.addWidget(self.button2) 

答案 3 :(得分:0)

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())