如何在PyQt4中向QWidget添加图片?

时间:2022-04-17 09:00:02

How can I add or import a picture to a QWidget? I have found a clue. I can add a Label and add a Picture in that label. I need the arguments for the QPicture(). The probable I can use is, QLabel.setPicture(self.QPicture).

如何向QWidget添加或导入图片?我找到了一个线索。我可以添加一个标签并在标签中添加图片。我需要QPicture()的参数。我可以使用的可能是qlab . setpicture (self.QPicture)。

3 个解决方案

#1


17  

Here is some code that does what you and @erelender described:

下面是一些代码,它们完成了您和@erelender所描述的:

import os,sys
from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)
window = QtGui.QMainWindow()
window.setGeometry(0, 0, 400, 200)

pic = QtGui.QLabel(window)
pic.setGeometry(10, 10, 400, 100)
#use full ABSOLUTE path to the image, not relative
pic.setPixmap(QtGui.QPixmap(os.getcwd() + "/logo.png"))

window.show()
sys.exit(app.exec_())

A generic QWidget does not have setPixmap(). If this approach does not work for you, you can look at making your own custom widget that derives from QWidget and override the paintEvent method to display the image.

通用QWidget没有setPixmap()。如果这种方法对您不起作用,您可以查看如何创建自定义小部件,该小部件派生自QWidget,并覆盖paintEvent方法以显示图像。

#2


12  

QPicture is not what you want. QPicture records and replays QPainter commands. What you want is QPixmap. Give a filename to the QPixmap constructor, and set this pixmap to the label using QLabel.setPixmap().

QPicture不是你想要的。QPicture记录和回放QPainter命令。你想要的是QPixmap。给QPixmap构造函数一个文件名,然后使用qlab . setpixmap()将该像素映射设置为标签。

An implementation example in python would be:

python中的实现示例是:

label = QLabel() 
pixmap = QPixmap('path_to_your_image')
label.setPixmap(pixmap)

#3


-1  

Use this snippet in your GUI script:

在您的GUI脚本中使用这个代码片段:

image = QtGui.QLabel(self)
image.setGeometry(50, 40, 250, 250)
pixmap = QtGui.QPixmap("C:\\address\\to\\your_image.png")
image.setPixmap(pixmap)
image.show()

#1


17  

Here is some code that does what you and @erelender described:

下面是一些代码,它们完成了您和@erelender所描述的:

import os,sys
from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)
window = QtGui.QMainWindow()
window.setGeometry(0, 0, 400, 200)

pic = QtGui.QLabel(window)
pic.setGeometry(10, 10, 400, 100)
#use full ABSOLUTE path to the image, not relative
pic.setPixmap(QtGui.QPixmap(os.getcwd() + "/logo.png"))

window.show()
sys.exit(app.exec_())

A generic QWidget does not have setPixmap(). If this approach does not work for you, you can look at making your own custom widget that derives from QWidget and override the paintEvent method to display the image.

通用QWidget没有setPixmap()。如果这种方法对您不起作用,您可以查看如何创建自定义小部件,该小部件派生自QWidget,并覆盖paintEvent方法以显示图像。

#2


12  

QPicture is not what you want. QPicture records and replays QPainter commands. What you want is QPixmap. Give a filename to the QPixmap constructor, and set this pixmap to the label using QLabel.setPixmap().

QPicture不是你想要的。QPicture记录和回放QPainter命令。你想要的是QPixmap。给QPixmap构造函数一个文件名,然后使用qlab . setpixmap()将该像素映射设置为标签。

An implementation example in python would be:

python中的实现示例是:

label = QLabel() 
pixmap = QPixmap('path_to_your_image')
label.setPixmap(pixmap)

#3


-1  

Use this snippet in your GUI script:

在您的GUI脚本中使用这个代码片段:

image = QtGui.QLabel(self)
image.setGeometry(50, 40, 250, 250)
pixmap = QtGui.QPixmap("C:\\address\\to\\your_image.png")
image.setPixmap(pixmap)
image.show()