我在使用PyQt 6(版本6.5.2)时使用了以下代码:
self.font_id = QFontDatabase.addApplicationFont('conf/font.otf')
self.font_family = QFontDatabase.applicationFontFamilies(font_id)
for name, classtype in self.__dict__.items():
try:
classtype.setFont(QFont(self.font_family[0], int(self.base_cfg['Config']['fontSize']), QFont.Weight.Bold))
except AttributeError:
pass
无论我在PyCharm中运行或调试程序,我总是得到消息“Process finished with exit code -1073741819(0xC 0000005)"。但是当我注解掉代码的前两行时,程序正常运行。
经过多次尝试,我现在确信**QFontDatabase.addApplicationFont('conf/font. otf')**本身对我来说是最小的可复制示例。即使我只执行这两行,脚本也会崩溃:
from PyQt6.QtGui import QFontDatabase
QFontDatabase.addApplicationFont('conf/font.otf')
我已经在Python 3.9.4/3.10.2/3.11.4上测试过了,情况仍然是一样的。我使用的是Windows 11版本22621.2070。我已经搜索了相关信息,但还没有找到任何有用的东西。
我在PyCharm Run/编译器中尝试了**-X dev**解释器选项,得到了如下结果:
Windows fatal exception: access violation
Thread 0x00002a00 (most recent call first):
File "D:\Software\PyCharm Community Edition 2023.2\plugins\python-ce\helpers\pydev\pydevd.py", line 138 in _on_run
File "D:\Software\PyCharm Community Edition 2023.2\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 219 in run
File "D:\Software\Python\lib\threading.py", line 1009 in _bootstrap_inner
File "D:\Software\Python\lib\threading.py", line 966 in _bootstrap
Thread 0x00004cd4 (most recent call first):
File "D:\Software\PyCharm Community Edition 2023.2\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 293 in _on_run
File "D:\Software\PyCharm Community Edition 2023.2\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 219 in run
File "D:\Software\Python\lib\threading.py", line 1009 in _bootstrap_inner
File "D:\Software\Python\lib\threading.py", line 966 in _bootstrap
Thread 0x00004728 (most recent call first):
File "D:\Software\Python\lib\threading.py", line 324 in wait
File "D:\Software\Python\lib\queue.py", line 180 in get
File "D:\Software\PyCharm Community Edition 2023.2\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 368 in _on_run
File "D:\Software\PyCharm Community Edition 2023.2\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 219 in run
File "D:\Software\Python\lib\threading.py", line 1009 in _bootstrap_inner
File "D:\Software\Python\lib\threading.py", line 966 in _bootstrap
有人能帮我吗?
1条答案
按热度按时间k3bvogb11#
感谢@musicamante的提醒:
顾名思义,
addApplicationFont()
向应用程序添加字体,但您根本没有创建任何应用程序。在调用addApplicationFont()
之前,至少要创建一个QApplication示例。我确实犯了一个错误。问题是这样发生的:
我的应用程序窗口
gui.py
的结构如下所示:然后,在我的应用程序的主要入口:
在整个过程中,
addApplicationFont()
在**app = QApplication(sys.argv)
之前执行**。将这两行移动到__init__()
部分后,一切都变得正常: