Skip to main content

pyqt-svg-button

PyQt button which supports svg icon

Qt's setIcon of button widget doesn't help that much in terms of setting SVG image as an icon. It's just another good old fashioned pixmap icon.

So i overrided the method to set SVG icon with the power of CSS.

By the way, parent class of this is QPushButton and SvgAbstractButton. (Multiple inheritance)

(the repo/package name has changed on 2022/05/18 from pyqt-svg-icon-pushbutton)

Default background color of this is transparent.

If you want to use svg supporting QToolButton instead of QPushButton, use pyqt-svg-toolbutton.

Requirements

  • PyQt5 >= 5.8

Setup

python -m pip install pyqt-svg-button

Usage

  • SvgButton(base_widget: QWidget = None) - Constructor. Base widget is the widget that the button's background color based of when button get hovered/pressed by mouse cursor. If value is default(None), background color of button which is getting hovered/pressed will set to #DDDDDD, #FFFFFF.

  • setIcon(icon: str) - set the icon. Icon should be a SVG file's name. This is overriding method.

  • setPadding(padding: int) - set the button's padding.

  • setBorderRadius(border_radius: int)

  • setBackground(background=None) - you can give background argument's value either 'transparent' or 6-digits or 3-digits hex color string or color's name like 'red', 'green'. If you don't give any arguments(None), background will automatically be set based on base widget's color if you give base widget argument to SvgButton constructor.

  • setAsCircle() - set button's shape as circle.

Included Packages

Example

Code Sample

from PyQt5.QtWidgets import QWidget, QApplication, QHBoxLayout



from pyqt_svg_button.svgButton import SvgButton





class SvgButtonExample(QWidget):

    def __init__(self):

        super().__init__()

        self.__initUi()



    def __initUi(self):

        newButton = SvgButton()

        newButton.setIcon('new.svg')



        openButton = SvgButton()

        openButton.setIcon('open.svg')



        saveButton = SvgButton()

        saveButton.setIcon('save.svg')



        lay = QHBoxLayout()

        lay.addWidget(newButton)

        lay.addWidget(openButton)

        lay.addWidget(saveButton)



        self.setLayout(lay)





if __name__ == "__main__":

    import sys



    app = QApplication(sys.argv)

    ex = SvgButtonExample()

    ex.show()

    sys.exit(app.exec_())

Result

https://user-images.githubusercontent.com/55078043/153802219-ae019e5b-f603-4aad-93ce-035d33edd9a8.mp4

Sorry for the video quality.

image

Image quality is not perfect, but much better than video. Image above is slightly bigger than actual size.

Metadata

Release files for pyqt-svg-button 0.0.26

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyqt-svg-button 0.0.26
File Size Uploaded
pyqt-svg-button-0.0.26.tar.gz 3.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pyqt-svg-button 0.0.26
File Interpreter ABI Platform
pyqt_svg_button-0.0.26-py3-none-any.whl Python 3 none any Details

Total release size: 7.4 kB

Release files / pyqt-svg-button-0.0.26.tar.gz

Download URL pyqt-svg-button-0.0.26.tar.gz
Size 3.4 kB
Tags Source
SHA-256 checksum
How to use checksums
30d426b53b5d182866f07dd6ba3bfc32f70e154251058ccebb9c5c40df319f28
BLAKE2b-256 checksum
How to use checksums
363624571cf5faf43ac10b6115575fc6d53629c7e71a553d343c4a6506f0f575
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/0.0.0 CPython/3.7.6

Release files / pyqt_svg_button-0.0.26-py3-none-any.whl

Download URL pyqt_svg_button-0.0.26-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a9fe0278865ae3f8986e71685f3b0e1460a60266a88c0c7caf53d4dcb026f69a
BLAKE2b-256 checksum
How to use checksums
c8740deb858394ac4dc62ab057098ae312feb335db9f7f228fdc2e25a995f24c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/0.0.0 CPython/3.7.6

Release history Release notifications | RSS feed

This release

0.0.26 This release

2 release files

0.0.25

2 release files

0.0.24

2 release files

0.0.23

2 release files

0.0.22

2 release files

0.0.21

2 release files

0.0.20

2 release files

0.0.19

2 release files

0.0.18

2 release files

0.0.17

2 release files

0.0.16

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page