简体   繁体   English

QMainwindow PyQt5中的背景图片

[英]Background picture in QMainwindow PyQt5

I'm trying to get a background image to my mainwindow but i can't get it to work properly.我正在尝试将背景图像添加到我的主窗口,但无法正常工作。

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QLabel
from PyQt5.QtGui import QIcon
from PyQt5 import QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtSvg import *
from PyQt5.QtWidgets import *
from abc import abstractmethod


class App(QMainWindow):

    def __init__(self, parent=None):
        super(App, self).__init__(parent=parent)
        self.title = 'Title'
        self.left = 500
        self.top = 500
        self.width = 440
        self.height = 280
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        # ...

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    #view = TableScene(ex)
    ex.show()
    sys.exit(app.exec_())

I've tried different kinds of methods but none of them works as it should.我尝试了不同种类的方法,但没有一种方法可以正常工作。 I found the following code as a solution from another topic but it just gives me a black background and the rest of the widgets get laggy.我发现以下代码是另一个主题的解决方案,但它只是给了我一个黑色背景,其余的小部件变得滞后。

     oImage = QImage("table.png")
     sImage = oImage.scaled(QSize(440, 280))
     palette = QPalette()
     palette.setBrush(QPalette.Window, QBrush(sImage))
     self.setPalette(palette)

I don't know if the whole window gets laggy or what really happens but the picture below is a screenshot of a part of the window using the code above, and as you can see it gets all black and the slider shows all the previous position it has been on, sort of laggy anyways.我不知道整个窗口是否变得迟钝或者真的发生了什么,但下图是使用上面代码的窗口的一部分的屏幕截图,正如你所看到的,它全黑了,滑块显示了之前的所有位置它一直在,反正有点滞后。

“滞后”版本的图像

I've also tried the setStyleSheet but I don't know if it's my syntax that's wrong or if it's a faulty way of doing it.我也试过 setStyleSheet 但我不知道是我的语法错误还是这样做的方式有问题。 Does anyone know a way of doing it correctly?有谁知道正确的方法吗?

EDIT编辑

This is my current window:这是我当前的窗口:

目前来看

This is the picture I'm trying to implement as a background to my current window, the picture called "table.png" :这是我试图作为当前窗口背景的图片,该图片名为“table.png”:

我想要作为背景的图片

This is a visualization of what I'm trying to do, and this is made in paint since I don't know how to do it correctly:这是我正在尝试做的事情的可视化,这是在油漆中制作的,因为我不知道如何正确地做到这一点:

想要的视图

And this is what I get if i use the code from the other topic:如果我使用其他主题中的代码,这就是我得到的结果:

不正确的看法

One of the possible reasons why a black background appears is that QImage is null.出现黑色背景的可能原因之一是 QImage 为空。 And a QImage is null because the image is invalid or because the image path is incorrect.并且一个 QImage 为空,因为图像无效或因为图像路径不正确。 In this case I think it is the second case since the OP uses a relative path that is prone to errors.在这种情况下,我认为这是第二种情况,因为 OP 使用了容易出错的相对路径。 The solution is to build the absolute path using the script information such as its location:解决方案是使用脚本信息(例如其位置)构建绝对路径:

import os
import sys

from PyQt5 import QtCore, QtGui, QtWidgets

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))


class App(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(App, self).__init__(parent=parent)
        self.initUI()

    def initUI(self):
        self.setWindowTitle("Title")
        self.setGeometry(500, 500, 440, 280)
        oImage = QtGui.QImage(os.path.join(CURRENT_DIR, "table.png"))
        sImage = oImage.scaled(QtCore.QSize(440, 280))
        palette = QtGui.QPalette()
        palette.setBrush(QtGui.QPalette.Window, QtGui.QBrush(sImage))
        self.setPalette(palette)

        pushbutton = QtWidgets.QPushButton("test", self)
        pushbutton.move(100, 100)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ex = App()
    ex.show()
    sys.exit(app.exec_())

在此处输入图片说明


Note: The image provided by the OP has extension .jpg but the one indicated by code is .png, maybe "imgur" has changed the extension.注意: OP提供的图片扩展名为.jpg,但代码指示的是.png,可能是“imgur”更改了扩展名。

Note: If the window is resized manually, the following behavior will be observed:注意:如果手动调整窗口大小,将观察到以下行为:

在此处输入图片说明

So for this there are 2 possible solutions depending on the developer's criteria:因此,根据开发人员的标准,有两种可能的解决方案:

  • Set a fixed size: self.setFixedSize(440, 280)设置固定大小: self.setFixedSize(440, 280)
  • Adapt the image to the size of the window:使图像适应窗口的大小:

     import os import sys from PyQt5 import QtCore, QtGui, QtWidgets CURRENT_DIR = os.path.dirname(os.path.realpath(__file__)) class App(QtWidgets.QMainWindow): def __init__(self, parent=None): super(App, self).__init__(parent=parent) self.initUI() def initUI(self): self.setWindowTitle("Title") self.setGeometry(500, 500, 440, 280) pushbutton = QtWidgets.QPushButton("test", self) pushbutton.move(100, 100) self.oImage = QtGui.QImage(os.path.join(CURRENT_DIR, "table.png")) # or QPixmap # self.oPixmap = QtGui.QPixmap(os.path.join(CURRENT_DIR, "table.png")) def paintEvent(self, event): painter = QtGui.QPainter(self) painter.drawImage(self.rect(), self.oImage) # or QPixmap # painter.drawPixmap(self.rect(), self.oPixmap) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) ex = App() ex.show() sys.exit(app.exec_())

    or或者

    import os import sys from PyQt5 import QtCore, QtGui, QtWidgets CURRENT_DIR = os.path.dirname(os.path.realpath(__file__)) class App(QtWidgets.QMainWindow): def __init__(self, parent=None): super(App, self).__init__(parent=parent) self.initUI() def initUI(self): self.setWindowTitle("Title") self.setGeometry(500, 500, 440, 280) pushbutton = QtWidgets.QPushButton("test", self) pushbutton.move(100, 100) self.setStyleSheet( """ QMainWindow{ border-image: url(%s) 0 0 0 0 stretch stretch } """ % os.path.join(CURRENT_DIR, "table.png") ) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) ex = App() ex.show() sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM