Python QMainwindow PyQt5中的背景图片
我正在尝试将背景图像添加到主窗口,但无法使其正常工作Python QMainwindow PyQt5中的背景图片,python,pyqt,pyqt5,qmainwindow,Python,Pyqt,Pyqt5,Qmainwindow,我正在尝试将背景图像添加到主窗口,但无法使其正常工作 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
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_())
我尝试过各种不同的方法,但没有一种能达到应有的效果。
我从另一个主题中找到了以下代码作为解决方案,但它只给了我一个黑色背景,其余的小部件变得滞后
oImage = QImage("table.png")
sImage = oImage.scaled(QSize(440, 280))
palette = QPalette()
palette.setBrush(QPalette.Window, QBrush(sImage))
self.setPalette(palette)
我不知道整个窗口是否变暗,或者到底发生了什么,但下面的图片是使用上面代码的窗口部分的屏幕截图,正如你所看到的,它变暗了,滑块显示了它之前打开的所有位置,总之有点暗
我也尝试过setStyleSheet,但我不知道是我的语法错了还是它是一种错误的方式。有人知道正确的方法吗
编辑
这是我当前的窗口:
这是我试图实现的作为当前窗口背景的图片,名为“table.png”:
这是我试图做的可视化,这是用油漆做的,因为我不知道如何正确地做:
如果我使用另一个主题的代码,我会得到:
出现黑色背景的一个可能原因是QImage为空。由于图像无效或图像路径不正确,QImage为null。在这种情况下,我认为这是第二种情况,因为OP使用的是容易出错的相对路径。解决方案是使用脚本信息(如其位置)构建绝对路径:
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_())
或
你所说的“其他小部件变得滞后”是什么意思?另外,你能告诉我们你是如何尝试设置样式表的(包括它们的内容),即使它们不起作用吗?添加了一张图片和我对laggy的简短描述。我尝试了几个不同版本的QMainWindow.setStyleSheet(self,'table.png'),但这一行代码与否并没有任何区别。背景仍然是没有背景的白色。@bjorstig 1)您可以提供table.png,2)显示您想要得到的图片,3)提供@eyllaesc我编辑了问题并添加了一些图片
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_())