Pywebview编写桌面应用入门

官网 https://pywebview.flowrl.com/guide/

安装

pip install pywebview

示例:写个计算器


<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>计算器应用</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }
        .calculator {
            background: rgba(255, 255, 255, 0.1);
            padding: 30px;
            border-radius: 15px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.3);
            text-align: center;
            backdrop-filter: blur(10px);
        }
        input {
            width: 200px;
            padding: 15px;
            margin: 10px;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            text-align: center;
        }
        button {
            background: #ff6b6b;
            color: white;
            border: none;
            padding: 15px 30px;
            margin: 10px;
            border-radius: 8px;
            cursor: pointer;
            font-size: 16px;
            transition: background 0.3s;
        }
        button:hover {
            background: #ff5252;
        }
        #result {
            font-size: 24px;
            margin: 20px 0;
            padding: 15px;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 8px;
        }
    </style>
</head>
<body>
    <div class="calculator">
        <h1>🧮 超级计算器</h1>
        <input type="number" id="num1" placeholder="输入第一个数字">
        <input type="number" id="num2" placeholder="输入第二个数字">
        <br>
        <button onclick="calculate('add')">➕ 加法</button>
        <button onclick="calculate('subtract')">➖ 减法</button>
        <button onclick="calculate('multiply')">✖️ 乘法</button>
        <button onclick="calculate('divide')">➗ 除法</button>
        <div id="result">结果将在这里显示</div>
    </div>

    <script>
        function calculate(operation) {
            const num1 = parseFloat(document.getElementById('num1').value);
            const num2 = parseFloat(document.getElementById('num2').value);

            if (isNaN(num1) || isNaN(num2)) {
                document.getElementById('result').innerText = '请输入有效的数字!';
                return;
            }

            // 调用Python函数
            pywebview.api.calculate(num1, num2, operation).then(result => {
                document.getElementById('result').innerText = `结果:${result}`;
            });
        }
    </script>
</body>
</html>
# app.py
import webview
import os

class Api:
    def calculate(self, num1, num2, operation):
        """处理计算逻辑"""
        try:
            if operation == 'add':
                result = num1 + num2
            elif operation == 'subtract':
                result = num1 - num2
            elif operation == 'multiply':
                result = num1 * num2
            elif operation == 'divide':
                if num2 == 0:
                    return "错误:除数不能为0"
                result = num1 / num2
            else:
                return "未知操作"

            return round(result, 2)
        except Exception as e:
            return f"计算错误:{str(e)}"

def main():
    # 创建API实例
    api = Api()

    # 获取HTML文件的绝对路径
    html_path = os.path.abspath('index.html')

    # 创建窗口
    webview.create_window(
        title='超级计算器',
        url=html_path,
        js_api=api,
        width=500,
        height=600,
        resizable=False
    )

    # 开启开发者工具(调试时使用)
    webview.start(debug=True)

if __name__ == '__main__':
    main()
python app.py
posted @ 2025-07-09 10:01  朝阳1  阅读(84)  评论(0)    收藏  举报