无法通过函数创建窗口

This commit is contained in:
lsy2246 2024-04-19 00:48:24 +08:00
parent a8206726bf
commit 3a72f45677
11 changed files with 154 additions and 118 deletions

View File

@ -0,0 +1,2 @@
Id,Password,NetName,UpDataTime
1000,1,aw,2024-04-18 23:22:44
1 Id Password NetName UpDataTime
2 1000 1 aw 2024-04-18 23:22:44

View File

@ -0,0 +1,5 @@
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
1 Contact Remark State UpDataTime
2 1001 HJads True 2024-04-18 23:22:44
3 1002 lsy True 2024-04-18 23:22:44
4 1003 sx True 2024-04-18 23:22:44
5 1004 不啊 True 2024-04-18 23:22:44

View File

@ -0,0 +1,3 @@
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
1 Send Receive Type Content Time
2 1000 1001 text 你好1 2024-04-18 23:22:44
3 1000 1001 text 你好3 2024-04-18 23:22:44

View File

@ -1,3 +1,4 @@
import os
import wx import wx
import wx.aui import wx.aui
import wx.lib.scrolledpanel as scrolled import wx.lib.scrolledpanel as scrolled
@ -11,6 +12,10 @@ class ChatFrame(wx.Frame, ProcessClient):
wx.Frame.__init__(self, None, size=(800, 600), title="账号: " + str(Id)) wx.Frame.__init__(self, None, size=(800, 600), title="账号: " + str(Id))
ProcessClient.__init__(self) ProcessClient.__init__(self)
current_file_path = __file__
current_file_name = os.path.basename(current_file_path).split('.')[0]
self.Process_client_send("Server", "Name", current_file_name)
ChatMain_Panel = wx.Panel(self, style=wx.BORDER_SUNKEN) ChatMain_Panel = wx.Panel(self, style=wx.BORDER_SUNKEN)
ChatMain_box = wx.BoxSizer(wx.HORIZONTAL) ChatMain_box = wx.BoxSizer(wx.HORIZONTAL)
ChatMain_Panel.SetSizer(ChatMain_box) ChatMain_Panel.SetSizer(ChatMain_box)
@ -82,6 +87,8 @@ class ChatFrame(wx.Frame, ProcessClient):
ChatMain_Panel.SetSizer(ChatMain_box) ChatMain_Panel.SetSizer(ChatMain_box)
self.chat_page.ChatPage_add_Contact_person(999, 'Remark', 'Time')
def click_chat_button(self, event): def click_chat_button(self, event):
self.chat_page.Hide() self.chat_page.Hide()
self.connect_page.Hide() self.connect_page.Hide()
@ -114,6 +121,20 @@ class ChatFrame(wx.Frame, ProcessClient):
self.find_page.Show() self.find_page.Show()
self.Layout() 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 = eval(data['Contact'])
Remark = data['Remark']
Time = data['UpDataTime']
self.chat_page.ChatPage_add_Contact_person(Contact, Remark, Time)
self.Layout()
case 'ChatPage_add_Contact_tab':
# self.ChatPage.ChatPage_add_Contact_tab('Id', 'Remark','Remark')
print(data)
class ChatPage(wx.Panel): class ChatPage(wx.Panel):
def __init__(self, parent): def __init__(self, parent):
wx.Panel.__init__(self, parent, style=wx.BORDER_SUNKEN) wx.Panel.__init__(self, parent, style=wx.BORDER_SUNKEN)
@ -133,28 +154,18 @@ class ChatFrame(wx.Frame, ProcessClient):
self.SetSizer(ChatPage_main_box) self.SetSizer(ChatPage_main_box)
def ChatPage_add_Contact_person(self, Id, Remark, info): def ChatPage_add_Contact_person(self, Id, Remark, Time):
ChatPage_add_Contact_person_box = wx.BoxSizer(wx.VERTICAL) ChatPage_add_Contact_person_box = wx.BoxSizer(wx.VERTICAL)
ChatPage_add_Contact_person_top_box = wx.BoxSizer(wx.HORIZONTAL)
if len(Remark) > 6: if len(Remark) > 6:
Remark = Remark[:6] + "..." Remark = Remark[:6] + "..."
else: else:
Remark += ''.join([" " for i in range(max(0, 9 - len(Remark)))]) 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_NetName = wx.StaticText(self.ChatPage_Contact_person_panel, -1, Remark)
ChatPage_add_Contact_person_top_box.Add(ChatPage_add_Contact_person_NetName, 0, wx.ALIGN_LEFT, 0) ChatPage_add_Contact_person_box.Add(ChatPage_add_Contact_person_NetName, 0, wx.ALIGN_LEFT, 0)
ChatPage_add_Contact_person_top_box.AddSpacer(20) 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)
ChatPage_add_Contact_person_date = wx.StaticText(self.ChatPage_Contact_person_panel, -1,
time.strftime("%Y/%m/%d", time.localtime()))
ChatPage_add_Contact_person_top_box.Add(ChatPage_add_Contact_person_date, 0, wx.ALIGN_CENTER_VERTICAL, 0)
ChatPage_add_Contact_person_box.Add(ChatPage_add_Contact_person_top_box, 0, wx.EXPAND, 0)
if len(info) > 10:
info = info[:10] + "..."
ChatPage_add_Contact_person_info = wx.StaticText(self.ChatPage_Contact_person_panel, -1, info)
ChatPage_add_Contact_person_box.Add(ChatPage_add_Contact_person_info, 0, wx.ALIGN_LEFT, 0)
self.ChatPage_Contact_person_panel.GetSizer().Add(ChatPage_add_Contact_person_box, 0, wx.ALL, 5) 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.Layout()

View File

@ -109,8 +109,9 @@ class LoginFrame(wx.Frame, ProcessClient):
if receive_content["genre"] == '登录': if receive_content["genre"] == '登录':
match receive_content["data"]['status']: match receive_content["data"]['status']:
case 0: case 0:
self.Destroy() self.Process_client_send("file_operate", "detection_data", receive_content["data"]['account'])
multiprocessing.Process(target=open_chat_window, args=(receive_content["data"]['account'],)).start() multiprocessing.Process(target=open_chat_window, args=(receive_content["data"]['account'],)).start()
self.Destroy()
case -1: case -1:
wx.MessageBox('重复登录', '登录', wx.OK | wx.ICON_INFORMATION) wx.MessageBox('重复登录', '登录', wx.OK | wx.ICON_INFORMATION)
case 1: case 1:

View File

@ -48,7 +48,7 @@ class Session_server(ProcessClient):
receive_content = json.loads(receive_content_json) receive_content = json.loads(receive_content_json)
self.content_pick(receive_content) self.content_pick(receive_content)
except Exception as a: except Exception as a:
print("接收错误:" + str(a)) print("客户端接收错误:" + str(a))
self.server_status = False self.server_status = False
self.Process_client_send("ALL", "server_status", self.server_status) self.Process_client_send("ALL", "server_status", self.server_status)
@ -57,7 +57,7 @@ class Session_server(ProcessClient):
try: try:
data = {"genre": genre, "target": target, "data": content, data = {"genre": genre, "target": target, "data": content,
"datetime": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())} "datetime": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}
data_json = json.dumps(data) data_json = json.dumps(data)+'\n'
self.server_socket.send(data_json.encode("utf-8")) self.server_socket.send(data_json.encode("utf-8"))
except Exception as a: except Exception as a:
print("发送错误:" + str(a)) print("发送错误:" + str(a))
@ -77,7 +77,6 @@ class Session_server(ProcessClient):
case '注册' | '登录': case '注册' | '登录':
self.Process_client_send("Login", "login_page_receive", data) self.Process_client_send("Login", "login_page_receive", data)
case '数据更新': case '数据更新':
print(data) self.Process_client_send("file_operate", "save_data", data['data'])
self.Process_client_send("file_operate", "login_page_receive", data)

View File

