简体   繁体   中英

How can I edit the style of button with pyside2?

Now I use Pyside2 to create a UI, but the style of the button is very old, just like winxp. I want it to be newer, but I don't know how to do it, do anyone know how to do?

now ui

what I want

My code is just like that:

class MainWindow(QMainWindow):

    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)
        self.open_directory_button = QPushButton("打开文件夹")
self.open_directory_button.clicked.connect(self.open_directory_button_clicked)
        self.path_layout = QHBoxLayout()
        self.path_layout.addWidget(self.path_edit)
        self.path_layout.addWidget(self.open_directory_button)
        self.main_layout = QVBoxLayout()
        self.main_layout.addLayout(self.path_layout)
        self.frame = QWidget(self)
        self.frame.setLayout(self.main_layout)
        self.setCentralWidget(self.frame)

As I see the style of the second image is the "fusion" so a possible solution is:

import sys
from PySide2 import QtWidgets

app = QtWidgets.QApplication(sys.argv)
app.setStyle("fusion") # <----

# ...

You need to use the setStyleSheet, see this:

open_directory_button = QtWidgets.QPushButton()
open_directory_button.setStyleSheet("QPushButton:pressed{image:url(C:\image.png); border:none} QPushButton:hover{image:url(C:\image_hover.png); border:none}")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM