QPalette,设置背景图像Python3 PyQT4。

时间:2021-05-30 21:20:32

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

我试图将背景图像设置为一个QFrame,我在QMainWindows中调用如下:

        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 :

使用Qpalette / Qpixmap,就像我在网上找到的例子一样。但它不起作用:

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

自我。palette = QPalette() NameError:全局名称“QPalette”没有定义。

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

为什么?我的班级:班级董事会(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.

我很好地继承了QtGui,所以Qpalette应该工作。我必须承认,我对Qpalette的工作方式有点迷惑。

Any help much apreciated,

任何帮助apreciated,

Thanks !

谢谢!

1 个解决方案

#1


1  

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)

#1


1  

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)