wxformbuilder生成的代码(wxudp.py):
# -*- coding: utf-8 -*-
###########################################################################
# Python code generated with wxFormBuilder (version Jan 23 2018)
# http://www.wxformbuilder.org/
##
# PLEASE DO *NOT* EDIT THIS FILE!
###########################################################################
import wx
import wx.xrc
###########################################################################
# Class denglu
###########################################################################
class denglu (wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition, size=wx.Size(
369, 216), style=wx.DEFAULT_DIALOG_STYLE)
self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)
bSizer1 = wx.BoxSizer(wx.VERTICAL)
bSizer2 = wx.BoxSizer(wx.HORIZONTAL)
self.m_staticText1 = wx.StaticText(
self, wx.ID_ANY, u"输入昵称:", wx.DefaultPosition, wx.DefaultSize, 0)
self.m_staticText1.Wrap(-1)
bSizer2.Add(self.m_staticText1, 0, wx.ALL |
wx.ALIGN_CENTER_VERTICAL, 5)
self.m_nichen = wx.TextCtrl(
self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
bSizer2.Add(self.m_nichen, 1, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
bSizer1.Add(bSizer2, 1, wx.EXPAND, 5)
bSizer3 = wx.BoxSizer(wx.HORIZONTAL)
self.m_staticText2 = wx.StaticText(
self, wx.ID_ANY, u"输入房间号:(0-50000)", wx.DefaultPosition, wx.DefaultSize, 0)
self.m_staticText2.Wrap(-1)
bSizer3.Add(self.m_staticText2, 0, wx.ALL |
wx.ALIGN_CENTER_VERTICAL, 5)
self.m_fangjian = wx.TextCtrl(
self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
bSizer3.Add(self.m_fangjian, 1, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
bSizer1.Add(bSizer3, 1, wx.EXPAND, 5)
bSizer5 = wx.BoxSizer(wx.HORIZONTAL)
self.m_button1 = wx.Button(
self, wx.ID_ANY, u"确定", wx.DefaultPosition, wx.DefaultSize, 0)
bSizer5.Add(self.m_button1, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
self.m_button2 = wx.Button(
self, wx.ID_ANY, u"取消", wx.DefaultPosition, wx.DefaultSize, 0)
bSizer5.Add(self.m_button2, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5)
bSizer1.Add(bSizer5, 1, wx.EXPAND, 5)
self.SetSizer(bSizer1)
self.Layout()
self.Centre(wx.BOTH)
# Connect Events
self.m_button1.Bind(wx.EVT_BUTTON, self.btn_ok)
self.m_button2.Bind(wx.EVT_BUTTON, self.btn_no)
def __del__(self):
pass
# Virtual event handlers, overide them in your derived class
def btn_ok(self, event):
event.Skip()
def btn_no(self, event):
event.Skip()
###########################################################################
# Class liaotian
###########################################################################
class liaotian (wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition, size=wx.Size(
500, 300), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)
bSizer7 = wx.BoxSizer(wx.VERTICAL)
bSizer8 = wx.BoxSizer(wx.VERTICAL)
self.m_msg = wx.TextCtrl(
self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
bSizer8.Add(self.m_msg, 1, wx.ALL | wx.EXPAND, 5)
bSizer7.Add(bSizer8, 1, wx.EXPAND, 5)
bSizer9 = wx.BoxSizer(wx.HORIZONTAL)
self.m_staticText3 = wx.StaticText(
self, wx.ID_ANY, u"消息:", wx.DefaultPosition, wx.DefaultSize, 0)
self.m_staticText3.Wrap(-1)
bSizer9.Add(self.m_staticText3, 0, wx.ALL, 5)
self.m_fasong = wx.TextCtrl(
self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
bSizer9.Add(self.m_fasong, 1, wx.ALL, 5)
self.m_button3 = wx.Button(
self, wx.ID_ANY, u"发送", wx.DefaultPosition, wx.DefaultSize, 0)
bSizer9.Add(self.m_button3, 0, wx.ALL, 5)
bSizer7.Add(bSizer9, 0, wx.EXPAND, 5)
self.SetSizer(bSizer7)
self.Layout()
self.Centre(wx.BOTH)
# Connect Events
self.m_button3.Bind(wx.EVT_BUTTON, self.btn_fasong)
def __del__(self):
pass
# Virtual event handlers, overide them in your derived class
def btn_fasong(self, event):
event.Skip()
udpgb.py的代码:
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
u'''
Created on 2018年12月23日
@author: wuluo
'''
__author__ = 'wuluo'
__version__ = '1.0.0'
__company__ = u'重庆交大'
__updated__ = '2018-12-23'
import socket
import datetime
import threading
import wx
import jpyc.jcoding as jc
from udplte import wxudpe
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
class denglu(wxudpe.denglu):
def __init__(self):
wxudpe.denglu.__init__(self, None)
self.myname = None
self.myroom = None
self.m_button1.SetDefault()
def __del__(self):
wxudpe.denglu.__del__(self)
def btn_ok(self, event):
myname = self.m_nichen.GetValue().strip() # 昵称
if not myname:
wx.MessageBox(u'昵称不能为空', u'错误信息', wx.OK | wx.ICON_ERROR)
self.m_nichen.SetBackgroundColour("red")
self.m_nichen.SetFocus()
return
else:
self.m_nichen.SetBackgroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW))
myroom = int(self.m_fangjian.GetValue())
if not myroom or myroom < 0 or myroom > 50000:
wx.MessageBox(u'房间号错误', u'错误信息', wx.OK | wx.ICON_ERROR)
self.m_fangjian.SetBackgroundColour("red")
self.m_fangjian.SetFocus()
return
else:
self.m_fangjian.SetBackgroundColour(
wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW))
self.myname = myname
self.myroom = myroom
self.Close()
event.Skip()
def btn_no(self, event):
self.Close()
event.Skip()
class liaotian(wxudpe.liaotian):
def __init__(self, nichen, fangjian):
wxudpe.liaotian.__init__(self, None)
self.myname = nichen
self.room = fangjian
self.udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.udp.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
self.udp.bind(('', 5000 + self.room))
self.udp.settimeout(1)
self.isalive = True
self.th = threading.Thread(target=self.recv1, args=())
self.th.setDaemon(True)
self.th.start()
self.m_fasong.SetFocus()
def recv1(self):
try:
while self.isalive:
data, _ = self.udp.recvfrom(8192)
self.m_msg.AppendText(
u'%s\n' % jc.tounicode(datetime.datetime.now()))
self.m_msg.AppendText(u'%s\n\n' % jc.tounicode(data))
except:
pass
finally:
self.udp.close()
def __del__(self):
wxudpe.liaotian.__del__(self)
def send(self):
msg = self.m_fasong.GetValue().strip()
if not msg:
return
self.m_fasong.SetValue(u'')
msg = jc.toutf8(self.myname) + jc.toutf8(msg)
self.m_msg.AppendText(u'%s\n' % jc.tounicode(datetime.datetime.now()))
self.m_msg.AppendText(u'%s\n\n' % jc.tounicode(msg))
# ('255.255.255.255', 5000 + self.room)
self.udp.sendto(msg, '255.255.255.255', 5000 + self.room)
def btn_fasong(self, event):
self.send()
event.Skip()
class MyApp(wx.App):
def OnInit(self):
room = denglu()
room.ShowModal()
myname = room.myname
myroom = room.myroom
if myname is None:
room.Destroy()
self.ExitMainLoop()
return True
self.chatmain = liaotian(myname, myroom)
self.chatmain.Show()
return True
def OnExit(self):
self.chatmain.Destroy()
if __name__ == "__main__":
app = MyApp()
app.MainLoop()
有能力的,可以去解决!