-
Notifications
You must be signed in to change notification settings - Fork 3
/
mainpage.py
373 lines (340 loc) · 15.8 KB
/
mainpage.py
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import sys
import tkinter as tk
from views import *
from PIL import Image, ImageTk
from tkinter import filedialog
import shutil
import os
import pyautogui
import time
class Mainpage:
def __init__(self, root: tk.Tk):
self.root = root
self.root.title('ChatLYT😅')
self.root.geometry('600x800+1500+500')
self.crate_page()
temp_folder = os.path.join(BASE_DIR, 'temp')
obj_folder = os.path.join(BASE_DIR,'object')
history_folder = os.path.join(BASE_DIR,'history')
if not os.path.exists(temp_folder):
os.makedirs(temp_folder)
if not os.path.exists(obj_folder):
os.makedirs(obj_folder)
if not os.path.exists(history_folder):
os.makedirs(history_folder)
def crate_page(self):
self.top_frame = tk.Frame(self.root)
def on_click(event=None):
self.top_frame.pack_forget()
self.start_frame.pack()
self.run_frame.pack_forget()
self.change_frame.pack_forget()
self.friend_frame.pack_forget()
self.friend_revise_frame.pack_forget()
self.flst_frame.pack_forget()
self.flst_revise_frame.pack_forget()
self.init_frame.pack_forget()
self.delete_frame.pack_forget()
self.check_frame.pack_forget()
img = Image.open(os.path.join(BASE_DIR,'assets/sign5.png'))
imgTk = ImageTk.PhotoImage(img)
return_label = tk.Label(self.top_frame, image=imgTk)
return_label.image = imgTk
tk.Label(self.top_frame, text='', width=5).pack(side=tk.LEFT)
return_label.pack(side=tk.LEFT, padx=15)
return_label.bind('<Button-1>', on_click)
self.title = tk.Label(self.top_frame, text="", font=('微软雅黑', 24, 'bold'), height=1, width=30, anchor="nw")
self.title.pack(side=tk.RIGHT, pady=40)
self.start_frame = tk.Frame(self.root)
self.sign_photo = tk.PhotoImage(file=os.path.join(BASE_DIR,'assets/sign.gif'))
self.sign_label = tk.Label(self.start_frame, image=self.sign_photo)
self.sign_label.image = self.sign_photo # 保持对图片的引用,防止被垃圾回收
self.sign_label.pack()
ttk.Button(self.start_frame, text="开始聊天", cursor="hand2", width=20, style='Custom.TButton', command=self.show_run).pack(pady=10)
ttk.Button(self.start_frame, text="查看好友", cursor="hand2", width=20, style='Custom.TButton', command=self.show_check).pack(pady=10)
ttk.Button(self.start_frame, text="初始化", cursor="hand2", width=20, style='Custom.TButton',
command=self.show_init).pack(pady=10)
ttk.Button(self.start_frame, text="退出", cursor="hand2", width=20, style='Custom.TButton', command=self.root.quit).pack(pady=10)
self.check_frame = tk.Frame(self.root)
tk.Label(self.check_frame, text='', height=3).pack()
friend_path = os.path.join(BASE_DIR,'object/people.json')
self.friends = []
if os.path.exists(friend_path):
with open(friend_path, 'r', encoding='utf-8') as f:
self.friends = json.load(f)
self.friend_name = [friend['name'] for friend in self.friends]
self.friend_dir = [friend['dir'] for friend in self.friends]
else:
self.friend_name = []
self.friend_dir = []
sub_check_frame = ttk.Frame(self.check_frame)
sub_check_frame.pack()
# 创建滚动条
scrollbar = ttk.Scrollbar(sub_check_frame)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# 创建Canvas
canvas = tk.Canvas(sub_check_frame, borderwidth=0, width=300, height=250)
canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# 将滚动条与Canvas关联
scrollbar.config(command=canvas.yview)
canvas.config(yscrollcommand=scrollbar.set)
# 创建一个frame放置在canvas上
sub_check_frame2 = tk.Frame(canvas)
canvas.create_window((0, 0), window=sub_check_frame2, anchor='nw')
# 为列表中的每个name创建一个多选框
avatar_path = os.path.join(BASE_DIR,'assets/sign4.png')
for i, name in enumerate(self.friend_name):
for file in os.listdir(self.friend_dir[i]):
if file.startswith('large_avatar'):
avatar_path = os.path.join(self.friend_dir[i], file)
checkbox_image = Image.open(avatar_path)
checkbox_image = checkbox_image.resize((50, 50))
checkbox_photo = ImageTk.PhotoImage(checkbox_image)
cb = ttk.Label(sub_check_frame2, text=' '+name, font=('微软雅黑', 12, 'bold'), image=checkbox_photo, compound="left")
cb.image = checkbox_photo
cb.pack(anchor='w', fill='x')
sub_check_frame2.update_idletasks()
canvas.config(scrollregion=canvas.bbox("all"))
tk.Label(self.check_frame, text='', height=5).pack()
check_button_frame = tk.Frame(self.check_frame)
check_button_frame2 = tk.Frame(self.check_frame)
check_button_frame3 = tk.Frame(self.check_frame)
ttk.Button(check_button_frame, text="新建好友资料", cursor="hand2", width=15, style='Custom.TButton',
command=self.show_friend).pack(side=tk.LEFT, padx=15)
ttk.Button(check_button_frame, text="新建好友列表", cursor="hand2", width=15, style='Custom.TButton',
command=self.show_flst).pack(side=tk.RIGHT, padx=15)
ttk.Button(check_button_frame2, text="修改好友资料", cursor="hand2", width=15, style='Custom.TButton',
command=self.show_friend_revise).pack(side=tk.LEFT,padx=15)
ttk.Button(check_button_frame2, text="修改好友列表", cursor="hand2", width=15, style='Custom.TButton',
command=self.show_flst_revise).pack(side=tk.RIGHT, padx=15)
ttk.Button(check_button_frame3, text="删除", cursor="hand2", width=15, style='Custom.TButton',
command=self.show_delete).pack(side=tk.LEFT,padx=15)
ttk.Button(check_button_frame3, text="返回", cursor="hand2", width=15, style='Custom.TButton',
command=self.show_start).pack(side=tk.RIGHT, padx=15)
check_button_frame.pack(pady=10)
check_button_frame2.pack(pady=10)
check_button_frame3.pack(pady=10)
self.about_frame = AboutFrame(self.root)
self.run_frame = RunFrame(self.root)
self.change_frame = ChangeFrame(self.root)
self.friend_frame = FriendFrame(self.root)
self.friend_revise_frame = FriendReviseFrame(self.root)
self.flst_frame = FlstFrame(self.root)
self.flst_revise_frame = FlstReviseFrame(self.root)
self.delete_frame = DeleteFrame(self.root)
self.init_frame = InitFrame(self.root)
if 'audio.png' not in os.listdir(OBJECT_DIR) or 'duplicate.png' not in os.listdir(OBJECT_DIR) or 'meme.png' not in os.listdir(OBJECT_DIR) or 'myavatar.png' not in os.listdir(OBJECT_DIR) or 'send.png' not in os.listdir(OBJECT_DIR):
self.top_frame.pack()
self.title.config(text='初始化')
self.init_frame.pack()
messagebox.showinfo("提示", "请先完成初始化!")
else:
self.start_frame.pack()
menubar = tk.Menu(self.root)
set_menu = tk.Menu(menubar, tearoff=0)
set_menu.add_command(label="帮助", command=self.guide)
set_menu.add_command(label="文件夹中打开", command=self.open_dir)
set_menu.add_command(label="更改api key", command=self.show_change)
set_menu.add_separator()
set_menu.add_command(label="退出", command=self.root.quit)
menubar.add_cascade(label="设置", menu=set_menu)
menubar.add_command(label='初始化', command=self.show_init)
friend_menu = tk.Menu(menubar, tearoff=0)
new_menu = tk.Menu(friend_menu, tearoff=0)
new_menu.add_command(label="好友资料", command=self.show_friend)
new_menu.add_command(label="好友列表", command=self.show_flst)
revise_menu = tk.Menu(friend_menu, tearoff=0)
revise_menu.add_command(label="好友资料", command=self.show_friend_revise)
revise_menu.add_command(label="好友列表", command=self.show_flst_revise)
friend_menu.add_command(label='查看好友', command=self.show_check)
friend_menu.add_separator()
friend_menu.add_cascade(label='新建', menu=new_menu)
friend_menu.add_cascade(label='修改', menu=revise_menu)
friend_menu.add_command(label='删除', command=self.show_delete)
menubar.add_cascade(label="好友", menu=friend_menu)
menubar.add_command(label='开始', command=self.show_run)
menubar.add_command(label='关于', command=self.show_about)
self.root['menu'] = menubar
def show_start(self):
self.start_frame.pack()
self.top_frame.pack_forget()
self.about_frame.pack_forget()
self.run_frame.pack_forget()
self.change_frame.pack_forget()
self.friend_frame.pack_forget()
self.friend_revise_frame.pack_forget()
self.flst_frame.pack_forget()
self.flst_revise_frame.pack_forget()
self.init_frame.pack_forget()
self.delete_frame.pack_forget()
self.check_frame.pack_forget()
def show_check(self):
self.start_frame.pack_forget()
self.top_frame.pack()
self.about_frame.pack_forget()
self.run_frame.pack_forget()
self.change_frame.pack_forget()
self.friend_frame.pack_forget()
self.friend_revise_frame.pack_forget()
self.flst_frame.pack_forget()
self.flst_revise_frame.pack_forget()
self.init_frame.pack_forget()
self.delete_frame.pack_forget()
self.title.config(text='查看好友')
self.check_frame.pack()
def show_about(self):
self.start_frame.pack_forget()
self.check_frame.pack_forget()
self.top_frame.pack_forget()
self.about_frame.pack()
self.run_frame.pack_forget()
self.change_frame.pack_forget()
self.friend_frame.pack_forget()
self.friend_revise_frame.pack_forget()
self.flst_frame.pack_forget()
self.flst_revise_frame.pack_forget()
self.init_frame.pack_forget()
self.delete_frame.pack_forget()
def show_run(self):
self.check_frame.pack_forget()
self.start_frame.pack_forget()
self.top_frame.pack()
self.about_frame.pack_forget()
self.run_frame.pack_forget()
self.change_frame.pack_forget()
self.friend_frame.pack_forget()
self.friend_revise_frame.pack_forget()
self.flst_frame.pack_forget()
self.flst_revise_frame.pack_forget()
self.init_frame.pack_forget()
self.delete_frame.pack_forget()
self.run_frame = RunFrame(self.root)
self.title.config(text='开始聊天')
self.run_frame.pack()
if not os.path.exists(os.path.join(OBJECT_DIR,'people.json')):
messagebox.showinfo("提示", "暂无好友资料,请新建")
self.title.config(text='新建好友资料')
self.run_frame.pack_forget()
self.friend_frame.pack()
def show_change(self):
self.check_frame.pack_forget()
self.start_frame.pack_forget()
self.top_frame.pack()
self.about_frame.pack_forget()
self.run_frame.pack_forget()
self.change_frame.pack_forget()
self.friend_frame.pack_forget()
self.friend_revise_frame.pack_forget()
self.flst_frame.pack_forget()
self.flst_revise_frame.pack_forget()
self.init_frame.pack_forget()
self.delete_frame.pack_forget()
self.change_frame = ChangeFrame(self.root)
self.title.config(text='更改 api key')
self.change_frame.pack()
def show_friend(self):
self.check_frame.pack_forget()
self.start_frame.pack_forget()
self.top_frame.pack()
self.about_frame.pack_forget()
self.run_frame.pack_forget()
self.change_frame.pack_forget()
self.friend_frame.pack_forget()
self.friend_revise_frame.pack_forget()
self.flst_frame.pack_forget()
self.flst_revise_frame.pack_forget()
self.init_frame.pack_forget()
self.delete_frame.pack_forget()
self.friend_frame = FriendFrame(self.root)
self.title.config(text='新建好友资料')
self.friend_frame.pack()
def show_friend_revise(self):
self.check_frame.pack_forget()
self.start_frame.pack_forget()
self.top_frame.pack()
self.about_frame.pack_forget()
self.run_frame.pack_forget()
self.change_frame.pack_forget()
self.friend_frame.pack_forget()
self.friend_revise_frame.pack_forget()
self.flst_frame.pack_forget()
self.flst_revise_frame.pack_forget()
self.init_frame.pack_forget()
self.delete_frame.pack_forget()
self.friend_revise_frame = FriendReviseFrame(self.root)
self.title.config(text='修改好友资料')
self.friend_revise_frame.pack()
def show_flst(self):
self.check_frame.pack_forget()
self.start_frame.pack_forget()
self.top_frame.pack()
self.about_frame.pack_forget()
self.run_frame.pack_forget()
self.change_frame.pack_forget()
self.friend_frame.pack_forget()
self.friend_revise_frame.pack_forget()
self.flst_frame.pack_forget()
self.flst_revise_frame.pack_forget()
self.init_frame.pack_forget()
self.delete_frame.pack_forget()
self.flst_frame = FlstFrame(self.root)
self.title.config(text='新建好友列表')
self.flst_frame.pack()
def show_flst_revise(self):
self.check_frame.pack_forget()
self.start_frame.pack_forget()
self.top_frame.pack()
self.about_frame.pack_forget()
self.run_frame.pack_forget()
self.change_frame.pack_forget()
self.friend_frame.pack_forget()
self.friend_revise_frame.pack_forget()
self.flst_frame.pack_forget()
self.flst_revise_frame.pack_forget()
self.init_frame.pack_forget()
self.delete_frame.pack_forget()
self.flst_revise_frame = FlstReviseFrame(self.root)
self.title.config(text='修改好友列表')
self.flst_revise_frame.pack()
def show_delete(self):
self.check_frame.pack_forget()
self.start_frame.pack_forget()
self.top_frame.pack()
self.about_frame.pack_forget()
self.run_frame.pack_forget()
self.change_frame.pack_forget()
self.friend_frame.pack_forget()
self.friend_revise_frame.pack_forget()
self.flst_frame.pack_forget()
self.flst_revise_frame.pack_forget()
self.init_frame.pack_forget()
self.delete_frame = DeleteFrame(self.root)
self.title.config(text='删除')
self.delete_frame.pack()
def show_init(self):
self.check_frame.pack_forget()
self.start_frame.pack_forget()
self.top_frame.pack()
self.about_frame.pack_forget()
self.run_frame.pack_forget()
self.change_frame.pack_forget()
self.friend_frame.pack_forget()
self.friend_revise_frame.pack_forget()
self.flst_frame.pack_forget()
self.flst_revise_frame.pack_forget()
self.init_frame.pack_forget()
self.delete_frame.pack_forget()
self.init_frame = InitFrame(self.root)
self.title.config(text='初始化')
self.init_frame.pack()
def guide(self):
webbrowser.open("https://github.com/Lee612-1/ChatLYT_wechatbot")
def open_dir(self):
os.startfile(OBJECT_DIR)
if __name__ == '__main__':
# pyautogui.hotkey('winleft', 'down')
# time.sleep(0.3)
root = tk.Tk()
Mainpage(root)
root.attributes("-topmost", False)
root.mainloop()