You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Curriculum_Design/start_marktext.py

169 lines
5.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
MagicWord - MarkText风格启动器
基于MarkText开源编辑器的现代化Markdown编辑器
集成MagicWord现有功能学习模式、天气、名言等
"""
import sys
import os
import platform
import traceback
# 添加项目根目录到Python路径
project_root = os.path.dirname(os.path.abspath(__file__))
src_path = os.path.join(project_root, 'src')
sys.path.insert(0, project_root)
sys.path.insert(0, src_path)
# 设置Qt平台插件路径
def setup_qt_environment():
"""设置Qt环境变量"""
system = platform.system()
python_version = f"python{sys.version_info.major}.{sys.version_info.minor}"
possible_paths = []
if system == "Windows":
possible_paths.extend([
os.path.join(project_root, '.venv', 'Lib', 'site-packages', 'PyQt5', 'Qt5', 'plugins'),
os.path.join(sys.prefix, 'Lib', 'site-packages', 'PyQt5', 'Qt5', 'plugins'),
])
elif system == "Darwin": # macOS
possible_paths.extend([
os.path.join(project_root, '.venv', 'lib', python_version, 'site-packages', 'PyQt5', 'Qt5', 'plugins'),
os.path.join(sys.prefix, 'lib', python_version, 'site-packages', 'PyQt5', 'Qt5', 'plugins'),
'/usr/local/opt/qt5/plugins',
'/opt/homebrew/opt/qt5/plugins',
])
elif system == "Linux":
possible_paths.extend([
os.path.join(project_root, '.venv', 'lib', python_version, 'site-packages', 'PyQt5', 'Qt5', 'plugins'),
os.path.join(sys.prefix, 'lib', python_version, 'site-packages', 'PyQt5', 'Qt5', 'plugins'),
'/usr/lib/x86_64-linux-gnu/qt5/plugins',
'/usr/lib/qt5/plugins',
])
for path in possible_paths:
if os.path.exists(path) and os.path.exists(os.path.join(path, 'platforms')):
os.environ['QT_PLUGIN_PATH'] = path
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = os.path.join(path, 'platforms')
if system == "Darwin":
os.environ['QT_QPA_PLATFORM'] = 'cocoa'
os.environ['QT_MAC_WANTS_LAYER'] = '1'
elif system == "Windows":
os.environ['QT_QPA_PLATFORM'] = 'windows'
elif system == "Linux":
os.environ['QT_QPA_PLATFORM'] = 'xcb'
print(f"✅ Qt插件路径设置成功: {path}")
return True
print("⚠️ 警告未找到Qt插件路径")
return False
# 检查依赖
def check_dependencies():
"""检查必要的依赖"""
missing_deps = []
try:
import PyQt5
except ImportError:
missing_deps.append("PyQt5")
try:
import requests
except ImportError:
missing_deps.append("requests")
try:
import docx
except ImportError:
missing_deps.append("python-docx")
try:
import fitz # PyMuPDF
except ImportError:
missing_deps.append("PyMuPDF")
if missing_deps:
print(f"❌ 缺少依赖包: {', '.join(missing_deps)}")
print("请运行: pip install " + " ".join(missing_deps))
return False
return True
def main():
"""主函数"""
try:
print("🚀 启动MagicWord - MarkText风格编辑器")
# 检查依赖
if not check_dependencies():
sys.exit(1)
# 设置Qt环境
setup_qt_environment()
# 导入Qt相关模块
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
# 设置高DPI支持
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True)
QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps, True)
# 创建应用
app = QApplication(sys.argv)
app.setApplicationName("MagicWord")
app.setApplicationVersion("1.0.0")
app.setOrganizationName("MagicWord")
# 设置样式
if platform.system() != "Darwin":
app.setStyle('Fusion')
# 设置图标
icon_path = os.path.join(project_root, 'resources', 'icons', 'app_icon.png')
if os.path.exists(icon_path):
app.setWindowIcon(QIcon(icon_path))
# 导入并创建MarkText主窗口
try:
from main import MarkTextMainWindow
main_window = MarkTextMainWindow()
print("✅ MarkText编辑器启动成功")
except ImportError as e:
print(f"❌ MarkText编辑器导入失败: {e}")
print("正在尝试启动Word风格编辑器...")
try:
from word_main_window import WordStyleMainWindow
main_window = WordStyleMainWindow()
print("✅ Word风格编辑器启动成功")
except ImportError as e2:
print(f"❌ Word风格编辑器也启动失败: {e2}")
sys.exit(1)
# 显示窗口
main_window.show()
# 运行应用
exit_code = app.exec_()
print(f"👋 应用已退出,退出码: {exit_code}")
sys.exit(exit_code)
except KeyboardInterrupt:
print("\n👋 用户中断,正在退出...")
sys.exit(0)
except Exception as e:
print(f"❌ 发生未预期的错误: {e}")
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()