如何通过按钮调整按钮图标的大小

时间:2019-06-13 19:00:25

标签: python pyqt pyqt5

我创建了一个自己的类,继承了QPushButton的“两个状态”按钮,因此单击时可以压缩。无论如何,我在主窗口中使用了水平布局来调整按钮的大小,但是当我在TwoStateButton上有一个图标而试图调整窗口大小时,却遇到了问题。该按钮本身可以正确调整大小,但其顶部的图标不能正确调整大小。我已经尝试过QResizeEvent,但不确定如何使用它。我从联机文档中复制了eventFilter方法,但该方法最初是用C ++编写的,因此当前存在一些语法错误。如何修复代码,以便按钮上的图标与按钮本身一起调整大小?

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtCore import QEvent
from PyQt5.Qt import QResizeEvent


class TwoStateButton(QtWidgets.QPushButton):

    def __init__(self, on, hover, parent=None):
        super(TwoStateButton, self).__init__(parent)
        self.setIcon(QtGui.QIcon(on))
        self.on = on
        self.hover = hover


    def mousePressEvent(self, event):
        super(TwoStateButton, self).mousePressEvent(event)
        self.setIcon(QtGui.QIcon(self.hover))

    def mouseReleaseEvent(self, event):
        super(TwoStateButton, self).mouseReleaseEvent(event)
        self.setIcon(QtGui.QIcon("%s" % (self.on)))

    def eventFilter(self, obj, event):
        if (event.type() == QEvent.Resize):
            print("\nRESIZING\n")
            self.setIconSize(QResizeEvent.size(self))



class Ui_MainWindow(QMainWindow):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1090, 681)
        MainWindow.setStyleSheet("")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")


        self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
        self.horizontalLayout.setObjectName("horizontalLayout")


        self.pushButton_2 = TwoStateButton("images/Button5-3.png", "images/Button5-4.png", self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(430, 310, 141, 81))
        self.pushButton_2.setText("")
        self.pushButton_2.setIconSize(QtCore.QSize(200, 200))
        self.pushButton_2.setCheckable(False)
        self.pushButton_2.setObjectName("pushButton_2")

        self.pushButton_2.installEventFilter(self.pushButton_2)

        self.horizontalLayout.addWidget(self.pushButton_2)


        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1090, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setStyleSheet("border-image: url(:/images/Button5-3.png)")
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

除了没有调整图标的大小外,我还收到以下错误消息:

TypeError: invalid result from TwoStateButton.eventFilter(), a 'bool' is expected not 'NoneType'

0 个答案:

没有答案