pyqt5将ui和资源文件一起打包为exe代码示例
时间: 2024-01-16 22:05:16 浏览: 294
以下是一个示例代码,可以将UI文件和资源文件一起打包为exe文件:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi
from PyQt5.QtGui import QIcon, QPixmap
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
loadUi('mainwindow.ui', self) # 加载UI文件
self.setWindowTitle('My App')
self.setWindowIcon(QIcon('icon.png')) # 加载资源文件
self.label.setPixmap(QPixmap('image.png')) # 加载资源文件
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
要将UI文件和资源文件打包为exe文件,可以使用PyInstaller工具。首先需要安装PyInstaller:
```bash
pip install pyinstaller
```
然后,在命令行中运行以下命令:
```bash
pyinstaller --onefile --windowed main.py
```
其中,`main.py`是上面的代码文件名。这个命令将会生成一个独立的exe文件,其中包含了UI文件和资源文件。
阅读全文
相关推荐


















