如何将图片添加到 PyQt4 中的 QWidget [英] How can I add a Picture to a QWidget in PyQt4

查看:62
本文介绍了如何将图片添加到 PyQt4 中的 QWidget的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何向 QWidget 添加或导入图片?我找到了一个线索.我可以添加一个 Label 并在该标签中添加一个 Picture.我需要争论对于 QPicture().我可以使用的可能是,QLabel.setPicture(self.QPicture).

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).

推荐答案

这里有一些代码可以完成你和@erelender 描述的事情:

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

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_())

通用QWidget 没有setPixmap().如果这种方法对您不起作用,您可以考虑制作自己的从 QWidget 派生的自定义小部件,并覆盖 paintEvent 方法以显示图像.

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.

这篇关于如何将图片添加到 PyQt4 中的 QWidget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