簡體   English   中英

如何使用pyside2編輯按鈕的樣式?

[英]How can I edit the style of button with pyside2?

現在,我使用Pyside2創建UI,但是按鈕的樣式非常古老,就像winxp一樣。 我希望它更新,但是我不知道該怎么做,有人知道該怎么做嗎?

現在ui

我想要的是

我的代碼就是這樣:

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)

如我所見,第二張圖片的樣式是“融合”,因此可能的解決方案是:

import sys
from PySide2 import QtWidgets

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

# ...

您需要使用setStyleSheet,請參見以下內容:

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}")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM