(终于打好了,GUI有点伤肾)
客户端运行图
服务端运行图
客户端源码
import wx
import wx.xrc
import socket
import time
import threading
app=wx.App(False)
name=''
sk=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
while 1:
try:
print('尝试连接服务器')
sk.connect(('192.168.19.203',5252))
except:
print('连接服务器失败,2秒后尝试重新连接')
else:
print('连接服务器成功')
break
time.sleep(2)
class MyDialog ( wx.Dialog ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"聊天室", pos = wx.DefaultPosition, size = wx.Size( 550,530 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
fgSizer = wx.FlexGridSizer( 0, 2, 0, 0 )
fgSizer.SetFlexibleDirection( wx.BOTH )
fgSizer.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
bSizer = wx.BoxSizer( wx.VERTICAL )
self.m_textCtrl = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size( 400,400 ), wx.TE_MULTILINE )
self.m_textCtrl.SetForegroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_BTNTEXT ) )
self.m_textCtrl.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_INFOBK ) )
bSizer.Add( self.m_textCtrl, 0, wx.ALL, 5 )
self.m_textCtrl2 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size( 400,-1 ), wx.TE_PROCESS_ENTER )
self.m_textCtrl2.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_INFOBK ) )
bSizer.Add( self.m_textCtrl2, 0, wx.ALL, 5 )
gbSizer = wx.GridBagSizer( 0, 0 )
gbSizer.SetFlexibleDirection( wx.BOTH )
gbSizer.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED )
self.m_button = wx.Button( self, wx.ID_ANY, u"发送", wx.DefaultPosition, wx.DefaultSize, 0 )
gbSizer.Add( self.m_button, wx.GBPosition( 0, 0 ), wx.GBSpan( 1, 1 ), wx.ALL, 5 )
self.m_button2 = wx.Button( self, wx.ID_ANY, u"清空", wx.DefaultPosition, wx.DefaultSize, 0 )
gbSizer.Add( self.m_button2, wx.GBPosition( 0, 1 ), wx.GBSpan( 1, 1 ), wx.ALL, 5 )
bSizer.Add( gbSizer, 1, wx.EXPAND, 5 )
fgSizer.Add( bSizer, 1, wx.EXPAND, 5 )
bSizer2 = wx.BoxSizer( wx.VERTICAL )
self.m_staticText = wx.StaticText( self, wx.ID_ANY, u"状态栏:", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText.Wrap( -1 )
self.m_staticText.SetFont( wx.Font( 12, 70, 90, 92, False, "宋体" ) )
bSizer2.Add( self.m_staticText, 0, wx.ALL, 5 )
self.m_textCtrl3 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size( -1,400 ), wx.TE_MULTILINE )
bSizer2.Add( self.m_textCtrl3, 0, wx.ALL, 5 )
fgSizer.Add( bSizer2, 1, wx.EXPAND, 5 )
self.SetSizer( fgSizer )
self.Layout()
self.Centre( wx.BOTH )
# Connect Events
self.m_textCtrl2.Bind( wx.EVT_TEXT_ENTER, self.fs )
self.m_button.Bind( wx.EVT_BUTTON, self.fs )
self.m_button2.Bind( wx.EVT_BUTTON, self.qk )
def __del__( self ):
print('exit')
def sc( self, cw):
self.m_textCtrl3.Value=self.m_textCtrl3.Value+'\n'+cw
# Virtual event handlers, overide them in your derived class
def fs( self, event ):
global sk
print('触发发送功能')
date=self.m_textCtrl2.GetValue()
if not len(date):
self.sc('发送失败,无法发送空字符')
return
print(self.m_textCtrl2.GetValue())
self.m_textCtrl2.Value=''
try:
sk.sendall(name.encode('utf-8'))
date=' '+date
sk.sendall(date.encode('utf-8'))
except Exception as e:
self.sc('发送失败,'+str(e))
else:
self.sc('发送成功')
def qk( self, event ):
print('触发清空功能')
self.m_textCtrl.Value=''
zjm=MyDialog(None)
zjm.Show(False)
def js():
global zjm
while 1:
date=sk.recv(10240)
date=date.decode('utf-8')
zjm.m_textCtrl.Value=zjm.m_textCtrl.Value+'\n'+date
class MyDialog2 ( wx.Dialog ):
def __init__( self, parent ):
wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = u"在线聊天室", pos = wx.DefaultPosition, size = wx.Size( 200,200 ), style = wx.DEFAULT_DIALOG_STYLE )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
sbSizer1 = wx.StaticBoxSizer( wx.StaticBox( self, wx.ID_ANY, u"输入您的昵称" ), wx.VERTICAL )
self.m_textCtrl1 = wx.TextCtrl( sbSizer1.GetStaticBox(), wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.TE_PROCESS_ENTER )
self.m_textCtrl1.SetMaxLength( 5 )
sbSizer1.Add( self.m_textCtrl1, 0, wx.ALL, 5 )
self.m_button1 = wx.Button( sbSizer1.GetStaticBox(), wx.ID_ANY, u"提交", wx.DefaultPosition, wx.DefaultSize, 0 )
sbSizer1.Add( self.m_button1, 0, wx.ALL, 5 )
self.m_staticText1 = wx.StaticText( sbSizer1.GetStaticBox(), wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText1.Wrap( -1 )
sbSizer1.Add( self.m_staticText1, 0, wx.ALL, 5 )
self.SetSizer( sbSizer1 )
self.Layout()
self.Centre( wx.BOTH )
# Connect Events
self.m_textCtrl1.Bind( wx.EVT_TEXT_ENTER, self.fs )
self.m_button1.Bind( wx.EVT_BUTTON, self.fs )
def __del__( self ):
pass
# Virtual event handlers, overide them in your derived class
def fs( self, event ):
global name
name=self.m_textCtrl1.Value
if len(name):
self.Show(False)
global zjm
zjm.Show(True)
t=threading.Thread(target=js)
t.setDaemon(1)
t.start()
#建立多线程接受信息
qm=MyDialog2(None)
qm.Show(True)
app.MainLoop()
服务端源码
import socket
import threading
import re
yh=[]
def getip(dree):
ip=re.findall("'.*'",str(dree))[0]
return ip
def fs(msg):
print('发送:',msg)
for i in yh:
try:
i.send(msg.encode('utf-8'))
except Exception as e:
pass
else:
pass
ck=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ck.bind(('192.168.19.203',5252))
ck.listen(1)
def cin():
while 1:
name='未命名用户'
try:
sed,dree=ck.accept()
print('有人连接上了服务器')
yh.append(sed)
while 1:
date=sed.recv(1024).decode('utf-8')#接受数据
fs(date)
except:
pass
else:
pass
yh.remove(sed)
#fs(name+' 退出群聊')
print(name+' 退出群聊')
if __name__ == "__main__":
for i in range(10):
t=threading.Thread(target=cin)
t.setDaemon(1)
t.start()
while 1:
if input()=='exit':
break
注:如需运行,请修改ip为自己的本地ip
报错:F:/chatroom/client.py:135: wxPyDeprecationWarning: Call to deprecated item. Use SetSizeHints instead.
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
收录qwq
这已经是第4个版本了(懂我意思吧?)
了解
翻翻我的口算生成器吧,求求了(滑稽)
有 . 东西
嘿嘿嘿
哈哈 :)
sto
orz (https://www.acwing.com/blog/content/3643/)相仿您的口算生成器的想法的分享,求求您翻翻吧(orz)
hh