@ -1,42 +1,93 @@
import os import os
import csv import csv
import time
from .Process_Client import ProcessClient from .Process_Client import ProcessClient
class FileOperate(ProcessClient): class FileOperate(ProcessClient):
def __init__(self): def __init__(self, user_id):
ProcessClient.__init__(self) ProcessClient.__init__(self)
self.user_id = user_id
self.root_path = None
self.Account_path = None
self.Contacts_path = None
self.History_path = None
self.file_root_path = None
self.other_file_path = None
self.image_file_path = None
current_file_path = __file__ current_file_path = __file__
current_file_name = os.path.basename(current_file_path).split('.')[0] current_file_name = os.path.basename(current_file_path).split('.')[0]
self.Process_client_send("Server", "Name", current_file_name) self.Process_client_send("Server", "Name", current_file_name)
def detection_data(self, Id): def detection_data(self, Id):
root_path = rf'.\{Id}' self.user_id = Id
info_path = root_path + r'\info.csv' self.root_path = rf'.\{Id}'
file_root_path = root_path + r'\file' self.Account_path = self.root_path + r'\Account.csv'
other_file_path = file_root_path + r'\other' self.Contacts_path = self.root_path + r'\Contacts.csv'
image_file_path = file_root_path + r'\image' self.History_path = self.root_path + r'\History.csv'
if not os.path.isdir(root_path): self.file_root_path = self.root_path + r'\file'
os.mkdir(root_path) self.other_file_path = self.file_root_path + r'\other'
if not os.path.isdir(file_root_path): self.image_file_path = self.file_root_path + r'\image'
os.mkdir(file_root_path) if not os.path.isdir(self.root_path):
os.mkdir(other_file_path) os.mkdir(self.root_path)
os.mkdir(image_file_path) if not os.path.isdir(self.file_root_path):
if not os.path.exists(info_path): os.mkdir(self.file_root_path)
with open(info_path, 'w', encoding='utf-8') as f: os.mkdir(self.other_file_path)
os.mkdir(self.image_file_path)
if not os.path.exists(self.Account_path):
with open(self.Account_path, 'w', encoding='utf-8') as f:
pass pass
with open(info_path, 'r+', encoding='utf-8') as info: with open(self.Account_path, 'r+', encoding='utf-8') as info:
if info.tell() == 0: data = csv.DictReader(info)
date = None if os.path.getsize(self.Account_path) == 0: # 检查文件大小是否为0
date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(0))
else:
for d in data:
date = d['UpDataTime']
target = "服务器" target = "服务器"
genre = "数据更新" genre = "数据更新"
content = date data = {"genre": genre, "target": target, "content": date}
data = {"genre": genre, "target": target, "content": content}
self.Process_client_send("Session_server", "send_server", data) 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 save_data(self, data):
target = list(data.keys())[0]
data = list(data.values())[0]
match target:
case 'Account':
with open(self.Account_path, 'w+', encoding='utf-8', newline='') as file:
csv_file = csv.writer(file)
csv_file.writerow(list(data.keys()))
csv_file.writerow(list(data.values()))
case 'Contacts':
with open(self.Contacts_path, 'a+', encoding='utf-8', newline='') as file:
csv_file = csv.writer(file)
if os.path.getsize(self.Contacts_path) == 0: # 检查文件大小是否为0
csv_file.writerow(list(data.keys()))
csv_file.writerow(list(data.values()))
case 'History':
with open(self.History_path, 'a+', encoding='utf-8', newline='') as file:
csv_file = csv.writer(file)
if os.path.getsize(self.History_path) == 0: # 检查文件大小是否为0
csv_file.writerow(list(data.keys()))
csv_file.writerow(list(data.values()))
def Process_client_pick(self, data): def Process_client_pick(self, data):
if data['target'] in ['ALL', 'file_operate']: if data['target'] in ['ALL', 'file_operate']:
print(data) match data['function']:
#match data['function']: case 'detection_data':
self.detection_data(data['content'])
case 'save_data':
self.save_data(data['content'])

View File

@ -4,10 +4,12 @@ from .Transmission import *
class start_all: class start_all:
def __init__(self): def __init__(self):
ProcessServer() ProcessServer()
Session_server() Session_server()
FileOperate(None)
app = wx.App() app = wx.App()
LoginFrame().Show() LoginFrame().Show()
app.MainLoop() app.MainLoop()

View File

