python 3 如何将图片放入我的程序中
python 3 how to put pics inside my program
我有一个程序和一些我在程序中使用的图片。
icon.addPixmap(QtGui.QPixmap("logo_p3.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.label_6.setPixmap(QtGui.QPixmap("Logo-4.jpg"))
图片与程序在同一个文件夹中。
有没有办法把图片放在程序里? (虽然它们只是在文件夹中,但可以轻松更改或删除它们,我不希望这种情况发生)
可能是这样的:
k=b'bytes of pic here'
self.label_6.setPixmap(QtGui.QPixmap(k))
或任何其他方法。
我正在使用 py2exe 构建可执行文件(但即使使用选项 'compressed':正确 - 我的 2 张图片就在文件夹中。他们不想进入 exe 文件的内部)。也许有办法让它们从文件夹中消失并进入程序。
谢谢。
Qt 正在使用 resource system for this task. This is also supported by pyqt. There are a few answers here on SO already: here and here
这是一个简单的例子:
首先,创建一个资源文件(例如,resources.qrc)。
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/images">
<file alias="image.png">images/image.png</file>
</qresource>
</RCC>
然后将资源文件编译成python模块:
pyrcc5 -o resources_rc.py resources.qrc
然后包含资源文件,当您创建像素图时,使用资源符号。
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QLabel
from PyQt5.QtGui import QPixmap
import resources_rc
class Form(QWidget):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
mainLayout = QGridLayout()
pixmap = QPixmap(':/images/image.png') # resource path starts with ':'
label = QLabel()
label.setPixmap(pixmap)
mainLayout.addWidget(label, 0, 0)
self.setLayout(mainLayout)
self.setWindowTitle("Hello Qt")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
screen = Form()
screen.show()
sys.exit(app.exec_())
假设文件结构如下:
|-main.py # main module
|-resources.qrc # the resource xml file
|-resouces_rc.py # generated resource file
|-images # folder with images
|--images/image.png # the image to load
我有一个程序和一些我在程序中使用的图片。
icon.addPixmap(QtGui.QPixmap("logo_p3.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.label_6.setPixmap(QtGui.QPixmap("Logo-4.jpg"))
图片与程序在同一个文件夹中。 有没有办法把图片放在程序里? (虽然它们只是在文件夹中,但可以轻松更改或删除它们,我不希望这种情况发生)
可能是这样的:
k=b'bytes of pic here'
self.label_6.setPixmap(QtGui.QPixmap(k))
或任何其他方法。
我正在使用 py2exe 构建可执行文件(但即使使用选项 'compressed':正确 - 我的 2 张图片就在文件夹中。他们不想进入 exe 文件的内部)。也许有办法让它们从文件夹中消失并进入程序。
谢谢。
Qt 正在使用 resource system for this task. This is also supported by pyqt. There are a few answers here on SO already: here and here
这是一个简单的例子:
首先,创建一个资源文件(例如,resources.qrc)。
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/images">
<file alias="image.png">images/image.png</file>
</qresource>
</RCC>
然后将资源文件编译成python模块:
pyrcc5 -o resources_rc.py resources.qrc
然后包含资源文件,当您创建像素图时,使用资源符号。
from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QLabel
from PyQt5.QtGui import QPixmap
import resources_rc
class Form(QWidget):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
mainLayout = QGridLayout()
pixmap = QPixmap(':/images/image.png') # resource path starts with ':'
label = QLabel()
label.setPixmap(pixmap)
mainLayout.addWidget(label, 0, 0)
self.setLayout(mainLayout)
self.setWindowTitle("Hello Qt")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
screen = Form()
screen.show()
sys.exit(app.exec_())
假设文件结构如下:
|-main.py # main module
|-resources.qrc # the resource xml file
|-resouces_rc.py # generated resource file
|-images # folder with images
|--images/image.png # the image to load