Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.

Commit 05c4ba7

Browse files
committed
增加异常处理
1 parent fe9122c commit 05c4ba7

File tree

1 file changed

+53
-44
lines changed

1 file changed

+53
-44
lines changed

DEcoder.py

+53-44
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3-
import tkinter as tk
3+
import tkinter as ttk
44
import tkinter.messagebox
55
from tkinter.filedialog import askopenfilenames
66
from base64 import b64encode, b64decode, b32encode, b32decode
@@ -9,19 +9,19 @@
99
from hashlib import md5
1010
from pathlib import Path
1111

12-
window = tk.Tk()
12+
window = ttk.Tk()
1313
window.title('DEcoder')
1414
window.geometry('800x650')
1515

1616
# Base64/32编码解码
1717
# 窗体名称
18-
b64Text = tk.Label(window, text='Base64/32编码解码:', font=('微软雅黑', 16))
18+
b64Text = ttk.Label(window, text='Base64/32编码解码:', font=('微软雅黑', 16))
1919
b64Text.place(x=10, y=10, anchor='nw')
2020
# 左侧输入输出框
21-
b64_input = tk.Text(window, height=8, width=40)
21+
b64_input = ttk.Text(window, height=8, width=40)
2222
b64_input.place(x=10, y=40, anchor='nw')
2323
# 右侧输入输出框
24-
b64_output = tk.Text(window, height=8, width=40)
24+
b64_output = ttk.Text(window, height=8, width=40)
2525
b64_output.place(x=500, y=40, anchor='nw')
2626

2727

@@ -68,40 +68,46 @@ def base_select_encode():
6868

6969
def base_select_decode():
7070
if var_base_num.get() == "base32":
71-
return b32_decode()
71+
try:
72+
return b32_decode()
73+
except:
74+
tkinter.messagebox.showerror(title='错误', message='解码错误,请检查输入')
7275
if var_base_num.get() == "base64":
73-
return b64_decode()
76+
try:
77+
return b64_decode()
78+
except:
79+
tkinter.messagebox.showerror(title='错误', message='解码错误,请检查输入')
7480

7581

7682
# 选择按钮
77-
var_base_num = tk.StringVar(None, 'base64') # 设置默认值为base64
78-
base_num1 = tk.Radiobutton(window, text='base32', variable=var_base_num, value='base32')
83+
var_base_num = ttk.StringVar(None, 'base64') # 设置默认值为base64
84+
base_num1 = ttk.Radiobutton(window, text='base32', variable=var_base_num, value='base32')
7985
base_num1.place(x=310, y=65, anchor='nw')
80-
base_num2 = tk.Radiobutton(window, text='base64', variable=var_base_num, value='base64')
86+
base_num2 = ttk.Radiobutton(window, text='base64', variable=var_base_num, value='base64')
8187
base_num2.place(x=410, y=65, anchor='nw')
8288

8389
# 选择按钮
84-
var_base_type = tk.StringVar(None, 'utf-8') # 设置默认值为utf-8
85-
base_type1 = tk.Radiobutton(window, text='gbk', variable=var_base_type, value='gbk')
90+
var_base_type = ttk.StringVar(None, 'utf-8') # 设置默认值为utf-8
91+
base_type1 = ttk.Radiobutton(window, text='gbk', variable=var_base_type, value='gbk')
8692
base_type1.place(x=310, y=40, anchor='nw')
87-
base_type2 = tk.Radiobutton(window, text='utf-8', variable=var_base_type, value='utf-8')
93+
base_type2 = ttk.Radiobutton(window, text='utf-8', variable=var_base_type, value='utf-8')
8894
base_type2.place(x=410, y=40, anchor='nw')
8995

9096
# 编码解码按钮
91-
b1 = tk.Button(window, text='编码 -->', width=10, height=1, command=base_select_encode)
92-
b2 = tk.Button(window, text='<-- 解码', width=10, height=1, command=base_select_decode)
97+
b1 = ttk.Button(window, text='编码 -->', width=10, height=1, command=base_select_encode)
98+
b2 = ttk.Button(window, text='<-- 解码', width=10, height=1, command=base_select_decode)
9399
b1.place(x=335, y=95, anchor='nw')
94100
b2.place(x=335, y=120, anchor='nw')
95101

