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)
Python在线编辑器
于 2025-02-01 23:59:49 首次发布