how to make python socket server work with the app.MainLoop() in wxpython?

Philippe Martin pmartin at snakecard.com
Sun Jul 30 10:51:40 EDT 2006


zxo102 wrote:

> Hi everyone,
>     I am using a python socket server to collect data from a socket
> client and then control a image location ( wxpython) with the data,
> i.e. moving the image around in the wxpython frame.
>    But  the "app.MainLoop()" in wxpython looks like conflicting with
> the "while 1:" in socket server. After I commented the
> "app.MainLoop()", everything is working except two things:
>      1. if I click anywhere on the screen with the mouse, the image is
> gong and only the empty frame( or panel) is left.
>      2. if I don't  touch anything, the image is being moved around but
> the previous images are left behind in the panel.
>     I guess that may be caused by "app.MainLoop()" commented.
>    Anybody knows how to make the two things work together? I really
> appreciate your help.
>    My sample code is modified based on the wxpython demo: image.py.
> socket client is also attached for your reference.
> 
> Ouyang
> 
> ################ socket server with wxpython ##############
> 
> from Main import opj
> import wx,string
> class MMCS(wx.Frame):
>     def __init__(self):
>         self.bmp = wx.Image(opj('bitmaps/image.bmp'),
> wx.BITMAP_TYPE_BMP)
>         self.bmp.SetMask(True)
>         wx.Frame.__init__(self, parent=None, title='monitoring system',
> size=(500,600))
>         self.panel = wx.Panel(self,-1)
> 
>     def monitor(self,x,y,angle):
>         bmp = self.bmp.Rotate(angle, (x,y), True,None)
>         bmp = bmp.ConvertToBitmap()
> 
>         wx.StaticBitmap(self.panel, -1, bmp, (x, y), (bmp.GetWidth(),
> bmp.GetHeight()))
>         del bmp
> 
> app = wx.PySimpleApp()
> frame = MMCS()
> frame.Show()
> frame.monitor(50,10,0.0)
> #app.MainLoop()
> 
> # Server program
> from socket import *
> # Set the socket parameters
> host = "192.168.0.2"
> port = 21567
> buf = 1024
> addr = (host,port)
> 
> # Create socket and bind to address
> UDPSock = socket(AF_INET,SOCK_DGRAM)
> UDPSock.bind(addr)
> 
> # Receive messages
> while 1:
> data,addr = UDPSock.recvfrom(buf)
> if not data:
> print "Client has exited!"
> break
> else:
> print "\nReceived message '", data,"'"
>                          d = string.split(data, '-')
> 
> frame.monitor(string.atoi(d[0]),string.atoi(d[1]),string.atof(d[2]))
> if data == 'END':
> print "end of moving the ship"
> 
> # Close socket
> UDPSock.close()
> 
> ############# socket client ######################>
> rom socket import *
> import time
> 
> # Set the socket parameters
> host = "192.168.0.2"
> port = 21567
> buf = 1024
> addr = (host,port)
> 
> # Create socket
> UDPSock = socket(AF_INET,SOCK_DGRAM)
> def_msg = "===Enter message to send to server===";
> print "\n",def_msg
> 
> # Send messages
> while (1):
>           for i in range(100):
>                time.sleep(1)
> data = "50-100-%s"%(0.1*i)
> if(UDPSock.sendto(data,addr)):
> print "Sending message '",data,"'....."
> # Close socket
> UDPSock.close()


If you get rid of app.MaiLoop(), you basically get rid of all GUI events.
You need to have you server in a separate thread.

Philippe





More information about the Python-list mailing list