Skip to content

魔法窗口:在主线程里调 Qt ​

PY → GUI · 主线程

控制器方法不跑在 Qt 主线程上 —— 普通 def 在线程池里,async def 在 asyncio 事件循环里。 所以你不能在控制器里直接 QMessageBox.information(...) 或者改 QLabel.setText(...):Qt 控件只能在主线程碰。

AxonMagicGUI 解决的就是这件事:把一段函数送回主线程执行,结果用 Future 拿回来。 想在哪调就在哪调 —— 线程池里、事件循环里、定时器里都行。

Python 侧 ​

controller.py
python
from PySide6.QtCore import class QThreadQThread
from PySide6.QtWidgets import class QApplicationQApplication, class QMessageBoxQMessageBox

from axon import AxonController(web_name: str | list[str] = '*', id: Optional[str] = None, shared: bool = False)
``AxonController`` 的命名空间对象:既能当装饰器用,也挂注册表操作。
AxonController
, class AxonMagicGUIAxonMagicGUI
@AxonController(web_name: str | list[str] = '*', id: Optional[str] = None, shared: bool = False)
``AxonController`` 的命名空间对象:既能当装饰器用,也挂注册表操作。
AxonController
("MainView")
class class DemoControllerDemoController: def ask(self) -> bool: def on_main() -> bool: # 这里已经在 Qt 主线程:任何 Qt 控件都能安全使用 app = class QApplicationQApplication.instance() on_main_thread = class QThreadQThread.currentThread() is app.thread() class QMessageBoxQMessageBox.information(None, "来自 Python", "这个弹窗在主线程里弹出") return on_main_thread # run() 返回 Future;.result(5) 等 5 秒 return class AxonMagicGUIAxonMagicGUI.run(on_main).result(5)
app.js
js
const const onMainThread: anyonMainThread = await 
const Axon: {
    DemoController: {
        add(a: number, b: number): Promise<number>;
        asset_count(): Promise<number>;
        blob(size?: number): Promise<string>;
        broadcast(text: string): Promise<Array<any>>;
        call_js_mirror(payload: any): Promise<any>;
        cross_broadcast(text?: string): Promise<Record<string, any>>;
        cross_call(text?: string): Promise<any>;
        cross_close(): Promise<Array<string>>;
        cross_emit(text?: string): Promise<Array<string>>;
        cross_open(): Promise<Array<string>>;
        delayed(msg: string, ms?: number): Promise<string>;
        ... 13 more ...;
        counter: number;
    };
}

Axon 的 JS 侧语法糖。

ts
await Axon.Users.add(1, 2)          // 调 Python 方法, 位置参数直接摊开
await Axon.Users.slow([1000], {}, 300)  // 需要 kwargs / 超时时用打包写法
await Axon.Users.count              // 读 Python 属性 (getVar)
Axon.Users.count = 3                // 写 Python 属性 (setVar, fire-and-forget)

属性读是惰性的: 只有真正 await / .then / .catch / .finally 时才发请求。 取到的 dispatcher 本身是函数, 同时挂了 then/catch/finally, 让 Axon.Users.count 既能当 Promise 用, 又能当方法调用。

Axon
.
type DemoController: {
    add(a: number, b: number): Promise<number>;
    asset_count(): Promise<number>;
    blob(size?: number): Promise<string>;
    broadcast(text: string): Promise<Array<any>>;
    call_js_mirror(payload: any): Promise<any>;
    cross_broadcast(text?: string): Promise<Record<string, any>>;
    cross_call(text?: string): Promise<any>;
    cross_close(): Promise<Array<string>>;
    cross_emit(text?: string): Promise<Array<string>>;
    cross_open(): Promise<Array<string>>;
    delayed(msg: string, ms?: number): Promise<string>;
    fail(reason?: string): Promise<null>;
    ... 12 more ...;
    counter: number;
}
DemoController
.ask();

先初始化

建好 QApplication 之后要调用一次 AxonMagicGUI(),否则 run() 抛 RuntimeError("init AxonMagicGUI first"):

main.py
app = QApplication(sys.argv)
AxonMagicGUI()                 # 只需要一次
view = AxonGUI("MainView", url)

要点 ​

  • 只在需要碰 Qt 对象时用:纯计算、读写文件、发请求不需要绕主线程。
  • run() 是异步排队的:它把函数丢进主线程的事件队列,结果通过 Future 拿; 不关心结果可以不取,但不能指望它"立刻"执行。
  • on_main 里别阻塞:time.sleep、同步网络请求会让整个界面卡住 —— 这段跑在主线程上。
  • async def 里想 await 它:await asyncio.wrap_future(AxonMagicGUI.run(on_main))。

相关

一次要把调用发给所有窗口,用广播。