PyQtImageViewer 项目教程
1. 项目的目录结构及介绍
PyQtImageViewer 项目的目录结构如下:
PyQtImageViewer/
├── LICENSE
├── README.md
├── QtImageViewer.py
├── QtImageStackViewer.py
├── setup.py
└── gitignore
目录结构介绍
LICENSE
: 项目许可证文件,采用 MIT 许可证。README.md
: 项目说明文档,包含项目的基本介绍和使用方法。QtImageViewer.py
: 核心文件,包含图像查看器的主要功能。QtImageStackViewer.py
: 多页图像查看器,基于QtImageViewer.py
实现。setup.py
: 项目安装脚本。gitignore
: Git 忽略文件配置。
2. 项目的启动文件介绍
项目的启动文件是 QtImageViewer.py
和 QtImageStackViewer.py
。
QtImageViewer.py
QtImageViewer.py
是项目的核心文件,包含图像查看器的主要功能。以下是启动该文件的示例代码:
import sys
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QApplication
from QtImageViewer import QtImageViewer
# 自定义鼠标点击处理槽函数
def handleLeftClick(x, y):
row = int(y)
column = int(x)
print(f"Pixel (row={row}, column={column})")
if __name__ == '__main__':
# 创建 QApplication 实例
app = QApplication(sys.argv)
# 创建图像查看器实例
viewer = QtImageViewer()
# 连接鼠标点击信号到自定义槽函数
viewer.leftMouseButtonReleased.connect(handleLeftClick)
# 显示查看器并运行应用程序
viewer.show()
sys.exit(app.exec())
QtImageStackViewer.py
QtImageStackViewer.py
是多页图像查看器,基于 QtImageViewer.py
实现。以下是启动该文件的示例代码:
import sys
from PyQt6.QtWidgets import QApplication
from QtImageStackViewer import QtImageStackViewer
if __name__ == '__main__':
# 创建 QApplication 实例
app = QApplication(sys.argv)
# 创建多页图像查看器实例
viewer = QtImageStackViewer()
# 加载图像堆栈文件(会弹出文件对话框)
viewer.open()
# 显示查看器并运行应用程序
viewer.show()
sys.exit(app.exec())
3. 项目的配置文件介绍
项目的配置文件主要是 setup.py
,它用于项目的安装和分发。
setup.py
setup.py
文件的内容如下:
from setuptools import setup
setup(
name='PyQtImageViewer',
version='0.1',
description='PyQt image viewer widget with mouse zooming and panning',
author='Marcel Goldschen-Ohm',
author_email='marcel.goldschen@gmail.com',
url='https://github.com/marcel-goldschen-ohm/PyQtImageViewer',
py_modules=['QtImageViewer', 'QtImageStackViewer'],
install_requires=[
'PyQt6',
'numpy',
'Pillow'
],
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
],
)
配置文件介绍
name
: 项目名称。version
: 项目版本。description
: 项目描述。author
: 项目作者。author_email
: 作者邮箱。url
: 项目仓库地址。py_modules
: 项目包含的模块。install_requires
: 项目依赖