跳转到主要内容

适用于PyQt{4,5}应用程序的distutils构建扩展

项目描述

适用于PyQt{4,5}应用程序的distutils构建扩展

根据setup.cfg中的配置变量在树中构建UI特定元素。将工具链的运行委托给几个内部构建命令。

假设以下布局

project/
  i18n/               # keep all translation specific files here
  i18n/project.pro    # translation project file (generated, optionally)
  ui/                 # all designer forms, may contain sub folders
  project.qrc         # project resource definition (generated)
  project_rc.py       # project resources (generated)
setup.py              # distutils/setuptools module for the project
setup.cfg             # setup configuration
...

翻译

适当的翻译是获取所有表单和源模块中的可翻译字符串,使用linguist进行翻译,并将文本表示形式(.ts)转换为二进制形式(.qm),适合QTranslator实例。

完成这项任务有两种方法:使用中间项目文件(.pro),该文件可以通过内置命令 gentrpro 生成,或者直接向工具 pylupdatelrelease 提供globbing参数。遗憾的是,后者受一些bug的影响。因此,首选方法是使用 .pro 文件。

因为翻译源文件(.ts)使用相对路径引用表单和源文件,而工具 pylupdatelrelease 是相对于 .pro 文件位置操作的,并且我们希望将所有翻译特定文件放在一个地方,所以我们相对于 i18n/ 运行翻译工具链。

新增语言

  • i18n/ 中创建一个适当命名的文件,例如 touch i18n/project_lang.ts

  • 使用 setup.py build_ui 构建初始翻译源

  • 使用 linguist 一次性设置语言参数,例如 linguist i18n/project_lang.ts

翻译依赖于在源代码中正确使用的 tr() 和 translate()。

表单

ui/ 和子文件夹包含所有设计表单。你不应该在同一个文件夹中混合源代码和表单,因为表单被翻译成 Python 源文件,你希望以不同的方式处理它们(例如,排除在翻译之外,因为翻译源已经从表单生成。

<form>.ui 文件被翻译成 ui_<form>.py。通常,它包含一个表单,其中顶级对象是该模块的驼峰命名名称。例如:form.ui 包含一个名为 Form 的小部件,该小部件可以从顶级模块导入

from ui.ui_form import Ui_Form

通常,这个表单通过多继承进行了子类化

class Form(QWidget, Ui_Form):
    def __init__(self, parent = None):
        super(Form, self).__init__(parent)
        self.setupUi(self)

资源

资源集合文件(.qrc)定义了包含在单个模块中的资源。通常,这包括图像、翻译文件(.qm)和其他静态数据。这些资源通过以下方式访问

app.setWindowIcon(QIcon(":/images/icon.png"))

注意“:”前缀。资源文件通常在主模块的早期包含

import project_rc # __IGNORE_WARNING__ (this is not referenced any further)

并且包含的资源在所有模块中都是可用的。

distutils_ui 包含一个内置命令 genqrc,可以从globbing模式生成 .qrc 文件。 genqrc 支持两个特定选项: prefixstrip。Prefix 允许将所有资源放在一个自定义前缀下,而 strip 会从对象中删除路径。Strip 要求所有文件都是唯一命名的,否则某些对象不可访问。命令 pyrccproject.qrc 生成资源模块 project_rc.py

命令

gentrprogenqrc 命令是内置的,因此它们没有定义自己的命令,而是直接处理输入和输出文件。所有其他命令调用外部工具,这些工具必须是可用的,并且在 setup.cfg 中的 command 参数中指定。

命令参数使用 {macro} 表达式,它引用同一部分中的其他参数,例如 {infiles}{outfiles},以及像 {name}{version} 这样的元数据参数。这些参数可以与文件globbing模式混合使用。

infilesoutfiles 参数定义输入文件和目标。

exclude 参数从 infiles 中删除匹配的元素。

“chdir”参数允许更改该命令的执行路径,也受元数据宏扩展的影响。

提供了一种特殊的命令模式:singlefile。它用于逐个调用每个输入文件的命令。在此模式下,还提供了额外的宏,可用于进一步控制输出文件:{path}{filename}{fileext}。请检查 pyuicpyrcc 命令的模板以获取示例。

如果您只想使用命令子集:只需在 [build_ui] 部分相应地定义 commands 即可。

setup.py

from distutils.command.build import build
from build_ui import build_ui

[...]

cmdclass = {
    'build_ui': build_ui,
}

# Optional: inject ui specific build into standard build process
build.sub_commands.insert(0, ('build_ui', None))

[...]

setup(
    name = name,
    version = version,
    [...]
    cmdclass = cmdclass
)

PyQt5 模板的 setup.cfg

[build_ui]
# control the tool chain (default: run all commands)
#commands = gentrpro, pylupdate, lrelease, pyuic, genqrc, pyrcc

[gentrpro]
# pro files are processed relative to their location, cope with it:
# generate pro file with relative paths from i18n, and call
# pylupdate and lrelease from within i18n
chdir = {name}/i18n
infiles = ../ui/*.ui ../*.py *.ts
outfiles = {name}.pro
exclude = ../{name}_rc.py

[pylupdate]
# update translation source files (*.ts) from forms and source files
# -noobsolete will remove all outdated translations
chdir = {name}/i18n
command = pylupdate5 -verbose {infiles}
infiles = {name}.pro
outfiles = {name}_*.ts

[lrelease]
# convert translation source files into binary representation (*.qm)
chdir = {name}/i18n
command = lrelease-qt5 {infiles}
infiles = {name}.pro
outfiles = {name}_*.qm

[pyuic]
# generate python source files from UI definitions (*.ui)
command = pyuic5 -x -o {outfiles} {infiles}
infiles = {name}/ui/*.ui
outfiles = {name}/ui/ui_{filename}.py
singlefile = true

[genqrc]
# generate a resource description file (*.qrc)
chdir = {name}
infiles = images/*.png i18n/*.qm
outfiles = {name}.qrc
# these are specific for genqrc
strip = false
prefix =

[pyrcc]
# generate a resource module from qrc file
command = pyrcc5 -o {outfiles} {infiles}
infiles = {name}/{name}.qrc
outfiles = {name}/{name}_rc.py
singlefile = true

通过以下命令触发纯 UI 构建:

python3 setup.py build_ui [-f|--force]

可以通过类似的方式对生成的文件进行清理

python3 setup.py build_ui [-C|--clean]

注释

  • 避免在文件名中使用空格

  • “.pro”文件方法会导致虚假构建

调试

python3 setup.py -v build_ui

作者

  1. 2016 Hans-Peter Jansen <hpj@urpla.net>

许可协议

MIT,版权(c)2016,Hans-Peter Jansen,请参阅 LICENSE.txt

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源分布

distutils_ui-0.1.3.tar.gz (17.5 kB 查看哈希值)

上传时间

由以下提供支持