python - 在非GUI代码中访问fbs资源文件

标签 python pyqt5 fbs

使用fbs要打包 PyQt5 应用程序,从非 GUI 代码访问资源文件的最简洁方法是什么?

在下面的示例中,我制作了一个简单的 PyQt5 应用程序,其中一个窗口包含一个按钮。该按钮会触发一个函数,该函数将读取 .json 资源文件并在控制台中打印其属性 messagelogic.py 中定义的函数 print_data() 也应该在没有 GUI 应用程序的情况下工作,例如供其他 Python 脚本使用。因此,它不能作为 MainWindow 类的方法移动。

使用应用程序上下文的 get_resource() 方法可以轻松方便地获取 mainwindow.ui 的路径。不幸的是,print_data() 无法使用它来获取 static_data.json 的路径,因为该函数无法访问应用程序上下文。

最好的解决方案是什么?

  • 仅对 gui 相关的资源文件使用 fbs 的资源系统,并将 static_data.json 与 python 文件一起保留?
  • 尝试访问 MainWindow 类之外的应用程序上下文?
  • 使用另一个Python资源库(例如pkg_resources)来处理非GUI相关的资源文件?
  • 使用其他工具(例如 cx_freeze)来打包应用吗?
<小时/>

目录结构:

|-- src
|  |-- main
|  |  |-- python
|  |  |  |-- main.py
|  |  |  |-- gui.py
|  |  |  |-- logic.py
|  |  |-- resources
|  |  |  |-- base
|  |  |  |  |-- mainwindow.ui
|  |  |  |  |-- static_data.json

main.py的内容:

from fbs_runtime.application_context.PyQt5 import ApplicationContext
import sys
from gui import MainWindow

if __name__ == '__main__':
    appctxt = ApplicationContext()
    window = MainWindow(appctxt)
    window.show()
    exit_code = appctxt.app.exec_()
    sys.exit(exit_code)

gui.py 的内容:

from PyQt5.QtWidgets import QMainWindow
from PyQt5 import uic

from logic import print_data


class MainWindow(QMainWindow):
    def __init__(self, context):
        super().__init__()

        # Loading the .ui file from the resources
        self.ui = uic.loadUi(context.get_resource("mainwindow.ui"), self)

        # Binding the button to the print_data function defined in logic.py
        self.main_button.clicked.connect(print_data)

logic.py的内容:

import json


def print_data():

    # Getting the resource data filepath
    filepath = "../resources/base/static_data.json"

    # Processing the resource file
    with open(filepath) as file:
        data = json.load(file)
        print(data["message"])

最佳答案

python
├── base.py
├── gui.py
├── logic.py
└── main.py

一种可能的解决方案是在文件中创建上下文,每个人都可以访问该变量:

base.py

from fbs_runtime.application_context.PyQt5 import ApplicationContext

context = ApplicationContext()

然后在其他类中使用它:

gui.py

from PyQt5.QtWidgets import QMainWindow
from PyQt5 import uic

from base import context

from logic import print_data


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        # Loading the .ui file from the resources
        self.ui = uic.loadUi(context.get_resource("mainwindow.ui"), self)

        # Binding the button to the print_data function defined in logic.py
        self.main_button.clicked.connect(print_data)

逻辑.py

import json

from base import context


def print_data():
    # Getting the resource data filepath
    filepath = context.get_resource("static_data.json")

    # Processing the resource file
    with open(filepath) as file:
        data = json.load(file)
        print(data["message"])

ma​​in.py

import sys

from base import context
from gui import MainWindow

if __name__ == "__main__":
    window = MainWindow()
    window.resize(250, 150)
    window.show()
    exit_code = context.app.exec_()
    sys.exit(exit_code)

关于python - 在非GUI代码中访问fbs资源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60040208/

相关文章:

python - 显示来自不同线程的 QInputDialog 和其他 gui 对象

qlabel 中的 python 错误 : can not show . gif

python - QWidget::setLayout:尝试在 ProgramWindow ""上设置 QLayout "",该窗口已经有布局

python - 如何使用 fbs 在 PyQt 样式表中引用资源文件

python - 为什么 peewee 在 mysql 选择查询中包含 'id' 列?

python - 字符串操作递归函数

python - 如何使安装程序像在桌面上创建图标一样?

python - 在 Flask 中将数组处理为表单名称

python - 使用 flask 和外部上下文/cronjobs 发送电子邮件