使用PyQt5添加背景图像

2024-09-27 20:20:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图使用前面问题的答案添加背景

遗憾的是,它们无法工作并返回错误,无论是样式表、还是=符号或“.”

我想这可能是我的图像位置?是否有特殊要求来存储图像,或者我缺少的其他东西

我已经显示了代码的编辑版本

谢谢

import sys
from PyQt5.QtWidgets import QApplication,  QWidget,  QLabel, QMainWindow, QPushButton, QAction
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import pyqtSlot
import os
os.chdir(r'C:\Users\Paul Hannell\python_files')

class App(QMainWindow):      # Opening Window

    def __init__(self):
        super().__init__()
        self.title = "Timelord Timer PyQt5"
        self.left = 70
        self.top = 100
        self.width = 1170
        self.height = 740
        self.initUI()


    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.setWindowIcon(QIcon(r'C:\Users\Paul Hannell\python_files\Timelord.ico'))
        self.statusBar().showMessage('Message in Status Bar')
        label=QLabel(self)

        ############################
        # Background Image

        self.centralwidget = QWidget()
        self.setCentralWidget(self.centralwidget)
        lay = QHBoxLayout(self.centralwidget)

stylesheet = '''
    MainWindow {
        background-image: url(r'C:\Users\Paul Hannell\python_files\Running_Around4.png');
        background-repeat: no-repeat;
        background-position: center;
    }
'''

        #####################################

mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('File')
settingsMenu = mainMenu.addMenu('Settings')
resultsMenu = mainMenu.addMenu('Results')
reportsMenu = mainMenu.addMenu('Reports')
infoMenu = mainMenu.addMenu('Info')

newButton=QAction('New', self)
newButton.setStatusTip('New Race')
        #newButton.triggered.connect(self.create)    #This open new event options
fileMenu.addAction(newButton)

openButton = QAction('Open' , self)
openButton.setStatusTip('Open File')
        #openButton.triggered.connect(self.open)  # This will open existing
fileMenu.addAction(openButton)

deleteButton=QAction('Delete', self)
deleteButton.setStatusTip('Delete Race')
        #deleteButton.triggered.connect(self.create)    #This delete existing event.
fileMenu.addAction(deleteButton)

exitButton=QAction('Exit', self)
exitButton.setStatusTip('Exit application')
exitButton.triggered.connect(self.close)
fileMenu.addAction(exitButton)



self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())


Tags: importselfconnectpyqt5triggeredaddactionqactionmainmenu
1条回答
网友
1楼 · 发布于 2024-09-27 20:20:01

您的代码缩进严重(而且太长),因此很难判断,但我发现了几个问题:

  • 它应该在样式表中#MainWindow(您缺少一个#
  • 您需要使用以下名称命名应用程序:self.setObjectName('MainWindow')
  • 您需要在某个时候使用setStyleSheet
  • url需要修复:没有引号或'r';只需文件名(可能文件名中的空格需要转义,您可以尝试使用它)

例如,这种方法可以:

import sys
from PyQt5.QtWidgets import QApplication,  QWidget,  QLabel, QMainWindow, QPushButton, QAction


class App(QMainWindow):      # Opening Window
    def __init__(self):
        super().__init__()
        self.setWindowTitle('hello bg')

        self.setObjectName('MainWindow')

        stylesheet = '''
    #MainWindow {
        background-image: url(/home/me/photos/DSC_0001.jpg);
        background-repeat: no-repeat;
        background-position: center;
    }
'''        
        self.setStyleSheet(stylesheet)
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

相关问题 更多 >

    热门问题