96102
# URL编码解码
97103
# 窗体名称
98-
urlText = tk.Label(window, text='URL编码解码:', font=('微软雅黑', 16))
104+
urlText = ttk.Label(window, text='URL编码解码:', font=('微软雅黑', 16))
99105
urlText.place(x=10, y=160, anchor='nw')
100106
# 左侧输入输出框
101-
url_input = tk.Text(window, height=6, width=40)
107+
url_input = ttk.Text(window, height=6, width=40)
102108
url_input.place(x=10, y=190, anchor='nw')
103109
# 右侧输入输出框
104-
url_output = tk.Text(window, height=6, width=40)
110+
url_output = ttk.Text(window, height=6, width=40)
105111
url_output.place(x=500, y=190, anchor='nw')
106112

107113

@@ -121,7 +127,10 @@ def url_key_encode():
121127

122128
def url_decode():
123129
result = url_output.get("0.0", "end") # 从0行0列获取输入值直到结束
124-
result = unquote(result.strip())
130+
try:
131+
result = unquote(result.strip())
132+
except:
133+
tkinter.messagebox.showerror(title='错误', message='解码错误,请检查输入')
125134
url_input.delete("0.0", "end") # 每次输出结果前先清空文本框内的内容
126135
url_input.insert('end', result)
127136

@@ -134,27 +143,27 @@ def url_select_encode():
134143

135144

136145
# 选择按钮
137-
var_url_type = tk.StringVar(None, 'key') # 设置默认值为全编码
138-
url_type1 = tk.Radiobutton(window, text='全编码', variable=var_url_type, value='all')
146+
var_url_type = ttk.StringVar(None, 'key') # 设置默认值为全编码
147+
url_type1 = ttk.Radiobutton(window, text='全编码', variable=var_url_type, value='all')
139148
url_type1.place(x=310, y=190, anchor='nw')
140-
url_type2 = tk.Radiobutton(window, text='关键词编码', variable=var_url_type, value='key')
149+
url_type2 = ttk.Radiobutton(window, text='关键词编码', variable=var_url_type, value='key')
141150
url_type2.place(x=400, y=190, anchor='nw')
142151

143152
# 编码解码按钮
144-
c1 = tk.Button(window, text='编码 -->', width=10, height=1, command=url_select_encode)
145-
c2 = tk.Button(window, text='< --解码', width=10, height=1, command=url_decode)
153+
c1 = ttk.Button(window, text='编码 -->', width=10, height=1, command=url_select_encode)
154+
c2 = ttk.Button(window, text='< --解码', width=10, height=1, command=url_decode)
146155
c1.place(x=335, y=215, anchor='nw')
147156
c2.place(x=335, y=240, anchor='nw')
148157

149158
# 字符串与HEX互转
150159
# 窗体名称
151-
unicodeText = tk.Label(window, text='字符串与hex互转:', font=('微软雅黑', 16))
160+
unicodeText = ttk.Label(window, text='字符串与hex互转:', font=('微软雅黑', 16))
152161
unicodeText.place(x=10, y=280, anchor='nw')
153162
# 左侧输入输出框
154-
unicode_input = tk.Text(window, height=3.5, width=40)
163+
unicode_input = ttk.Text(window, height=3.5, width=40)
155164
unicode_input.place(x=10, y=310, anchor='nw')
156165
# 右侧输入输出框
157-
unicode_output = tk.Text(window, height=3.5, width=40)
166+
unicode_output = ttk.Text(window, height=3.5, width=40)
158167
unicode_output.place(x=500, y=310, anchor='nw')
159168

160169

@@ -174,20 +183,20 @@ def unicoce_decode():
174183
unicode_input.insert('end', result)
175184

176185

177-
d1 = tk.Button(window, text='str to hex -->', width=10, height=0, command=unicode_encode)
178-
d2 = tk.Button(window, text='<-- hex to str', width=10, height=0, command=unicoce_decode)
186+
d1 = ttk.Button(window, text='str to hex -->', width=10, height=0, command=unicode_encode)
187+
d2 = ttk.Button(window, text='<-- hex to str', width=10, height=0, command=unicoce_decode)
179188
d1.place(x=335, y=310, anchor='nw')
180189
d2.place(x=335, y=340, anchor='nw')
181190

