PYQT背景图像无法正确显示

时间:2017-09-22 14:24:33

标签: python pyqt background-image

我使用QT Designer创建了一个文件,并上传了一张背景图片。此文件运行良好,图像显示在背景中。 但是,当文件导入到主文件时,图像不会正确显示在后台。

和项目链接 https://github.com/ahmedlam3y/GarageSystem

1 个答案:

答案 0 :(得分:1)

因为它不是主窗口,但它是Widget,所以图片在后台不可见,其中一个Widgets已设置为mainWindow,因此它可以正常工作 和解决方案的代码:

import sys
from PyQt5.QtCore import QSize
from PyQt5 import QtCore, QtGui, QtWidgets as Q
from PyQt5.QtGui import QImage, QPalette, QBrush
from PyQt5.QtWidgets import *

import image_rc
from SignIN import Ui_Form as SignInForm
from WelFrame import Ui_Form as WelFrameForm
from SignUp import Ui_Form as SignUpForm
from Accounting import Ui_Form as AccountForm


class SignIn(Q.QWidget, SignInForm):  # Widget
    def __init__(self, parent=None):
        super(SignIn, self).__init__(parent)
        Q.QWidget.__init__(self, parent)
        self.setupUi(self)
        oImage = QImage("GTR.png")
        sImage = oImage.scaled(QSize(600, 360))  # resize Image to widgets size
        palette = QPalette()
        palette.setBrush(10, QBrush(sImage))  # 10 = WindowRole
        self.setPalette(palette)


class WelFrame(Q.QMainWindow, WelFrameForm):  # MainWindow
    def __init__(self, parent=None):
        Q.QWidget.__init__(self, parent)
        self.setupUi(self)


class SignUp(Q.QWidget, SignUpForm):  # Widget
    def __init__(self, parent=None):
        Q.QWidget.__init__(self, parent)
        self.setupUi(self)
        oImage = QImage("GTR.png")
        sImage = oImage.scaled(QSize(600, 360))  # resize Image to widgets size
        palette = QPalette()
        palette.setBrush(10, QBrush(sImage))  # 10 = WindowRole
        self.setPalette(palette)


class Accout(Q.QWidget, AccountForm):  # Widget
    def __init__(self, parent=None):
        Q.QWidget.__init__(self, parent)
        self.setupUi(self)
        oImage = QImage("GTR.png")
        sImage = oImage.scaled(QSize(600, 360))  # resize Image to widgets size
        palette = QPalette()
        palette.setBrush(10, QBrush(sImage))  # 10 = WindowRole
        self.setPalette(palette)


def foo(w1, w2):
    w1.show()
    w2.hide()


if __name__ == '__main__':
    app = Q.QApplication(sys.argv)
    wel = WelFrame()
    signIn = SignIn()
    signUp = SignUp()
    accout = AccountForm()
    wel.pushButton_2.clicked.connect(lambda: foo(signIn, wel))
    wel.pushButton.clicked.connect(lambda: foo(signUp, wel))
    signIn.pushButton_2.clicked.connect(lambda: foo(wel, signIn))
    signUp.pushButton_2.clicked.connect(lambda: foo(wel, signUp))
    wel.show()
    sys.exit(app.exec_())