Python在线编辑器

from flask import Flask, render_template, request, jsonify
import sys
from io import StringIO
import contextlib
import subprocess
import importlib
import threading
import time
import ast
import re

app = Flask(__name__)

RESTRICTED_PACKAGES = {
    'tkinter': '抱歉,在线编译器不支持 tkinter,因为它需要图形界面环境。请在本地运行需要GUI的代码。',
    'tk': '抱歉,在线编译器不支持 tk/tkinter,因为它需要图形界面环境。请在本地运行需要GUI的代码。',
    'pygame': 'pygame将被转换为Web版本运行'  # 不再限制pygame,而是转换它
}

def convert_tkinter_to_web(code):
    """将tkinter代码转换为Web等效实现"""
    # 解析Python代码
    tree = ast.parse(code)
    
    # 提取窗口属性
    window_props = {
        'title': 'Python GUI',
        'width': '700',
        'height': '500',
        'buttons': [],
        'labels': [],
        'entries': [],
        'layout': []
    }
    
    # 用于存储函数定义
    functions = {}
    
    # 首先收集所有函数定义
    for node in ast.walk(tree):
        if isinstance(node, ast.FunctionDef):
            functions[node.name] = ast.unparse(node)
    
    # 分析代码中的tkinter组件
    for node in ast.walk(tree):
        if isinstance(node, ast.Assign):
            if isinstance(node.value, ast.Call):
                # 提取窗口标题
                if hasattr(node.value.func, 'attr') and node.value.func.attr == 'Tk':
                    for subnode in ast.walk(tree):
                        if isinstance(subnode, ast.Call) and hasattr(subnode.func, 'attr'):
                            if subnode.func.attr == 'title' and len(subnode.args) > 0:
                                window_props['title'] = ast.literal_eval(subnode.args[0])
                            elif subnode.func.attr == 'geometry' and len(subnode.args) > 0:
                                geom = ast.literal_eval(subnode.args[0])
                                match = re.match(r'(\d+)x(\d+)', geom)
                                if match:
                                    window_props['width'] = match.group(1)
                          
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mosquito_lover1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值