【发布时间】:2021-05-28 12:22:20
【问题描述】:
我正在尝试使用样式表将图片添加到 PyQt5 中的按钮。如果我使用图片的绝对路径,它工作正常,但我需要使用相对路径。我已经尝试过pythonic方式(注释掉的部分),但它可能因为反斜杠而无法正常工作。我知道 Qt 资源,但我不明白如何使用它们。 link
import os, sys
from PyQt5.QtWidgets import *
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
#scriptDir = os.path.dirname(os.path.realpath(__file__))
#pngloc = (scriptDir + os.path.sep + 'resources' + os.path.sep + 'min.png')
button1 = QPushButton("", self)
button1.setStyleSheet('QPushButton {'
'background-image: url(e:/new/resources/min.png);'
'}')
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
【问题讨论】:
标签: python pyqt pyqt5 qtstylesheets