Files
tk-mini-program-PC/src/utils/pythonBridge.js

37 lines
739 B
JavaScript
Raw Normal View History

2025-04-03 16:41:54 +08:00
// pythonBridge.js
import { ref, onMounted } from 'vue';
export function usePythonBridge() {
const bridge = ref(null);
// 初始化 QWebChannel
const initBridge = () => {
new QWebChannel(qt.webChannelTransport, (channel) => {
bridge.value = channel.objects.bridge;
});
};
// 调用 Python 方法
const callPython = (data) => {
if (bridge.value) {
bridge.value.stringFromJs(data);
}
};
// 从 Python 获取数据
const getPythonData = () => {
if (bridge.value) {
bridge.value.stringToJs(function (result) {
alert(result);
});
}
};
// 在组件挂载时初始化桥接
onMounted(initBridge);
return {
callPython,
getPythonData
};
}