Giant Blob
Giant Blob

Reputation: 71

QPalette , set background image Python3 PyQT4

I am trying to set a background image to a QFrame I am calling in a QMainWindows like such :

        class MainWin(QtGui.QMainWindow):

        def __init__(self):
        super(MainWin, self).__init__()

        self.initUI()


        def initUI(self):


        #central widget
        self.theboard = Board(self)
        self.setCentralWidget(self.theboard)




        class Board(QtGui.QFrame):

        def __init__(self, parent):
        super(Board, self).__init__(parent)

        self.initBoard()

        def initBoard(self):

        #Set background Image
         frame = Board
         palette = QPalette()
         palette.setBrush(QPalette.Background,QBrush(QPixmap("ImageTest.jpg"))) 
         frame.setPalette(palette)

Using Qpalette / Qpixmap as I have found on some exemple on the net. But it don't work :

self.palette = QPalette() NameError: global name 'QPalette' is not defined

Why ? Here my class : class Board(QtGui.QFrame):

I am well inheriting of QtGui so Qpalette should work. I have to admit I am a bit confuse with the way Qpalette works.

Any help much apreciated,

Thanks !

Upvotes: 0

Views: 4426

Answers (1)

Lachlan
Lachlan

Reputation: 1282

You didn't write it in your code, so perhaps your issue is you didn't import it.

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QPalette

If that isn't the case, try this:

palette = QPalette()
palette.setBrush(QPalette.Background,QBrush(QPixmap("anne.jpg"))) # Haha, aren't I so funny??
frame.setPalette(palette)

Upvotes: 2

Related Questions