77class EncodingAnalyzer (tk .Tk ):
88 def __init__ (self ):
99 super ().__init__ ()
10+ self .init_ui ()
1011
12+ def init_ui (self ):
1113 self .title ("文本文件编码分析器" )
12- self .geometry ("800x600" ) # 调整窗口大小
14+ self .geometry ("800x600" )
1315
1416 self .selected_directory = None
1517 self .encoding_files = {} # 存储每种编码对应的文件列表
1618
17- # 创建界面元素
1819 self .create_widgets ()
19-
20+ self .setup_styles ()
21+
2022 def create_widgets (self ):
21- # 顶部按钮框架
23+ # 创建顶部框架
24+ self .create_top_frame ()
25+ # 创建主框架
26+ self .create_main_frame ()
27+
28+ def create_top_frame (self ):
2229 btn_frame = tk .Frame (self )
2330 btn_frame .pack (pady = 10 )
2431
25- # 选择目录按钮
2632 self .select_btn = tk .Button (btn_frame , text = "选择目录" , command = self .select_directory )
2733 self .select_btn .pack (side = tk .LEFT , padx = 5 )
2834
29- # 分析按钮
3035 self .analyze_btn = tk .Button (btn_frame , text = "开始分析" , command = self .start_analyze )
3136 self .analyze_btn .pack (side = tk .LEFT , padx = 5 )
3237
33- # 显示选中的目录
3438 self .path_label = tk .Label (self , text = "未选择目录" , wraplength = 500 )
3539 self .path_label .pack (pady = 5 )
3640
37- # 创建左右分栏框架
41+ def create_main_frame ( self ):
3842 paned = ttk .PanedWindow (self , orient = tk .HORIZONTAL )
3943 paned .pack (fill = tk .BOTH , expand = True , padx = 5 )
4044
41- # 创建左侧框架
42- left_frame = ttk .LabelFrame (paned , text = "编码统计 (点击查看文件列表)" )
43- paned .add (left_frame , weight = 1 )
45+ # 创建左侧编码统计区域
46+ self .create_encoding_frame (paned )
47+ # 创建右侧文件列表区域
48+ self .create_file_frame (paned )
49+
50+ def create_encoding_frame (self , parent ):
51+ left_frame = ttk .LabelFrame (parent , text = "编码统计 (点击查看文件列表)" )
52+ parent .add (left_frame , weight = 1 )
4453
45- # 创建左侧编码统计表格
4654 columns = ("编码" , "文件数量" , "百分比" )
4755 self .tree = ttk .Treeview (left_frame , columns = columns , show = "headings" , style = "Custom.Treeview" )
56+
4857 for col in columns :
4958 self .tree .heading (col , text = col )
5059 self .tree .column (col , width = 100 )
5160
52- # 添加滚动条
5361 tree_scroll = ttk .Scrollbar (left_frame , orient = "vertical" , command = self .tree .yview )
5462 self .tree .configure (yscrollcommand = tree_scroll .set )
5563
5664 self .tree .pack (side = tk .LEFT , fill = tk .BOTH , expand = True )
5765 tree_scroll .pack (side = tk .RIGHT , fill = tk .Y )
5866
59- # 创建右侧框架
60- right_frame = ttk .LabelFrame (paned , text = "文件列表 (双击打开文件)" )
61- paned .add (right_frame , weight = 1 )
67+ self .tree .bind ('<<TreeviewSelect>>' , self .on_encoding_select )
68+ self .tree .bind ('<Motion>' , lambda e : self .tree .configure (cursor = 'hand2' ))
69+
70+ def create_file_frame (self , parent ):
71+ right_frame = ttk .LabelFrame (parent , text = "文件列表 (双击打开文件)" )
72+ parent .add (right_frame , weight = 1 )
6273
63- # 创建右侧文件列表
6474 self .file_list = ttk .Treeview (right_frame , columns = ("文件路径" ,), show = "headings" , style = "Custom.Treeview" )
6575 self .file_list .heading ("文件路径" , text = "文件路径" )
6676 self .file_list .column ("文件路径" , width = 300 )
6777
68- # 添加滚动条
6978 file_scroll = ttk .Scrollbar (right_frame , orient = "vertical" , command = self .file_list .yview )
7079 self .file_list .configure (yscrollcommand = file_scroll .set )
7180
7281 self .file_list .pack (side = tk .LEFT , fill = tk .BOTH , expand = True )
7382 file_scroll .pack (side = tk .RIGHT , fill = tk .Y )
7483
75- # 设置树形控件的样式
76- style = ttk .Style ()
77- style .configure ("Custom.Treeview" , rowheight = 25 ) # 增加行高
78- style .configure ("Custom.Treeview.Heading" , font = ('TkDefaultFont' , 9 , 'bold' )) # 加粗表头
79-
80- # 绑定事件
81- self .tree .bind ('<<TreeviewSelect>>' , self .on_encoding_select )
8284 self .file_list .bind ('<Double-1>' , self .open_file )
83-
84- # 添加鼠标悬停效果
85- self .tree .bind ('<Motion>' , lambda e : self .tree .configure (cursor = 'hand2' ))
8685 self .file_list .bind ('<Motion>' , lambda e : self .file_list .configure (cursor = 'hand2' ))
86+
87+ def setup_styles (self ):
88+ style = ttk .Style ()
89+ style .configure ("Custom.Treeview" , rowheight = 25 )
90+ style .configure ("Custom.Treeview.Heading" , font = ('TkDefaultFont' , 9 , 'bold' ))
91+
8792 def select_directory (self ):
8893 directory = filedialog .askdirectory ()
8994 if directory :
9095 self .selected_directory = directory
9196 self .path_label .config (text = directory )
97+ # 清空之前的结果
98+ self .clear_results ()
9299
93100 def start_analyze (self ):
94101 if not self .selected_directory :
95102 tk .messagebox .showwarning ("警告" , "请先选择目录!" )
96103 return
97- self .analyze_directory (self .selected_directory )
98-
104+
105+ try :
106+ self .analyze_btn .config (state = tk .DISABLED )
107+ self .path_label .config (text = "正在分析中..." )
108+ self .update ()
109+
110+ self .analyze_directory (self .selected_directory )
111+
112+ self .path_label .config (text = self .selected_directory )
113+ tk .messagebox .showinfo ("完成" , "分析完成!" )
114+ except Exception as e :
115+ tk .messagebox .showerror ("错误" , f"分析过程出错:{ str (e )} " )
116+ finally :
117+ self .analyze_btn .config (state = tk .NORMAL )
118+
119+ def clear_results (self ):
120+ for item in self .tree .get_children ():
121+ self .tree .delete (item )
122+ for item in self .file_list .get_children ():
123+ self .file_list .delete (item )
124+ self .encoding_files .clear ()
99125 def analyze_directory (self , directory ):
100126 encoding_counter = Counter ()
101127 total_files = 0
@@ -157,4 +183,11 @@ def open_file(self, event):
157183 return
158184
159185 file_path = self .file_list .item (selected_items [0 ])['values' ][0 ]
160- os .startfile (file_path ) # Windows系统打开文件
186+ os .startfile (file_path ) # Windows系统打开文件
187+ # 将主程序入口移到类定义之外
188+ if __name__ == "__main__" :
189+ try :
190+ app = EncodingAnalyzer ()
191+ app .mainloop ()
192+ except Exception as e :
193+ print (f"程序出错: { e } " )
0 commit comments