-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpytk_gui_builder.py
More file actions
144 lines (124 loc) · 4.88 KB
/
pytk_gui_builder.py
File metadata and controls
144 lines (124 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# Python wrapper for executing PAGE. Running under Python removes the
# requirement of installing Tcl/Tk. Python 3 is required for running
# PAGE and the Python modules generated by PAGE.
import sys
import os, os.path
def is_pyinstaller():
"""检测是否在PyInstaller打包环境中运行"""
return getattr(sys, 'frozen', False)
def get_base_dir():
"""获取正确的基目录"""
if is_pyinstaller():
# PyInstaller打包环境下,资源文件在临时目录中
if hasattr(sys, '_MEIPASS'):
return sys._MEIPASS
else:
return os.path.dirname(sys.executable)
else:
# 正常Python环境下,使用脚本文件所在目录
return os.path.dirname(os.path.abspath(sys.argv[0]))
try:
import tkinter as tk
py_version = 3
except ImportError:
import Tkinter as tk
import tkMessageBox
py_version = 2
print("py_version = %s" % py_version)
if py_version < 3:
root = tk.Tk()
root.withdraw()
tkMessageBox.showerror("Error", "Python 3 required!\nExiting.")
sys.exit(1)
root.mainloop()
root = tk.Tk()
root.withdraw()
# 设置PAGE_HOME环境变量,这对PAGE正常运行很重要
base_dir = get_base_dir()
os.environ['PAGE_HOME'] = base_dir
root.tk.eval('set argv {}; set argc 0')
# 构建正确的page.tcl路径
page_tcl_path = os.path.join(base_dir, 'page.tcl')
# 检查文件是否存在,如果不存在则尝试其他位置
if not os.path.exists(page_tcl_path):
# 尝试当前工作目录
page_tcl_path = os.path.join(os.getcwd(), 'page.tcl')
if not os.path.exists(page_tcl_path):
# 尝试脚本所在目录
script_dir = os.path.dirname(os.path.abspath(__file__))
page_tcl_path = os.path.join(script_dir, 'page.tcl')
cmd = "source {" + page_tcl_path + "}"
print(f"PAGE_HOME: {base_dir}")
print(f"Python executable: {sys.executable}")
print(f"MEIPASS: {getattr(sys, '_MEIPASS', 'Not available')}")
print(f"Current working dir: {os.getcwd()}")
print(f"Script dir: {os.path.dirname(os.path.abspath(__file__)) if '__file__' in globals() else 'Not available'}")
print(f"Looking for page.tcl at: {page_tcl_path}")
print(f"File exists: {os.path.exists(page_tcl_path)}")
if os.path.exists(page_tcl_path):
print(f"File size: {os.path.getsize(page_tcl_path)} bytes")
else:
print("ERROR: page.tcl not found!")
# 列出目录内容帮助调试
print(f"Contents of {os.path.dirname(page_tcl_path)}:")
try:
for item in os.listdir(os.path.dirname(page_tcl_path)):
print(f" {item}")
except:
pass
for a in sys.argv[1:]: # Skip argv[0] (the filename of this script)
root.tk.eval("lappend argv {%s}; incr argc" % a)
try:
if os.path.exists(page_tcl_path):
# 添加Tcl错误处理
root.tk.eval('''
proc bgerror {msg} {
puts stderr "Tcl ERROR: $msg"
puts stderr "ERROR INFO: $::errorInfo"
puts stderr "ERROR CODE: $::errorCode"
exit 1
}
''')
# 添加更多的调试信息
print("=== Tcl Environment Debug Info ===")
try:
print(f"auto_path: {root.tk.eval('set auto_path')}")
print(f"tcl_pkgPath: {root.tk.eval('set tcl_pkgPath')}")
print(f"tcl_library: {root.tk.eval('set tcl_library')}")
print(f"tk_library: {root.tk.eval('set tk_library')}")
except Exception as e:
print(f"Error getting Tcl environment: {e}")
# 检查Img目录是否存在
img_dirs = ['Img/Img1.4.13-Darwin64', 'Img/Img1.4.13-Linux64', 'Img/Img1.4.13-win32', 'Img/Img1.4.13-win64']
for img_dir in img_dirs:
full_path = os.path.join(base_dir, img_dir)
print(f"Checking {img_dir}: {'EXISTS' if os.path.exists(full_path) else 'NOT FOUND'}")
if os.path.exists(full_path):
try:
files = os.listdir(full_path)
print(f" Contents: {len(files)} files")
if 'pkgIndex.tcl' in files:
print(f" pkgIndex.tcl found")
else:
print(f" pkgIndex.tcl NOT found")
except:
print(f" Cannot list contents")
print("=== Executing page.tcl ===")
root.tk.eval(cmd)
print("=== page.tcl executed successfully ===")
# sys.exit()
else:
print("Cannot find page.tcl file!")
input("Press Enter to continue...")
except Exception as e:
print(f"Error executing page.tcl: {e}")
import traceback
traceback.print_exc()
# 尝试获取Tcl错误信息
try:
tcl_error = root.tk.eval('set ::errorInfo')
print(f"Tcl Error Details: {tcl_error}")
except:
print("Could not retrieve Tcl error details")
input("Press Enter to continue...")
root.mainloop()