在pyqt5中对齐小部件中的按钮时出现问题

2024-05-13 18:42:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试使用python语言的pyqt5gui创建一个具有图像背景和两个按钮(cancel和ok)以及图像和下推/上推效果的小部件,但我有两个问题:

1–我无法对齐widget center中的按钮

2–按钮事件发生两次

代码:

import sys
from PyQt5 import QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class PicButton(QAbstractButton):
    def __init__(self, pixmap, pixmap_pressed, id_button, parent=None):
        super(PicButton, self).__init__(parent)
        self.pixmap = pixmap
        self.pixmap_pressed = pixmap_pressed
        self.id_buton = id_button

        self.pressed.connect(self.update)
        self.released.connect(self.update)

    def paintEvent(self, event):
        if self.isDown():
            pix = self.pixmap_pressed
            print("botao pressionado: ", self.id_buton)
        else:
            pix = self.pixmap

        painter = QPainter(self)
        painter.drawPixmap(event.rect(), pix)

    def enterEvent(self, event):
        self.update()

    def leaveEvent(self, event):
        self.update()

    def sizeHint(self):
        return QSize(131, 82)


app = QApplication(sys.argv)
window = QWidget()
window.setGeometry(800, 450, 800, 450)

pixmap = QPixmap("background.png")
brush = QBrush(pixmap)
palette = QPalette()
palette.setBrush(QPalette.Background, brush)
window.setPalette(palette)

button1 = PicButton(QtGui.QPixmap("cancel_up.png"), QtGui.QPixmap("cancel_down.png"), "cancel")
button2 = PicButton(QtGui.QPixmap("ok_up.png"), QtGui.QPixmap("ok_down.png"), "ok")

layout = QHBoxLayout()
layout.addStretch(1)
layout.addWidget(button1)
layout.addWidget(button2)
layout.setAlignment(Qt.AlignCenter)

window.setLayout(layout)
window.show()

sys.exit(app.exec_())

获取链接中的图像:https://www.filedropper.com/imagens

有人能告诉我我做错了什么吗?你知道吗


Tags: fromimportselfpngdefokwindowcancel
1条回答
网友
1楼 · 发布于 2024-05-13 18:42:25

我无法对齐窗口小部件中心的按钮:如果您在按钮1之前应用了addStretch(),使其对称,则必须在按钮2之后设置另一个addStretch()

按钮事件发生两次:QAbstractButton在您按下按钮时已调用update方法,因此无需将按下或释放的信号连接到update()方法或在enterEvent()leaveEvent()方法中调用它。你知道吗

考虑到上述情况,解决方案是:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class PicButton(QtWidgets.QAbstractButton):
    def __init__(self, pixmap, pixmap_pressed, id_button, parent=None):
        super(PicButton, self).__init__(parent)
        self.pixmap = pixmap
        self.pixmap_pressed = pixmap_pressed
        self.id_buton = id_button

    def paintEvent(self, event):
        if self.isDown():
            pix = self.pixmap_pressed
            print("botao pressionado: ", self.id_buton)
        else:
            pix = self.pixmap

        painter = QtGui.QPainter(self)
        painter.drawPixmap(event.rect(), pix)

    def sizeHint(self):
        return QtCore.QSize(131, 82)

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window =QtWidgets. QWidget()
    window.setGeometry(800, 450, 800, 450)

    pixmap = QtGui.QPixmap("background.png")
    brush = QtGui.QBrush(pixmap)
    palette = QtGui.QPalette()
    palette.setBrush(QtGui.QPalette.Background, brush)
    window.setPalette(palette)

    button1 = PicButton(QtGui.QPixmap("cancel_up.png"), QtGui.QPixmap("cancel_down.png"), "cancel")
    button2 = PicButton(QtGui.QPixmap("ok_up.png"), QtGui.QPixmap("ok_down.png"), "ok")

    layout = QtWidgets.QHBoxLayout(window)
    layout.addStretch()
    layout.addWidget(button1)
    layout.addWidget(button2)
    layout.addStretch()
    window.show()
    sys.exit(app.exec_())

相关问题 更多 >