(7)python开发经验
文章目录
- 1 找不到资源文件
- 2 使用subprocess执行时有黑色弹窗
- 3 找不到exec
- 4 pyside6-project lupdate的bug
- 5 找不到pyd模块
- 6 pyd模块编码错误
- 7 运行显示`Qt platform plugin "windows" in "`
- 8 `tr()`包含的字符串无法被翻译
更多精彩内容 |
---|
👉内容导航 👈 |
👉Qt开发 👈 |
👉python开发 👈 |
1 找不到资源文件
使用rcc将qrc文件编译为py文件后,找不到资源文件可能是因为使用的是python原生的函数,这里是一个大坑,例如:
print(QFile.exists(":/style/style.css")) # true
print(os.path.exists(":/style/style.css")) # false
# 打开失败
with open(":/style.css", "r") as f:app.setStyleSheet(f.read()) # 设置样式表
2 使用subprocess执行时有黑色弹窗
-
如下所示,可通过
shell=True
指定使用shell执行,不会出现弹窗。ret = subprocess.run(['adb', 'connect', '127.0.0.1:7555'], shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
3 找不到exec
错误信息:
Traceback (most recent call last):File "E:\Code\py\PythonProject\main.py", line 27, in <module>sys.exit(app.exec())
AttributeError: 'PySide2.QtWidgets.QApplication' object has no attribute 'exec'
解决办法:
- PySide2中使用的是
sys.exit(app.exec_())
,PySide6使用的是sys.exit(app.exec())
4 pyside6-project lupdate的bug
-
使用
pyside6-project lupdate
命令可以在pyside6中生成翻译使用的ts文件,但是有时候我们想将ts文件放在子文件夹下,却发现无法实现; -
并且使用
pyside6-project lupdate
命令无法清除已经失效的文本; -
这是因为
pyside6-project lupdate
中使用的是ts文件名,不包含路径,所以就是在工程路径下,并且有几个ts文件就会执行几次。(.venv) PS E:\Code\py\Test> pyside6-project.exe lupdate pyside6-lupdate main.py mainwindow.py TableWidget.py mainwindow.ui logwidget.ui control.ui -ts Test_zh_CN.ts
-
实现代码如下所示:for用来遍历ts文件,有几个ts文件就会调用run_command()执行几次。
cmd_prefix.append("-ts")for ts_file in self.project.ts_files:if requires_rebuild(source_files, ts_file):cmd = cmd_prefixcmd.append(ts_file.name)run_command(cmd, cwd=project_dir)
-
解决办法:打开
.venv\Lib\site-packages\PySide6\scripts\project.py
文件,在def lupdate(self)
函数中修改为下面所示代码。def lupdate(self):for sub_project_file in self.project.sub_projects_files:Project(project_file=sub_project_file).lupdate()if not self.project.ts_files:print(f"{self.project.project_file.name}: No .ts file found.",file=sys.stderr)returnsource_files = self.project.python_files + self.project.ui_filesproject_dir = self.project.project_file.parentcmd_prefix = [LUPDATE_CMD] + [os.fspath(p.relative_to(project_dir)) for p in source_files]cmd_prefix.append("-ts")ts_files = []for ts_file in self.project.ts_files:if requires_rebuild(source_files, ts_file):ts_dir = ts_file.parentif not ts_dir.exists():ts_dir.mkdir(parents=True, exist_ok=True)ts_files.append(ts_file.resolve().as_posix())if ts_files:cmd = cmd_prefix;cmd.extend(ts_files)cmd.append("-no-obsolete") # 清除失效文本run_command(cmd, cwd=project_dir)
-
运行结果如下所示:会将ts文件生成到指定路径中,并且只执行一次。
(.venv) PS E:\Code\py\Test> pyside6-project.exe lupdate pyside6-lupdate main.py mainwindow.py TableWidget.py mainwindow.ui logwidget.ui control.ui -ts E:/Code/py/Test/Language/Test_zh_CN.ts E:/Code/py/Test/Language/Test_en_US.ts
5 找不到pyd模块
- 可能是python版本不一致,例如pyd是使用python3.12编译,使用python3.8导入就找不到。
- 如果
PYBIND11_MODULE
中设置的模块名称和pybind11_add_module
指定编译生成的名称不同也找不到。
6 pyd模块编码错误
- python默认使用utf-8编码,如果使用pybind11导出的cpp文件使用的是其他编码,则生成的pyd模块导入时会报错。
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb2 in position 5: invalid start byteThe above exception was the direct cause of the following exception:Traceback (most recent call last):File "E:\Code\py\PythonProject1\main.py", line 1, in <module>import pcba_plugin
ImportError: initialization failed
7 运行显示Qt platform plugin "windows" in "
qt.qpa.plugin: Could not find the Qt platform plugin "windows" in ""
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
- 解决办法:路径不要有中文名。
8 tr()
包含的字符串无法被翻译
-
在PySide6中,
self.tr()
方法用于翻译字符串。 -
如果你发现
self.tr(f"中文{test}")
这样的格式无法被翻译,可能是因为Qt的翻译系统无法识别这种格式的字符串。 -
Qt的翻译工具(如
lupdate
)通常需要字符串是静态的,以便能够提取这些字符串进行翻译。 -
解决办法,改为:
self.tr("设备{arg}已离线:").format(arg=text) # 或者 self.tr("发现新设备:") + device