为另一个类中的小部件设置背景图像

时间:2019-06-18 23:50:18

标签: python pyqt pyqt5 python-3.6

我最近一直在学习pyqt5作为我的第一个GUI框架。到目前为止,我一直在尝试使用QtStackedLayout。我目前有两个窗口屏幕,一个在UI类中创建,另一个在另一个单独的类中创建。我有两个问题:

  1. 一切正常,直到我开始尝试为Window 1添加背景图像。没有图像显示,但是代码运行正常。
  2. 在开始的那一小段时间中,首先显示一个窗口,直到将其加载到主窗口为止,我尝试在对象实例化期间传递self来作为某种父级进行插入,以防止这,但我认为我做得不好。

请参阅以下代码(我的导入语句不正确,我将对其进行整理)

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class Ui(QWidget):

    def setupUi(self, Main, width, height):
        self.stack = QStackedLayout()
        self.window_1 = WindowOne(width, height)
        self.window_2 = QWidget(self)
        self.window_2_UI()

        self.stack.addWidget(self.window_1)
        self.stack.addWidget(self.window_2)

        # Only one button
        self.btn = QPushButton("Change window", self)

        # Create the central widget of your Main Window
        self.main_widget = QWidget(self)
        layout = QVBoxLayout(self.main_widget)
        layout.addLayout(self.stack)
        layout.addWidget(self.btn)

        self.setCentralWidget(self.main_widget)

        self.btn.clicked.connect(self.change_window)

    def change_window(self):
        if self.stack.currentIndex() == 0:
            self.stack.setCurrentIndex(1)
        else:
            self.stack.setCurrentIndex(0)

    def window_2_UI(self):
        label = QLabel("In Window 2", self.window_2)


class WindowOne(QWidget):
    def __init__(self, width, height):
        super().__init__()
        self.set_bg(width, height)
        self.set_label()
        # self.setStyleSheet("background-image: url(:resource/images/blue_bg.jpg)")

    def set_label(self):
        label = QLabel("In Window 1", self)

    def set_bg(self, w, h):
        oImage = QImage("resource/images/blue_bg.jpg")
        sImage = oImage.scaled(QSize(w, h))
        palette = QPalette()
        palette.setBrush(10, QBrush(sImage))
        self.setPalette(palette)


class Main(QMainWindow, Ui):
    def __init__(self):
        super().__init__()
        self.w_width = 480
        self.w_height = 720
        self.resize(self.w_width, self.w_height)
        self.init_ui()
        self.setupUi(self, self.w_width, self.w_height)

    def init_ui(self):
        self.center()
        self.setWindowTitle('Main Window')

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

if __name__ == "__main__":
    app = QApplication(sys.argv)
    M = Main()
    M.show()
    sys.exit(app.exec())

1 个答案:

答案 0 :(得分:2)

默认情况下,仅窗口(与窗口小部件不同)将使用QPalette的背景色,如果您希望窗口小部件使用QPalette的背景色,则必须启用autoFillBackground属性。

# ...
self.set_label(width, height)
self.setAutoFillBackground(True)
# ...

尽管您的代码有些混乱,例如,您确定某些方法会接收某些参数,但是您从不使用它们。最后,我认为您希望使用窗口的大小来重新缩放图像的背景,因此我已经覆盖了resizeEvent()方法,以便缩放采用窗口的大小。

考虑到上述所有情况,我改进了您的代码:

import sys

from PyQt5 import QtCore, QtGui, QtWidgets


class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)

        self.m_stacked_layout = QtWidgets.QStackedLayout()

        self.widget_1 = WidgetOne()
        self.widget_2 = QtWidgets.QWidget()

        self.widget_2_UI()

        self.m_stacked_layout.addWidget(self.widget_1)
        self.m_stacked_layout.addWidget(self.widget_2)

        button = QtWidgets.QPushButton(
            "Change window", clicked=self.change_window
        )

        lay = QtWidgets.QVBoxLayout(self)
        lay.addLayout(self.m_stacked_layout)
        lay.addWidget(button)

    @QtCore.pyqtSlot()
    def change_window(self):
        ix = self.m_stacked_layout.currentIndex()
        self.m_stacked_layout.setCurrentIndex(1 if ix == 0 else 0)

    def widget_2_UI(self):
        label = QtWidgets.QLabel("In Window 2", self.widget_2)


class WidgetOne(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setAutoFillBackground(True)
        self.set_label()
        self.m_image = QtGui.QImage("resource/images/blue_bg.jpg")

    def set_label(self):
        label = QtWidgets.QLabel("In Window 1", self)

    def resizeEvent(self, event):
        palette = self.palette()
        sImage = self.m_image.scaled(event.size())
        palette.setBrush(10, QtGui.QBrush(sImage))
        self.setPalette(palette)
        super(WidgetOne, self).resizeEvent(event)

class Main(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        widget = Widget()
        self.setCentralWidget(widget)

        self.resize(480, 720)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = Main()
    w.show()
    sys.exit(app.exec())