@ -94,37 +94,48 @@ class database(ProcessClient):
print(a) print(a)
def detection_data(self, Id, date): def detection_data(self, Id, date):
if date is None: datas = []
datas = []
self.database_cursor.execute(f"select Id,Password,NetName,UpDataTime from Account where Id = {Id}") self.database_cursor.execute(f"select Id,Password,NetName,UpDataTime from Account where Id = {Id}")
Account_database = self.database_cursor.fetchall() Account_database = self.database_cursor.fetchall()
Account_content = {'Id': Account_database[0][0], Account_content = {'Id': Account_database[0][0],
'Password': Account_database[0][1], 'Password': Account_database[0][1],
'NetName': Account_database[0][2], 'NetName': Account_database[0][2],
'UpDataTime': Account_database[0][3].strftime('%Y-%m-%d %H:%M:%S')} 'UpDataTime': Account_database[0][3].strftime('%Y-%m-%d %H:%M:%S')}
Account_data = {"Account": Account_content} if Account_content['UpDataTime'] == date:
datas.append(Account_data) return
self.database_cursor.execute(f"select ContactsId,Remark,State from Contacts where UserId = {Id}") Account_data = {"Account": Account_content}
Contacts_database = self.database_cursor.fetchall() datas.append(Account_data)
Contacts_data = {"Contacts": Contacts_database}
self.database_cursor.execute(
f"select ContactsId,Remark,State,UpDataTime from Contacts where UserId = {Id} and UpDataTime > '{date}'")
Contacts_database = self.database_cursor.fetchall()
for Contact in Contacts_database:
Contacts_content = {'Contact': Contact[0],
'Remark': Contact[1],
'State': Contact[2],
'UpDataTime': Contact[3].strftime('%Y-%m-%d %H:%M:%S')}
Contacts_data = {"Contacts": Contacts_content}
datas.append(Contacts_data) datas.append(Contacts_data)
self.database_cursor.execute(f"select ContactsId,Remark,State from Contacts where UserId = {Id}") self.database_cursor.execute(f"select * from History where Send = {Id} or Receive = {Id} and Time > '{date}'")
Contacts_database = self.database_cursor.fetchall() History_database = self.database_cursor.fetchall()
Contacts_data = {"Contacts": Contacts_database} for History in History_database:
datas.append(Contacts_data) content = {
'Send': History[0],
self.database_cursor.execute(f"select * from History where Send = {Id} or Receive ={Id}") 'Receive': History[1],
History_database = self.database_cursor.fetchall() 'Type': History[2],
History_data = {"History": History_database} 'Content': History[3],
'Time': History[4].strftime('%Y-%m-%d %H:%M:%S')}
History_data = {"History": content}
datas.append(History_data) datas.append(History_data)
for data in datas:
content = {"client_id": Id, "target": "客户端", "genre": "数据更新", "data": data}
self.Process_client_send('Session_client', 'send_client', content)
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 Process_client_pick(self, data): def Process_client_pick(self, data):
if data['target'] in ['ALL', 'Database_formula']: if data['target'] in ['ALL', 'Database_formula']:

View File

@ -43,7 +43,7 @@ class link_client(ProcessClient):
try: try:
data = {"genre": genre, "target": target, "data": content, data = {"genre": genre, "target": target, "data": content,
"datetime": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())} "datetime": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}
data_json = json.dumps(data) data_json = json.dumps(data)+'\n'
client_socket.send(data_json.encode("utf-8")) client_socket.send(data_json.encode("utf-8"))
except: except:
client_socket.close() client_socket.close()

View File

@ -1,54 +1,5 @@
import json import time
import multiprocessing
from multiprocessing.connection import Listener
import threading
class ProcessServer: date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(0))
def __init__(self): print(type(date))
try:
self.Process_port = 3244
self.Process_server = 'localhost'
self.Process_server_listener = Listener((self.Process_server, self.Process_port))
self.Process_client_link_dick = {}
Process_client_link_Thread = threading.Thread(target=self.Process_client_link)
Process_client_link_Thread.start()
except:
print("进程通信端口绑定失败")
def Process_client_link(self):
while True:
client_connect = self.Process_server_listener.accept()
client_Thread_recv = threading.Thread(target=self.Process_client_recv, args=(client_connect,))
client_Thread_recv.start()
def Process_client_recv(self, client_Thread_recv):
while True:
data_json = client_Thread_recv.recv()
data = json.loads(data_json)
if data['target'] == 'Server':
if data['function'] == 'Name':
self.Process_client_link_dick[data['content']] = client_Thread_recv
else:
self.Process_client_pick(data)
def Process_client_send(self, target, function, content):
connect = self.Process_client_link_dick[target]
data = {"target": target, "function": function, "content": content}
data_json = json.dumps(data)
connect.send(data_json)
def Process_client_pick(self, data):
if data['target'] == 'ALL':
for value in self.Process_client_link_dick.values():
data_json = json.dumps(data)
value.send(data_json)
else:
if data['target'] in self.Process_client_link_dick.keys():
data_json = json.dumps(data)
self.Process_client_link_dick[data['target']].send(data_json)
if __name__ == '__main__':
ProcessServer()