0

I'm trying to add a background using the answers from previous questions.

Sadly they don't work and return errors, either stylesheet, or the = sign, or the """.

I think it may be my location of the image? Is there something special required to store the image perhaps or something else I'm missing?

I've shown an edited down version of the code.

Thanks

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_())

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Paul Hannell
  • 49
  • 1
  • 2
  • 6

1 Answers1

1

Your code is badly indented (and too long) so it's hard to tell, but I see several issues:

  • it shoud be #MainWindow in the style sheet (you're missing a #)
  • you need to name the App with this name: self.setObjectName('MainWindow')
  • you need to use setStyleSheet at some point
  • the url needs fixing: no quotes nor 'r'; simply the file name (maybe the space in the file name needs escaping, you could try to play with it)

This, for instance, works:

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_())

Demi-Lune
  • 1,868
  • 2
  • 15
  • 26
  • Thanks for this. Unfortunately it didn't resolve the issue. The Syntax error still rejects at the 3 dashes. Cheers Paul – Paul Hannell May 28 '20 at 03:47
  • I'm opening the file location, clicking the folder image in the url to get the format, pasting that into the brackets and - Error. I can only imagine there is some special way to load the url specific to PyQt5 because it works in Tkinter. – Paul Hannell May 28 '20 at 04:08
  • I found a great answer here: https://stackoverflow.com/questions/53560035/pyqt-different-image-autoscaling-modes?noredirect=1&lq=1 But when I expand to a larger or full window, it multiplies the Menu labels. Is there a way to stop that from happening? – Paul Hannell May 28 '20 at 05:31
  • Giving up for the moment. It's holding up the project too much and killing my learning being stuck on this. Maybe is Window 10 ? – Paul Hannell May 31 '20 at 05:58