Search code examples
pythonpyside2

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)

Solution

  • 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") # <----
    
    # ...