diff --git a/python/test/1000/Account.csv b/python/test/1000/Account.csv deleted file mode 100644 index 572fe00..0000000 --- a/python/test/1000/Account.csv +++ /dev/null @@ -1,2 +0,0 @@ -Id,Password,NetName,UpDataTime -1000,1,aw,2024-04-18 23:22:44 diff --git a/python/test/1000/Contacts.csv b/python/test/1000/Contacts.csv deleted file mode 100644 index 0dce8d5..0000000 --- a/python/test/1000/Contacts.csv +++ /dev/null @@ -1,5 +0,0 @@ -Contact,Remark,State,UpDataTime -1001,HJads,True,2024-04-18 23:22:44 -1002,lsy,True,2024-04-18 23:22:44 -1003,sx,True,2024-04-18 23:22:44 -1004,不啊,True,2024-04-18 23:22:44 diff --git a/python/test/1000/History.csv b/python/test/1000/History.csv deleted file mode 100644 index 6684c6b..0000000 --- a/python/test/1000/History.csv +++ /dev/null @@ -1,6 +0,0 @@ -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 90aa81d..22cbb2d 100644 --- a/python/test/Client/Page/Chat_main.py +++ b/python/test/Client/Page/Chat_main.py @@ -1,4 +1,4 @@ -import csv +import wx.richtext as rt import os import wx import time @@ -90,6 +90,7 @@ class ChatFrame(wx.Frame, ProcessClient): ChatMain_Panel.SetSizer(ChatMain_box) + def click_chat_button(self, event): self.chat_page.Hide() self.connect_page.Hide() @@ -114,17 +115,6 @@ 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() @@ -138,23 +128,23 @@ class ChatFrame(wx.Frame, ProcessClient): match data['function']: case 'ChatPage_add_Contact_person': data = data['content'] - Contact = eval(data['Contact']) + Contact = data['Contact'] Remark = data['Remark'] Time = data['UpDataTime'] wx.CallAfter(self.chat_page.ChatPage_add_Contact_person, Contact, Remark) - case 'ChatPage_add_Contact_tab': + case 'Chat_screen_show': Contact = None data = data['content'] - send = eval(data['send']) - receive = eval(data['receive']) + send = data['Send'] + receive = data['Receive'] Type = data['Type'] - content = data['content'] - UpDataTime = data['UpDataTime'] - if send == self.userid: + content = data['Content'] + UpDataTime = data['Time'] + if send == str(self.userid): Contact = receive - elif receive == self.userid: + elif receive == str(self.userid): Contact = send - self.send_Chat_history(Contact, send, receive, Type, content, UpDataTime) + wx.CallAfter(self.chat_page.Chat_screen_show, Contact, send, receive, Type, content, UpDataTime) class ChatPage(wx.Panel): def __init__(self, parent): @@ -174,7 +164,8 @@ class ChatFrame(wx.Frame, ProcessClient): 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() + chat_page = self.ChatPage_add_Contact_tab(Id) + self.chat_page_map[Id] = chat_page self.chat_page_ids.append(Id) # 隐藏当前的聊天面板 @@ -186,18 +177,21 @@ class ChatFrame(wx.Frame, ProcessClient): chat_page.Show() # 更新当前显示的聊天面板 self.current_chat_page = chat_page + chat_page.Show() # 重新布局 self.Layout() - def ChatPage_add_Contact_tab(self): + def ChatPage_add_Contact_tab(self, Id): chat_panel = wx.Panel(self) chat_box = wx.BoxSizer(wx.VERTICAL) - chat_panel.chat_receive_panel = wx.ScrolledWindow(chat_panel, style=wx.BORDER_SUNKEN) - chat_panel.chat_receive_box = wx.BoxSizer(wx.HORIZONTAL) + chat_panel.contact_id = Id - 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) + chat_receive_box = wx.BoxSizer(wx.HORIZONTAL) + chat_panel.chat_receive_text = rt.RichTextCtrl(chat_panel, + style=wx.VSCROLL | wx.HSCROLL | wx.NO_BORDER | wx.WANTS_CHARS) + chat_receive_box.Add(chat_panel.chat_receive_text, 1, wx.EXPAND, 0) + chat_box.Add(chat_receive_box, 2, wx.EXPAND, 0) toolbar = wx.ToolBar(chat_panel) toolbar.SetToolBitmapSize((16, 16)) # Set the size of the toolbar icons @@ -215,12 +209,60 @@ class ChatFrame(wx.Frame, ProcessClient): chat_box.Add(chat_send_box, 1, wx.EXPAND, 0) send_button = wx.Button(chat_panel, label='发送') # Create a send button + send_button.Bind(wx.EVT_BUTTON, self.on_send_button_click) chat_send_box.Add(send_button, 0, wx.EXPAND | wx.LEFT, 5) # Add the button to the send box chat_panel.SetSizer(chat_box) return chat_panel + def Chat_screen_show(self, Contact, send, receive, Type, data, UpDataTime): + chat_panel = self.chat_page_map[Contact] + if Type == 'text': + # 确定对齐方式 + if Contact == receive: + alignment = wx.TEXT_ALIGNMENT_RIGHT + else: + alignment = wx.TEXT_ALIGNMENT_LEFT + # 开始设置对齐 + chat_panel.chat_receive_text.BeginAlignment(alignment) + # 按每30个字符分割消息内容并写入 + contents = [data[i:i + 30] for i in range(0, len(data), 30)] + contents.append(UpDataTime) # 添加时间戳 + for content in contents: + chat_panel.chat_receive_text.WriteText(content + '\n') # WriteText处理换行 + # 结束对齐设置 + chat_panel.chat_receive_text.EndAlignment() + # 添加额外的新行(如果需要) + chat_panel.chat_receive_text.Newline() + + # 在添加完所有消息后 + chat_panel.chat_receive_text.ShowPosition(chat_panel.chat_receive_text.GetLastPosition()) + chat_panel.Refresh() + chat_panel.Update() + self.Layout() + + def on_send_button_click(self, event): + button = event.GetEventObject() + chat_panel = button.GetParent() + send_text = chat_panel.chat_send_text.GetValue() # 正确获取输入框内容 + contact_id = str(chat_panel.contact_id) # 获取联系人ID + if send_text == '': + return + + self.Chat_screen_show(contact_id, None, contact_id, 'text', send_text, + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())) + + data = {"Type": "text", "content": send_text} + + content = {"genre": '聊天记录', "target": contact_id, "content": data} + + top_frame = self.GetTopLevelParent() + + top_frame.Process_client_send('Session_server', 'send_server', content) + chat_panel.chat_send_text.SetValue('') # 清空输入框 + event.Skip() + def on_page_changed(self, event): # 获取当前选定的页面的索引 current_page_index = self.ChatPage_Listbook.GetSelection() diff --git a/python/test/Client/Page/Login.py b/python/test/Client/Page/Login.py index 002c819..3e7891b 100644 --- a/python/test/Client/Page/Login.py +++ b/python/test/Client/Page/Login.py @@ -132,7 +132,7 @@ class LoginFrame(wx.Frame, ProcessClient): data = {"genre": genre, "target": target, "content": content} if account and password: self.Process_client_send("Session_server", "send_server", data) - time.sleep(3) + def send_register_button(self, event): if self.server_status: @@ -144,7 +144,7 @@ class LoginFrame(wx.Frame, ProcessClient): data = {"genre": genre, "target": target, "content": content} if account and password: self.Process_client_send("Session_server", "send_server", data) - time.sleep(3) + def Process_client_pick(self, data): if data['target'] in ['ALL', 'Login']: diff --git a/python/test/Client/Transmission/File_operate.py b/python/test/Client/Transmission/File_operate.py index 13e7937..311bc34 100644 --- a/python/test/Client/Transmission/File_operate.py +++ b/python/test/Client/Transmission/File_operate.py @@ -65,8 +65,8 @@ class FileOperate(ProcessClient): 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) + content = {'Send': i[0], 'Receive': i[1], 'Type': i[2], 'Content': i[3], 'Time': i[4]} + self.Process_client_send("Chat_main", "Chat_screen_show", content) def save_data(self, data): target = list(data.keys())[0] diff --git a/python/test/Client/Transmission/Session_server.py b/python/test/Client/Transmission/Session_server.py index 9a39fa4..ff833f0 100644 --- a/python/test/Client/Transmission/Session_server.py +++ b/python/test/Client/Transmission/Session_server.py @@ -57,7 +57,7 @@ class Session_server(ProcessClient): try: data = {"genre": genre, "target": target, "data": content, "datetime": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())} - data_json = json.dumps(data)+'\n' + data_json = json.dumps(data) + '\n' self.server_socket.send(data_json.encode("utf-8")) except Exception as a: print("发送错误:" + str(a)) @@ -72,11 +72,14 @@ class Session_server(ProcessClient): case 'send_server': self.send_server(data['content']['genre'], data['content']['target'], data['content']['content']) - def content_pick(self,data): + def content_pick(self, data): match data['genre']: case '注册' | '登录': self.Process_client_send("Login", "login_page_receive", data) case '数据更新': self.Process_client_send("File_operate", "save_data", data['data']) - - + case '聊天记录': + if data['data']['Type'] == 'text': + self.Process_client_send("File_operate", "save_data", {"History": data['data']}) + if data['target'] == '接收': + self.Process_client_send("Chat_main", "Chat_screen_show", data['data']) diff --git a/python/test/Server/Transmission/Database_formula.py b/python/test/Server/Transmission/Database_formula.py index 6743125..f664186 100644 --- a/python/test/Server/Transmission/Database_formula.py +++ b/python/test/Server/Transmission/Database_formula.py @@ -135,29 +135,47 @@ class database(ProcessClient): datas.append({"更新完成": Id}) - for data in datas: time.sleep(0.1) content = {"client_id": Id, "target": "客户端", "genre": "数据更新", "data": data} self.Process_client_send('Session_client', 'send_client', content) + def update_History(self, Send, Receive, Type, Content, Time): + if self.database_state: + try: + self.database_cursor.execute(f"insert into History(Send, Receive, Type, Content,Time)" + f"select {Send},{Receive},'{Type}',N'{Content}','{Time}'") + self.database_conn.commit() + except pymssql as a: + self.database_state = False + print(a) + def Process_client_pick(self, data): if data['target'] in ['ALL', 'Database_formula']: match data['function']: case 'check_account_state': - client_id = data['content']['client_id'] - account = data['content']['account'] - password = data['content']['password'] - self.check_account_state(client_id, account, password) + + self.check_account_state(data['content']['client_id'], + data['content']['account'], + data['content']['password']) case 'sign_account': - client_id = data['content']['client_id'] - account = data['content']['account'] - password = data['content']['password'] - self.sign_account(client_id, account, password) + + self.sign_account(data['content']['client_id'], + data['content']['account'], + data['content']['password']) case 'alter_state_database': - self.alter_state_database(data['content']['Id'], data['content']['sate']) + self.alter_state_database(data['content']['Id'], + data['content']['sate']) case 'detection_data': - self.detection_data(data['content']['client_id'], data['content']['date']) + self.detection_data(data['content']['client_id'], + data['content']['date']) + + case 'update_History': + self.update_History(data['content']['Send'], + data['content']['Receive'], + data['content']['Type'], + data['content']['Content'], + data['content']['Time']) diff --git a/python/test/Server/Transmission/Session_client.py b/python/test/Server/Transmission/Session_client.py index 3f52565..21e9ede 100644 --- a/python/test/Server/Transmission/Session_client.py +++ b/python/test/Server/Transmission/Session_client.py @@ -43,7 +43,7 @@ class link_client(ProcessClient): try: data = {"genre": genre, "target": target, "data": content, "datetime": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())} - data_json = json.dumps(data)+'\n' + data_json = json.dumps(data) + '\n' client_socket.send(data_json.encode("utf-8")) except: client_socket.close() @@ -61,7 +61,18 @@ class link_client(ProcessClient): case '数据更新': content = {'client_id': client_id, 'date': data['data']} self.Process_client_send("Database_formula", "detection_data", content) - + case '聊天记录': + Send = client_id + Receive = data['target'] + Time = data['datetime'] + data = data['data'] + Type = data['Type'] + Content = data['content'] + content = {'Send': Send, 'Receive': Receive, 'Time': Time, 'Type': Type, 'Content': Content} + self.Process_client_send("Database_formula", "update_History", content) + self.send_client(Send, "聊天记录", "发送", content) + if Receive in self.client_socket_dict: + self.send_client(Receive, "聊天记录", "接收", content) def recv_client(self, client_socket): state = True diff --git a/python/test/test.py b/python/test/test.py index 825514e..79b3f6a 100644 --- a/python/test/test.py +++ b/python/test/test.py @@ -1,21 +1,265 @@ +import wx.richtext as rt +import os import wx +import time +import multiprocessing -class ChatFrame(wx.Frame): - def __init__(self, *args, **kwargs): - super(ChatFrame, self).__init__(*args, **kwargs) - self.panel = wx.Panel(self) - self.sizer = wx.BoxSizer(wx.VERTICAL) +from Client.Transmission.Process_Client import ProcessClient - 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) +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 - self.panel.SetSizer(self.sizer) + current_file_path = __file__ + current_file_name = os.path.basename(current_file_path).split('.')[0] + self.Process_client_send("Server", "Name", current_file_name) -app = wx.App() -frame = ChatFrame(None) -frame.Show() -app.MainLoop() + ChatMain_Panel = wx.Panel(self, style=wx.BORDER_SUNKEN) + ChatMain_box = wx.BoxSizer(wx.HORIZONTAL) + ChatMain_Panel.SetSizer(ChatMain_box) + + Function_Panel = wx.Panel(ChatMain_Panel, style=wx.BORDER_RAISED) + Function_box = wx.BoxSizer(wx.VERTICAL) + Function_Panel.SetSizer(Function_box) + Function_Panel.SetBackgroundColour(wx.Colour(240, 240, 240)) + + image_chat = r'./Client/image/chat.png' + Chat_bitmap = wx.Bitmap(image_chat, wx.BITMAP_TYPE_PNG) + Chat_bitmap_static = wx.StaticBitmap(Function_Panel, bitmap=Chat_bitmap, style=wx.BORDER_RAISED) + Chat_bitmap_static.SetToolTip("聊天") + Function_box.Add(Chat_bitmap_static, 1, wx.EXPAND, 0) + Chat_bitmap_static.Bind(wx.EVT_LEFT_DOWN, self.click_chat_button) + + Function_box.AddSpacer(20) # 调整这个数字以改变间距大小 + + image_connect = r'./Client/image/connect.png' + connect_bitmap = wx.Bitmap(image_connect, wx.BITMAP_TYPE_PNG) + connect_bitmap_static = wx.StaticBitmap(Function_Panel, bitmap=connect_bitmap, style=wx.BORDER_RAISED) + connect_bitmap_static.SetToolTip("通讯录") + Function_box.Add(connect_bitmap_static, 1, wx.EXPAND, 0) + connect_bitmap_static.Bind(wx.EVT_LEFT_DOWN, self.click_connect_button) + + Function_box.AddSpacer(20) # 调整这个数字以改变间距大小 + + image_find = r'./Client/image/find.png' + find_bitmap = wx.Bitmap(image_find, wx.BITMAP_TYPE_PNG) + find_bitmap_static = wx.StaticBitmap(Function_Panel, bitmap=find_bitmap, style=wx.BORDER_RAISED) + find_bitmap_static.SetToolTip("查找") + Function_box.Add(find_bitmap_static, 1, wx.EXPAND, 0) + find_bitmap_static.Bind(wx.EVT_LEFT_DOWN, self.click_find_button) + + Function_box.AddSpacer(280) # 调整这个数字以改变间距大小 + + image_site = r'./Client/image/site.png' + site_bitmap = wx.Bitmap(image_site, wx.BITMAP_TYPE_PNG) + site_bitmap_static = wx.StaticBitmap(Function_Panel, bitmap=site_bitmap, style=wx.BORDER_RAISED) + site_bitmap_static.SetToolTip("设置") + Function_box.Add(site_bitmap_static, 1, wx.EXPAND, 0) + site_bitmap_static.Bind(wx.EVT_LEFT_DOWN, self.click_site_button) + + Function_box.Fit(Function_Panel) # 调整面板大小以适应图像 + + ChatMain_box.Add(Function_Panel, 0, wx.EXPAND | wx.ALL, 5) + + operate_box = wx.BoxSizer(wx.VERTICAL) + + self.chat_page = self.ChatPage(ChatMain_Panel) # 将 ChatMain_Panel 作为 ChatPage 的 parent + 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() + + self.site_page = self.SitePage(ChatMain_Panel) + operate_box.Add(self.site_page, 1, wx.EXPAND, 5) + self.site_page.Hide() + + self.find_page = self.FindPage(ChatMain_Panel) + operate_box.Add(self.find_page, 1, wx.EXPAND, 5) + self.find_page.Hide() + + ChatMain_box.Add(operate_box, 1, wx.EXPAND, 5) + + self.chat_page.Show() + + ChatMain_Panel.SetSizer(ChatMain_box) + + self.root_path = rf'.\{Id}' + self.Account_path = self.root_path + r'\Account.csv' + self.Contacts_path = self.root_path + r'\Contacts.csv' + self.History_path = self.root_path + r'\History.csv' + + def click_chat_button(self, event): + self.chat_page.Hide() + self.connect_page.Hide() + self.site_page.Hide() + self.find_page.Hide() + self.chat_page.Show() + self.Layout() + + def click_connect_button(self, event): + self.chat_page.Hide() + self.connect_page.Hide() + self.site_page.Hide() + self.find_page.Hide() + self.connect_page.Show() + self.Layout() + + def click_site_button(self, event): + self.chat_page.Hide() + self.connect_page.Hide() + self.site_page.Hide() + self.find_page.Hide() + 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] + if Type == 'text': + contents = ['My' if Contact == send else 'Ta'] + contents.append('') # 添加一个空字符串作为消息的一部分 + for i in range(0, len(data), 30): + contents.append(data[i:i + 30]) + contents.append(UpDataTime) + + + for content in contents: + chat_panel.chat_receive_text.AppendText(content+'\n') + + def click_find_button(self, event): + self.chat_page.Hide() + self.connect_page.Hide() + self.site_page.Hide() + self.find_page.Hide() + self.find_page.Show() + self.Layout() + + def Process_client_pick(self, data): + if data['target'] in ['ALL', 'Chat_main']: + match data['function']: + case 'ChatPage_add_Contact_person': + data = data['content'] + Contact = data['Contact'] + Remark = data['Remark'] + Time = data['UpDataTime'] + wx.CallAfter(self.chat_page.ChatPage_add_Contact_person, Contact, Remark) + case 'ChatPage_add_Contact_tab': + Contact = None + data = data['content'] + send = data['send'] + receive = data['receive'] + Type = data['Type'] + content = data['content'] + UpDataTime = data['UpDataTime'] + if send == str(self.userid): + Contact = receive + elif receive == str(self.userid): + Contact = send + wx.CallAfter(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_page_map = {} + self.chat_page_ids = [] + self.current_chat_page = None # 新增的属性用于跟踪当前显示的聊天面板 + + 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): + 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 + chat_page.Show() + # 重新布局 + self.Layout() + + 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_panel.chat_receive_text = rt.RichTextCtrl(chat_panel, style=wx.TE_MULTILINE | wx.TE_READONLY) + chat_receive_box.Add(chat_panel.chat_receive_text, 1, wx.EXPAND, 0) + chat_box.Add(chat_receive_box, 2, wx.EXPAND, 0) + + toolbar = wx.ToolBar(chat_panel) + toolbar.SetToolBitmapSize((16, 16)) # Set the size of the toolbar icons + toolbar.AddTool(wx.ID_ANY, "Tool", wx.Bitmap(wx.Bitmap(r'./Client/image/picture.png'))) + toolbar.AddTool(wx.ID_ANY, "Tool", wx.Bitmap(wx.Bitmap(r'./Client/image/file.png'))) + toolbar.AddTool(wx.ID_ANY, "Tool", wx.Bitmap(wx.Bitmap(r'./Client/image/speech.png'))) + toolbar.AddTool(wx.ID_ANY, "Tool", wx.Bitmap(wx.Bitmap(r'./Client/image/video.png'))) + toolbar.Realize() + toolbar.SetSize((-1, 30)) # Set the size of the toolbar + chat_box.Add(toolbar, 0, wx.EXPAND) + + chat_send_box = wx.BoxSizer(wx.HORIZONTAL) + 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 + chat_send_box.Add(send_button, 0, wx.EXPAND | wx.LEFT, 5) # Add the button to the send box + + chat_panel.SetSizer(chat_box) + + 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): + wx.Panel.__init__(self, parent) + print("联系人") + + class SitePage(wx.Panel): + def __init__(self, parent): + wx.Panel.__init__(self, parent) + print("设置") + + class FindPage(wx.Panel): + def __init__(self, parent): + wx.Panel.__init__(self, parent) + print("查找") diff --git a/python/test/test2.py b/python/test/test2.py index 64ce384..6e816b9 100644 --- a/python/test/test2.py +++ b/python/test/test2.py @@ -1,75 +1,30 @@ 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() +import wx.richtext as rt -class MainFrame(wx.Frame): - def __init__(self): - wx.Frame.__init__(self, None, title='Main Frame') - panel = ChatPanel(self) - panel.add_contact("Contact 1") # 添加一个联系人 +class MyFrame(wx.Frame): + def __init__(self, parent, id, title): + wx.Frame.__init__(self, parent, id, title, size=(400, 300)) + panel = wx.Panel(self) + vbox = wx.BoxSizer(wx.VERTICAL) + + # 创建 RichTextCtrl + self.rtc = rt.RichTextCtrl(panel, style=wx.VSCROLL | wx.HSCROLL | wx.NO_BORDER | wx.WANTS_CHARS) + self.rtc.BeginFontSize(12) + self.rtc.WriteText("Here is some text, and here is an image: ") + self.rtc.EndFontSize() + + # 插入图片 + image = wx.Bitmap('path_to_your_image.png', wx.BITMAP_TYPE_PNG) + self.rtc.WriteImage(image) + + vbox.Add(self.rtc, 1, flag=wx.EXPAND) + panel.SetSizer(vbox) + self.Show() -if __name__ == "__main__": - app = wx.App(False) - frame = MainFrame() +if __name__ == '__main__': + app = wx.App() + MyFrame(None, -1, 'Insert Image in RichTextCtrl') app.MainLoop()