diff --git a/python/test/1000/History.csv b/python/test/1000/History.csv index 95bbda5..6684c6b 100644 --- a/python/test/1000/History.csv +++ b/python/test/1000/History.csv @@ -1,3 +1,6 @@ Send,Receive,Type,Content,Time 1000,1001,text,你好1,2024-04-18 23:22:44 1000,1001,text,你好3,2024-04-18 23:22:44 +1000,1001,text,你好4,2024-04-18 23:22:44 +1001,1000,text,你好2,2024-04-18 23:22:44 +1001,1000,text,3qawzsxerctvybunmiocassadqwwqik0u97y896ty5rte45r6e4scr dcr7tv8ybonuihy8tg78rf56de56es43ehtym0,2024-04-18 23:22:44 \ No newline at end of file diff --git a/python/test/Client/Page/Chat_main.py b/python/test/Client/Page/Chat_main.py index 80c95da..90aa81d 100644 --- a/python/test/Client/Page/Chat_main.py +++ b/python/test/Client/Page/Chat_main.py @@ -1,9 +1,9 @@ +import csv import os import wx -import wx.aui -import wx.lib.scrolledpanel as scrolled import time import multiprocessing + from Client.Transmission.Process_Client import ProcessClient @@ -11,6 +11,7 @@ class ChatFrame(wx.Frame, ProcessClient): def __init__(self, Id): wx.Frame.__init__(self, None, size=(800, 600), title="账号: " + str(Id)) ProcessClient.__init__(self) + self.userid = Id current_file_path = __file__ current_file_name = os.path.basename(current_file_path).split('.')[0] @@ -69,6 +70,8 @@ class ChatFrame(wx.Frame, ProcessClient): operate_box.Add(self.chat_page, 1, wx.EXPAND, 5) self.chat_page.Hide() + self.Process_client_send("File_operate", "read_data", None) + self.connect_page = self.ConnectPage(ChatMain_Panel) operate_box.Add(self.connect_page, 1, wx.EXPAND, 5) self.connect_page.Hide() @@ -87,8 +90,6 @@ class ChatFrame(wx.Frame, ProcessClient): ChatMain_Panel.SetSizer(ChatMain_box) - self.chat_page.ChatPage_add_Contact_person(999, 'Remark', 'Time') - def click_chat_button(self, event): self.chat_page.Hide() self.connect_page.Hide() @@ -113,6 +114,17 @@ class ChatFrame(wx.Frame, ProcessClient): self.site_page.Show() self.Layout() + def send_Chat_history(self, Contact, send, receive, Type, data, UpDataTime): + chat_panel = self.chat_page.chat_page_map[Contact] + match Type: + case 'text': + if Contact == send: + contents = ['My:', data, UpDataTime] + for content in contents: + message = wx.StaticText(chat_panel.chat_receive_panel, label=content, style=wx.ALIGN_RIGHT) + chat_panel.chat_receive_box.Add(message, flag=wx.EXPAND | wx.RIGHT, border=10) + + def click_find_button(self, event): self.chat_page.Hide() self.connect_page.Hide() @@ -129,63 +141,63 @@ class ChatFrame(wx.Frame, ProcessClient): Contact = eval(data['Contact']) Remark = data['Remark'] Time = data['UpDataTime'] - self.chat_page.ChatPage_add_Contact_person(Contact, Remark, Time) - self.Layout() + wx.CallAfter(self.chat_page.ChatPage_add_Contact_person, Contact, Remark) case 'ChatPage_add_Contact_tab': - # self.ChatPage.ChatPage_add_Contact_tab('Id', 'Remark','Remark') - print(data) + Contact = None + data = data['content'] + send = eval(data['send']) + receive = eval(data['receive']) + Type = data['Type'] + content = data['content'] + UpDataTime = data['UpDataTime'] + if send == self.userid: + Contact = receive + elif receive == self.userid: + Contact = send + self.send_Chat_history(Contact, send, receive, Type, content, UpDataTime) class ChatPage(wx.Panel): def __init__(self, parent): wx.Panel.__init__(self, parent, style=wx.BORDER_SUNKEN) + ChatPage_main_box = wx.BoxSizer(wx.HORIZONTAL) - self.chat_window_id = [] + self.chat_page_map = {} + self.chat_page_ids = [] + self.current_chat_page = None # 新增的属性用于跟踪当前显示的聊天面板 - ChatPage_Contact_person_box = wx.BoxSizer(wx.VERTICAL) - self.ChatPage_Contact_person_panel = scrolled.ScrolledPanel(self, -1, style=wx.SUNKEN_BORDER) - self.ChatPage_Contact_person_panel.SetupScrolling() # 启用滚动功能 - self.ChatPage_Contact_person_panel.SetSizer(wx.BoxSizer(wx.VERTICAL)) - ChatPage_Contact_person_box.Add(self.ChatPage_Contact_person_panel, proportion=1, flag=wx.EXPAND | wx.ALL, - border=5) - ChatPage_main_box.Add(ChatPage_Contact_person_box, proportion=1, flag=wx.EXPAND | wx.ALL, border=5) - - self.chat_tab = wx.aui.AuiNotebook(self, style=wx.aui.AUI_NB_CLOSE_ON_ALL_TABS) - ChatPage_main_box.Add(self.chat_tab, proportion=3, flag=wx.EXPAND | wx.ALL, border=5) + self.ChatPage_Listbook = wx.Listbook(self, style=wx.LB_LEFT) + self.ChatPage_Listbook.Bind(wx.EVT_LISTBOOK_PAGE_CHANGED, self.on_page_changed) + ChatPage_main_box.Add(self.ChatPage_Listbook, 1, wx.EXPAND, 0) self.SetSizer(ChatPage_main_box) - def ChatPage_add_Contact_person(self, Id, Remark, Time): - ChatPage_add_Contact_person_box = wx.BoxSizer(wx.VERTICAL) + def ChatPage_add_Contact_person(self, Id, Remark): + contact_page = wx.Panel(self.ChatPage_Listbook) + self.ChatPage_Listbook.AddPage(contact_page, Remark) + chat_page = self.ChatPage_add_Contact_tab() + self.chat_page_map[Id] = chat_page + self.chat_page_ids.append(Id) + # 隐藏当前的聊天面板 + if self.current_chat_page: + self.current_chat_page.Hide() + # 将新的聊天面板添加到ChatPage_main_box中 + self.GetSizer().Add(chat_page, 5, wx.EXPAND) + # 显示新的聊天面板 + chat_page.Show() + # 更新当前显示的聊天面板 + self.current_chat_page = chat_page + # 重新布局 + self.Layout() - if len(Remark) > 6: - Remark = Remark[:6] + "..." - else: - Remark += ''.join([" " for i in range(max(0, 9 - len(Remark)))]) - ChatPage_add_Contact_person_NetName = wx.StaticText(self.ChatPage_Contact_person_panel, -1, Remark) - ChatPage_add_Contact_person_box.Add(ChatPage_add_Contact_person_NetName, 0, wx.ALIGN_LEFT, 0) - - ChatPage_add_Contact_person_date = wx.StaticText(self.ChatPage_Contact_person_panel, -1, Time) - ChatPage_add_Contact_person_box.Add(ChatPage_add_Contact_person_date, 0, wx.EXPAND, 0) - - self.ChatPage_Contact_person_panel.GetSizer().Add(ChatPage_add_Contact_person_box, 0, wx.ALL, 5) - self.ChatPage_Contact_person_panel.Layout() - self.ChatPage_Contact_person_panel.SetupScrolling() - - ChatPage_add_Contact_person_NetName.Bind(wx.EVT_LEFT_DOWN, - lambda event: self.ChatPage_add_Contact_tab(Id, Remark)) - - def ChatPage_add_Contact_tab(self, Id, Remark): - if Id in self.chat_window_id: - return - self.chat_window_id.append(Id) - - chat_panel = wx.Panel(self.chat_tab) + def ChatPage_add_Contact_tab(self): + chat_panel = wx.Panel(self) chat_box = wx.BoxSizer(wx.VERTICAL) - chat_receive_box = wx.BoxSizer(wx.HORIZONTAL) - chat_receive_text = wx.TextCtrl(chat_panel, style=wx.TE_MULTILINE | wx.TE_READONLY) - chat_receive_box.Add(chat_receive_text, 1, wx.EXPAND, 0) - chat_box.Add(chat_receive_box, 2, wx.EXPAND, 0) + chat_panel.chat_receive_panel = wx.ScrolledWindow(chat_panel, style=wx.BORDER_SUNKEN) + chat_panel.chat_receive_box = wx.BoxSizer(wx.HORIZONTAL) + + chat_panel.chat_receive_panel.SetSizer(chat_panel.chat_receive_box) + chat_box.Add(chat_panel.chat_receive_panel, 3, wx.EXPAND | wx.ALL, 0) toolbar = wx.ToolBar(chat_panel) toolbar.SetToolBitmapSize((16, 16)) # Set the size of the toolbar icons @@ -198,8 +210,8 @@ class ChatFrame(wx.Frame, ProcessClient): chat_box.Add(toolbar, 0, wx.EXPAND) chat_send_box = wx.BoxSizer(wx.HORIZONTAL) - chat_send_text = wx.TextCtrl(chat_panel, style=wx.TE_MULTILINE) - chat_send_box.Add(chat_send_text, 1, wx.EXPAND, 0) + chat_panel.chat_send_text = wx.TextCtrl(chat_panel, style=wx.TE_MULTILINE) + chat_send_box.Add(chat_panel.chat_send_text, 1, wx.EXPAND, 0) chat_box.Add(chat_send_box, 1, wx.EXPAND, 0) send_button = wx.Button(chat_panel, label='发送') # Create a send button @@ -207,8 +219,26 @@ class ChatFrame(wx.Frame, ProcessClient): chat_panel.SetSizer(chat_box) - self.chat_tab.AddPage(chat_panel, Remark, select=True) - self.GetParent().contact_windows[Id] = chat_panel + return chat_panel + + def on_page_changed(self, event): + # 获取当前选定的页面的索引 + current_page_index = self.ChatPage_Listbook.GetSelection() + # 通过索引从chat_page_ids列表中获取对应的Id + current_page_id = self.chat_page_ids[current_page_index] + # 从chat_page_map中找到对应的聊天面板 + new_chat_page = self.chat_page_map[current_page_id] + # 隐藏旧的聊天面板 + if self.current_chat_page: + self.current_chat_page.Hide() + # 显示新的聊天面板 + new_chat_page.Show() + # 更新当前显示的聊天面板 + self.current_chat_page = new_chat_page + # 重新布局 + self.Layout() + # 处理完事件后,不要忘记调用event.Skip(),以便事件可以继续传播到其他处理器 + event.Skip() class ConnectPage(wx.Panel): def __init__(self, parent): diff --git a/python/test/Client/Page/Login.py b/python/test/Client/Page/Login.py index 4bf6c3f..002c819 100644 --- a/python/test/Client/Page/Login.py +++ b/python/test/Client/Page/Login.py @@ -109,9 +109,8 @@ class LoginFrame(wx.Frame, ProcessClient): if receive_content["genre"] == '登录': match receive_content["data"]['status']: case 0: - self.Process_client_send("file_operate", "detection_data", receive_content["data"]['account']) - multiprocessing.Process(target=open_chat_window, args=(receive_content["data"]['account'],)).start() - self.Destroy() + self.Process_client_send("File_operate", "detection_data", receive_content["data"]['account']) + wx.MessageBox('登录成功正在获取数据', '登录', wx.OK | wx.ICON_INFORMATION) case -1: wx.MessageBox('重复登录', '登录', wx.OK | wx.ICON_INFORMATION) case 1: @@ -154,6 +153,10 @@ class LoginFrame(wx.Frame, ProcessClient): self.server_status = data['content'] case 'login_page_receive': self.login_page_receive(data['content']) + case '更新完成': + multiprocessing.Process(target=open_chat_window, args=(data['content'],)).start() + self.Destroy() + class LoginPanel(wx.Panel): diff --git a/python/test/Client/Transmission/file_operate.py b/python/test/Client/Transmission/File_operate.py similarity index 81% rename from python/test/Client/Transmission/file_operate.py rename to python/test/Client/Transmission/File_operate.py index 44bd234..13e7937 100644 --- a/python/test/Client/Transmission/file_operate.py +++ b/python/test/Client/Transmission/File_operate.py @@ -54,13 +54,19 @@ class FileOperate(ProcessClient): data = {"genre": genre, "target": target, "content": date} self.Process_client_send("Session_server", "send_server", data) - time.sleep(1) - with open(self.Contacts_path, 'r', encoding='utf-8') as file: - data = csv.DictReader(file) - for row in data: - self.Process_client_send("Chat_main", "ChatPage_add_Contact_person", row) - - + def read_data(self): + with open(self.Contacts_path, 'r', encoding='utf-8') as Contacts: + data = csv.reader(Contacts) + next(data) + for i in data: + content = {'Contact': i[0], 'Remark': i[1], 'state': i[2], 'UpDataTime': i[3]} + self.Process_client_send("Chat_main", "ChatPage_add_Contact_person", content) + with open(self.History_path, 'r', encoding='utf-8') as History_path: + data = csv.reader(History_path) + next(data) + for i in data: + content = {'send': i[0], 'receive': i[1], 'Type': i[2], 'content': i[3], 'UpDataTime': i[4]} + self.Process_client_send("Chat_main", "ChatPage_add_Contact_tab", content) def save_data(self, data): target = list(data.keys())[0] @@ -83,11 +89,15 @@ class FileOperate(ProcessClient): if os.path.getsize(self.History_path) == 0: # 检查文件大小是否为0 csv_file.writerow(list(data.keys())) csv_file.writerow(list(data.values())) + case '更新完成': + self.Process_client_send("ALL", "更新完成", eval(data)) def Process_client_pick(self, data): - if data['target'] in ['ALL', 'file_operate']: + if data['target'] in ['ALL', 'File_operate']: match data['function']: case 'detection_data': self.detection_data(data['content']) case 'save_data': self.save_data(data['content']) + case 'read_data': + self.read_data() diff --git a/python/test/Client/Transmission/Session_server.py b/python/test/Client/Transmission/Session_server.py index dbe8f71..9a39fa4 100644 --- a/python/test/Client/Transmission/Session_server.py +++ b/python/test/Client/Transmission/Session_server.py @@ -77,6 +77,6 @@ class Session_server(ProcessClient): case '注册' | '登录': self.Process_client_send("Login", "login_page_receive", data) case '数据更新': - self.Process_client_send("file_operate", "save_data", data['data']) + self.Process_client_send("File_operate", "save_data", data['data']) diff --git a/python/test/Client/Transmission/__init__.py b/python/test/Client/Transmission/__init__.py index a3f1b63..7f42f08 100644 --- a/python/test/Client/Transmission/__init__.py +++ b/python/test/Client/Transmission/__init__.py @@ -1,4 +1,4 @@ from .Process_Server import * from .Process_Client import * from .Session_server import * -from .file_operate import * \ No newline at end of file +from .File_operate import * \ No newline at end of file diff --git a/python/test/Server/Transmission/Database_formula.py b/python/test/Server/Transmission/Database_formula.py index 92bb34c..6743125 100644 --- a/python/test/Server/Transmission/Database_formula.py +++ b/python/test/Server/Transmission/Database_formula.py @@ -103,6 +103,8 @@ class database(ProcessClient): 'NetName': Account_database[0][2], 'UpDataTime': Account_database[0][3].strftime('%Y-%m-%d %H:%M:%S')} if Account_content['UpDataTime'] == date: + content = {"client_id": Id, "target": "客户端", "genre": "数据更新", "data": {"更新完成": Id}} + self.Process_client_send('Session_client', 'send_client', content) return Account_data = {"Account": Account_content} @@ -131,6 +133,8 @@ class database(ProcessClient): History_data = {"History": content} datas.append(History_data) + datas.append({"更新完成": Id}) + for data in datas: time.sleep(0.1) diff --git a/python/test/Server/Transmission/Session_client.py b/python/test/Server/Transmission/Session_client.py index 21054c4..3f52565 100644 --- a/python/test/Server/Transmission/Session_client.py +++ b/python/test/Server/Transmission/Session_client.py @@ -62,6 +62,7 @@ class link_client(ProcessClient): content = {'client_id': client_id, 'date': data['data']} self.Process_client_send("Database_formula", "detection_data", content) + def recv_client(self, client_socket): state = True while state: diff --git a/python/test/test.py b/python/test/test.py index 59bbad0..825514e 100644 --- a/python/test/test.py +++ b/python/test/test.py @@ -1,5 +1,21 @@ -import time +import wx +class ChatFrame(wx.Frame): + def __init__(self, *args, **kwargs): + super(ChatFrame, self).__init__(*args, **kwargs) -date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(0)) -print(type(date)) \ No newline at end of file + self.panel = wx.Panel(self) + self.sizer = wx.BoxSizer(wx.VERTICAL) + + self.contact_message = wx.StaticText(self.panel, label="Contact's message") + self.my_message = wx.StaticText(self.panel, label="My message", style=wx.ALIGN_RIGHT) + + self.sizer.Add(self.contact_message, flag=wx.EXPAND|wx.LEFT, border=10) + self.sizer.Add(self.my_message, flag=wx.EXPAND|wx.RIGHT, border=10) + + self.panel.SetSizer(self.sizer) + +app = wx.App() +frame = ChatFrame(None) +frame.Show() +app.MainLoop() diff --git a/python/test/test2.py b/python/test/test2.py index db1f26a..64ce384 100644 --- a/python/test/test2.py +++ b/python/test/test2.py @@ -1,33 +1,75 @@ -import json -import threading -from multiprocessing.connection import Client +import wx + +class ChatPanel(wx.Panel): + def __init__(self, parent): + wx.Panel.__init__(self, parent) + + sizer = wx.BoxSizer(wx.HORIZONTAL) + + self.listbook = wx.Listbook(self, style=wx.LB_LEFT) + self.chat_pages = {} + + self.listbook.Bind(wx.EVT_LISTBOOK_PAGE_CHANGED, self.on_page_changed) + + sizer.Add(self.listbook, 1, wx.EXPAND) + self.SetSizer(sizer) + + def add_contact(self, contact_name): + contact_page = wx.Panel(self.listbook) + wx.StaticText(contact_page, label=contact_name) + self.listbook.AddPage(contact_page, contact_name) + + chat_page = self.create_chat_page() + self.chat_pages[contact_name] = chat_page + + if self.listbook.GetPageCount() == 1: + self.GetSizer().Add(chat_page, 3, wx.EXPAND) + self.Layout() + + def create_chat_page(self): + chat_page = wx.Panel(self) + chat_sizer = wx.BoxSizer(wx.VERTICAL) + + chat_page.chat_text = wx.TextCtrl(chat_page, style=wx.TE_MULTILINE | wx.TE_READONLY) + chat_sizer.Add(chat_page.chat_text, 1, wx.EXPAND | wx.ALL, 5) + + chat_page.message_text = wx.TextCtrl(chat_page) + chat_sizer.Add(chat_page.message_text, 0, wx.EXPAND | wx.ALL, 5) + + send_button = wx.Button(chat_page, label="Send") + send_button.Bind(wx.EVT_BUTTON, self.on_send) + chat_sizer.Add(send_button, 0, wx.ALL, 5) + + chat_page.SetSizer(chat_sizer) + return chat_page + + def on_page_changed(self, event): + old_page = event.GetOldSelection() + new_page = event.GetSelection() + contact_name = self.listbook.GetPageText(new_page) + + self.GetSizer().Replace(self.chat_pages[self.listbook.GetPageText(old_page)], self.chat_pages[contact_name]) + self.Layout() + + def on_send(self, event): + current_page_index = self.listbook.GetSelection() + current_page_name = self.listbook.GetPageText(current_page_index) + current_chat_page = self.chat_pages[current_page_name] + + message = current_chat_page.message_text.GetValue() + current_chat_page.chat_text.AppendText(f"You: {message}\n") + current_chat_page.message_text.Clear() -class ProcessClient: +class MainFrame(wx.Frame): def __init__(self): - self.Process_port = 3244 - self.Process_server = 'localhost' - self.Process_client_Client = Client((self.Process_server, self.Process_port)) - Process_client_recv = threading.Thread(target=self.Process_client_recv) - Process_client_recv.start() + wx.Frame.__init__(self, None, title='Main Frame') + panel = ChatPanel(self) + panel.add_contact("Contact 1") # 添加一个联系人 + self.Show() - def Process_client_send(self, target, function, content): - data = {"target": target, "function": function, "content": content} - data_json = json.dumps(data) - self.Process_client_Client.send(data_json) - def Process_client_recv(self): - while True: - try: - data_json = self.Process_client_Client.recv() - data = json.loads(data_json) - self.Process_client_pick(data) - except EOFError: - print("连接已关闭") - break - - def Process_client_pick(self, data): - pass - -if __name__ == '__main__': - ProcessClient() \ No newline at end of file +if __name__ == "__main__": + app = wx.App(False) + frame = MainFrame() + app.MainLoop()