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('b itmaps/image.bmp'),
wx.BITMAP_TYPE_ BMP)
self.bmp.SetMas k(True)
wx.Frame.__init __(self, parent=None, title='monitori ng 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.ConvertToBi tmap()
wx.StaticBitmap (self.panel, -1, bmp, (x, y), (bmp.GetWidth() ,
bmp.GetHeight() ))
del bmp
app = wx.PySimpleApp( )
frame = MMCS()
frame.Show()
frame.monitor(5 0,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(ad dr)
# Receive messages
while 1:
data,addr = UDPSock.recvfro m(buf)
if not data:
print "Client has exited!"
break
else:
print "\nReceived message '", data,"'"
d = string.split(da ta, '-')
frame.monitor(s tring.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_ms g
# Send messages
while (1):
for i in range(100):
time.sleep(1)
data = "50-100-%s"%(0.1*i)
if(UDPSock.send to(data,addr)):
print "Sending message '",data,"'..... "
# Close socket
UDPSock.close()
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('b itmaps/image.bmp'),
wx.BITMAP_TYPE_ BMP)
self.bmp.SetMas k(True)
wx.Frame.__init __(self, parent=None, title='monitori ng 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.ConvertToBi tmap()
wx.StaticBitmap (self.panel, -1, bmp, (x, y), (bmp.GetWidth() ,
bmp.GetHeight() ))
del bmp
app = wx.PySimpleApp( )
frame = MMCS()
frame.Show()
frame.monitor(5 0,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(ad dr)
# Receive messages
while 1:
data,addr = UDPSock.recvfro m(buf)
if not data:
print "Client has exited!"
break
else:
print "\nReceived message '", data,"'"
d = string.split(da ta, '-')
frame.monitor(s tring.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_ms g
# Send messages
while (1):
for i in range(100):
time.sleep(1)
data = "50-100-%s"%(0.1*i)
if(UDPSock.send to(data,addr)):
print "Sending message '",data,"'..... "
# Close socket
UDPSock.close()
Comment