182191
# ASCII与字符串互转
183192
# 窗体名称
184-
ascText = tk.Label(window, text='字符串与ASCII互转:', font=('微软雅黑', 16))
193+
ascText = ttk.Label(window, text='字符串与ASCII互转:', font=('微软雅黑', 16))
185194
ascText.place(x=10, y=380, anchor='nw')
186195
# 左侧输入输出框
187-
asc_input = tk.Text(window, height=3.5, width=40)
196+
asc_input = ttk.Text(window, height=3.5, width=40)
188197
asc_input.place(x=10, y=410, anchor='nw')
189198
# 右侧输入输出框
190-
asc_output = tk.Text(window, height=3.5, width=40)
199+
asc_output = ttk.Text(window, height=3.5, width=40)
191200
asc_output.place(x=500, y=410, anchor='nw')
192201

193202

@@ -220,18 +229,18 @@ def asc_decode(): # 目前仅支持连续的中文和英文ascii转字符串
220229
asc_input.insert('end', result_out)
221230

222231

223-
e1 = tk.Button(window, text='str to ASCII -->', width=10, height=0, command=asc_encode)
224-
e2 = tk.Button(window, text='<-- ASCII to str', width=10, height=0, command=asc_decode)
232+
e1 = ttk.Button(window, text='str to ASCII -->', width=10, height=0, command=asc_encode)
233+
e2 = ttk.Button(window, text='<-- ASCII to str', width=10, height=0, command=asc_decode)
225234
e1.place(x=335, y=410, anchor='nw')
226235
e2.place(x=335, y=440, anchor='nw')
227236

228237

229238
# md5加密
230239
# 窗体名称
231-
md5Text = tk.Label(window, text='md5 hash 计算:', font=('微软雅黑', 16))
240+
md5Text = ttk.Label(window, text='md5 hash 计算:', font=('微软雅黑', 16))
232241
md5Text.place(x=10, y=480, anchor='nw')
233242
# 左侧输入输出框
234-
md5_input = tk.Text(window, height=2, width=40)
243+
md5_input = ttk.Text(window, height=2, width=40)
235244
md5_input.place(x=10, y=510, anchor='nw')
236245

237246

@@ -248,21 +257,21 @@ def select_file():
248257

249258

250259
# 文件选择框
251-
md5_text = tk.Text(window, height=4, width=40)
260+
md5_text = ttk.Text(window, height=4, width=40)
252261
md5_text.place(x=10, y=560, anchor='nw')
253262
md5_text.insert('end', '可以选择单个文件或多个文件!')
254263
# 文件选择按钮
255-
md5_file_buttom = tk.Button(window, text='选择文件', width=5, height=0, command=select_file)
264+
md5_file_buttom = ttk.Button(window, text='选择文件', width=5, height=0, command=select_file)
256265
md5_file_buttom.place(x=300, y=600, anchor='nw')
257266
# 右侧输出框
258-
md5_output = tk.Text(window, height=6, width=40)
267+
md5_output = ttk.Text(window, height=6, width=40)
259268
md5_output.place(x=500, y=530, anchor='nw')
260269

261270
# 选择按钮
262-
var_md5_type = tk.StringVar(None, '32') # 设置默认值为全编码
263-
md5_type1 = tk.Radiobutton(window, text='md5(16)', variable=var_md5_type, value='16')
271+
var_md5_type = ttk.StringVar(None, '32') # 设置默认值为全编码
272+
md5_type1 = ttk.Radiobutton(window, text='md5(16)', variable=var_md5_type, value='16')
264273
md5_type1.place(x=310, y=510, anchor='nw')
265-
md5_type2 = tk.Radiobutton(window, text='md5(32)', variable=var_md5_type, value='32')
274+
md5_type2 = ttk.Radiobutton(window, text='md5(32)', variable=var_md5_type, value='32')
266275
md5_type2.place(x=400, y=510, anchor='nw')
267276

268277

@@ -323,9 +332,9 @@ def md5_select_file():
323332
return md5_file_32()
324333

325334

326-
f1 = tk.Button(window, text='文本hash -->', width=10, height=0, command=md5_select)
335+
f1 = ttk.Button(window, text='文本hash -->', width=10, height=0, command=md5_select)
327336
f1.place(x=335, y=540, anchor='nw')
328-
f2 = tk.Button(window, text='文件hash -->', width=10, height=0, command=md5_select_file)
337+
f2 = ttk.Button(window, text='文件hash -->', width=10, height=0, command=md5_select_file)
329338
f2.place(x=335, y=570, anchor='nw')
330339

331340
# 主窗口循环显示

0 commit comments

Comments
 (0)