如何在 PyQt 中编写图像按钮?

发布于 2024-08-30 02:26:29 字数 38 浏览 3 评论 0原文

我正在尝试做简单的音频播放器,但我想使用图像(图标)作为按钮。

Im trying to do simple audio player, but I want use a image(icon) as a pushbutton.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

焚却相思 2024-09-06 02:26:29

您可以子类化 QAbstractButton 并制作您自己的按钮。这是一个基本的简单示例:

import sys
from PyQt4.QtGui import *

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

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawPixmap(event.rect(), self.pixmap)

    def sizeHint(self):
        return self.pixmap.size()

app = QApplication(sys.argv)
window = QWidget()
layout = QHBoxLayout(window)

button = PicButton(QPixmap("image.png"))
layout.addWidget(button)

window.show()
sys.exit(app.exec_())

这不是一个超级简单的方法,但它为您提供了很多控制权。您可以添加第二个像素图,并且仅当鼠标指针悬停在按钮上时才绘制它。您可以将当前的拉伸行为更改为居中行为。您可以使其不具有矩形形状等等...

在鼠标悬停和按下时更改图像的按钮:

from PyQt4.QtGui import *
from PyQt4.QtCore import *

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

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

    def paintEvent(self, event):
        pix = self.pixmap_hover if self.underMouse() else self.pixmap
        if self.isDown():
            pix = self.pixmap_pressed

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

You can subclass QAbstractButton and make a button of your own. Here is a basic simple example:

import sys
from PyQt4.QtGui import *

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

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.drawPixmap(event.rect(), self.pixmap)

    def sizeHint(self):
        return self.pixmap.size()

app = QApplication(sys.argv)
window = QWidget()
layout = QHBoxLayout(window)

button = PicButton(QPixmap("image.png"))
layout.addWidget(button)

window.show()
sys.exit(app.exec_())

That's not a super easy way, but it gives you a lot of control. You can add second pixmap and draw it only when the mouse pointer is hover over button. You can change current stretching behavior to the centering one. You can make it to have not a rectangular shape and so on...

Button that changes images on mouse hover and when pressed:

from PyQt4.QtGui import *
from PyQt4.QtCore import *

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

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

    def paintEvent(self, event):
        pix = self.pixmap_hover if self.underMouse() else self.pixmap
        if self.isDown():
            pix = self.pixmap_pressed

        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(200, 200)
一花一树开 2024-09-06 02:26:29

您可以使用 QToolButton 并将 autoraise 属性设置为 true,并且还可以设置图像。

You can use QToolButton with set autoraise property true and there you can set your image also.

自由如风 2024-09-06 02:26:29

我看到很多人都遇到这个问题,并决定编写一个适当的示例来解决它。您可以在这里找到它: 示例如何使QLabel可点击
我帖子中的解决方案通过扩展 QLabel 使其发出 clicked() 信号来解决该问题。
扩展的 QLabel 看起来像这样:

class ExtendedQLabel(QLabel):

    def __init__(self, parent):
        QLabel.__init__(self, parent)

    def mouseReleaseEvent(self, ev):
        self.emit(SIGNAL('clicked()'))

我希望这有帮助!

I've seen that a lot of people have this problem and decided to write a proper example on how to fix it. You can find it here: An example on how to make QLabel clickable
The solution in my post solves the problem by extending QLabel so that it emits the clicked() signal.
The extended QLabel looks something like this:

class ExtendedQLabel(QLabel):

    def __init__(self, parent):
        QLabel.__init__(self, parent)

    def mouseReleaseEvent(self, ev):
        self.emit(SIGNAL('clicked()'))

I hope this helps!

鲜血染红嫁衣 2024-09-06 02:26:29

也许是这样的?

import sys

from PyQt4.QtGui import *
from PyQt4.QtCore import *

app = QApplication(sys.argv)
widget = QWidget()
layout = QHBoxLayout()
widget.setLayout(layout)
button = QPushButton()
layout.addWidget(button)
icon = QIcon("image.png")
button.setIcon(icon)
widget.show()
app.exec_()

Something like this, maybe?

import sys

from PyQt4.QtGui import *
from PyQt4.QtCore import *

app = QApplication(sys.argv)
widget = QWidget()
layout = QHBoxLayout()
widget.setLayout(layout)
button = QPushButton()
layout.addWidget(button)
icon = QIcon("image.png")
button.setIcon(icon)
widget.show()
app.exec_()
你穿错了嫁妆 2024-09-06 02:26:29

另一种选择是使用样式表。像这样的东西:

from PyQt4 import QtCore, QtGui
import os
...

path = os.getcwd()
self.myButton.setStyleSheet("background-image: url(" + path + "/myImage.png);")

Another option is to use stylesheets. Something like:

from PyQt4 import QtCore, QtGui
import os
...

path = os.getcwd()
self.myButton.setStyleSheet("background-image: url(" + path + "/myImage.png);")
恰似旧人归 2024-09-06 02:26:29

如果您想在菜单工具栏中使用图像而不是QPushButton,您可以通过QAction来完成。工作示例:

from PyQt5 import QtWidgets, QtGui, QtCore
import sys

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self):

        super(MainWindow, self).__init__()
        self.setWindowTitle("Clickable Image in Menubar and Toolbar")

        action = QtWidgets.QAction(
            QtGui.QIcon("image.png"), "&Clickable Image", self)
        action.triggered.connect(self.test)

        menubar = self.menuBar()
        menubar.addAction(action)
        toolbar = QtWidgets.QToolBar("Toolbar")
        toolbar.setIconSize(QtCore.QSize(20, 20))
        toolbar.addAction(action)
        self.addToolBar(toolbar)

    def test(self):

        print("Image clicked.")

app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

If you want use an image instead of a QPushButton in menubar or in toolbar, you can do it via QAction. Working example:

from PyQt5 import QtWidgets, QtGui, QtCore
import sys

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self):

        super(MainWindow, self).__init__()
        self.setWindowTitle("Clickable Image in Menubar and Toolbar")

        action = QtWidgets.QAction(
            QtGui.QIcon("image.png"), "&Clickable Image", self)
        action.triggered.connect(self.test)

        menubar = self.menuBar()
        menubar.addAction(action)
        toolbar = QtWidgets.QToolBar("Toolbar")
        toolbar.setIconSize(QtCore.QSize(20, 20))
        toolbar.addAction(action)
        self.addToolBar(toolbar)

    def test(self):

        print("Image clicked.")

app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文