wxPython unexpected exit

J

Jimmy

Hi, wxPython is cool and easy to use, But I ran into a problem
recently when I try to write a GUI.
The thing is I want to periodically update the content of StatixText
object, so after create them, I pack them into a list...the problem
comes when I later try to extract them from the list! I don't know
why?
my code is as following:

import wx, socket
import thread

class MyFrame(wx.Frame):

firstrun = 0
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Notifier')
self.panel = wx.Panel(self, -1)
self.length = 50
self.scale = 0.6
self.count = 5
self.size = wx.Frame.GetSize(self)
self.distance = self.size[1] / self.count
self.labellist = []
self.gaugelist = []

def ParseAndDisplay(self, data):
print "Successful access to main Frame class"
print 'And receive data: ', data
if MyFrame.firstrun == 0:
print 'First time run'
items = 3
for i in range(items):
self.labellist.append(wx.StaticText(self.panel, -1, data+str(i),
(150, 50+i*20), (300,30)))
MyFrame.firstrun = 1
else:
self.labellist[0].SetLabel('AAA')//PROGRAM WILL ABORT HERE!!!
self.labellist[1].SetLabel("Guo")
self.labellist[2].SetLabel("Qiang")


class NetUdp:

def __init__(self):
self.port = 8081
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.bind(("", self.port))
print "Listening on port", self.port

def recvdata(self):
data, addr = self.s.recvfrom(1024)
return data


def netThread():
netudp = NetUdp()
while True:
data = netudp.recvdata()
frame.ParseAndDisplay(data)

if __name__ == '__main__':
firstrun = 0
app = wx.PySimpleApp()
frame = MyFrame()
frame.Show()
# start network thread first
id = thread.start_new_thread(netThread, ())
# main wxpython loop begins
app.MainLoop()

I know the code is ugly, but can anyone really save me here!
 
T

Thin Myrna

Jimmy said:
Hi, wxPython is cool and easy to use, But I ran into a problem
recently when I try to write a GUI.
The thing is I want to periodically update the content of StatixText
object, so after create them, I pack them into a list...the problem
comes when I later try to extract them from the list! I don't know
why?
my code is as following:

import wx, socket
import thread

class MyFrame(wx.Frame):

firstrun = 0
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Notifier')
self.panel = wx.Panel(self, -1)
self.length = 50
self.scale = 0.6
self.count = 5
self.size = wx.Frame.GetSize(self)
self.distance = self.size[1] / self.count
self.labellist = []
self.gaugelist = []

def ParseAndDisplay(self, data):
print "Successful access to main Frame class"
print 'And receive data: ', data
if MyFrame.firstrun == 0:
print 'First time run'
items = 3
for i in range(items):
self.labellist.append(wx.StaticText(self.panel, -1, data+str(i),
(150, 50+i*20), (300,30)))
MyFrame.firstrun = 1
else:
self.labellist[0].SetLabel('AAA')//PROGRAM WILL ABORT HERE!!!
self.labellist[1].SetLabel("Guo")
self.labellist[2].SetLabel("Qiang")


class NetUdp:

def __init__(self):
self.port = 8081
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.bind(("", self.port))
print "Listening on port", self.port

def recvdata(self):
data, addr = self.s.recvfrom(1024)
return data


def netThread():
netudp = NetUdp()
while True:
data = netudp.recvdata()
frame.ParseAndDisplay(data)

if __name__ == '__main__':
firstrun = 0
app = wx.PySimpleApp()
frame = MyFrame()
frame.Show()
# start network thread first
id = thread.start_new_thread(netThread, ())
# main wxpython loop begins
app.MainLoop()

I know the code is ugly, but can anyone really save me here!

Communication OS thread -> wx has to be done in a certain way. You must not
do this directly, i.e. you must not call wx code from w/i an OS thread. See
the wxPython Demo for an example of what you want to do: Process and
Events -> Threads.

Cheers
Thin
 
K

kyosohma

Hi, wxPython is cool and easy to use, But I ran into a problem
recently when I try to write a GUI.
The thing is I want to periodically update the content of StatixText
object, so after create them, I pack them into a list...the problem
comes when I later try to extract them from the list! I don't know
why?
my code is as following:

import wx, socket
import thread

class MyFrame(wx.Frame):

firstrun = 0
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Notifier')
self.panel = wx.Panel(self, -1)
self.length = 50
self.scale = 0.6
self.count = 5
self.size = wx.Frame.GetSize(self)
self.distance = self.size[1] / self.count
self.labellist = []
self.gaugelist = []

def ParseAndDisplay(self, data):
print "Successful access to main Frame class"
print 'And receive data: ', data
if MyFrame.firstrun == 0:
print 'First time run'
items = 3
for i in range(items):
self.labellist.append(wx.StaticText(self.panel, -1, data+str(i),
(150, 50+i*20), (300,30)))
MyFrame.firstrun = 1
else:
self.labellist[0].SetLabel('AAA')//PROGRAM WILL ABORT HERE!!!
self.labellist[1].SetLabel("Guo")
self.labellist[2].SetLabel("Qiang")

class NetUdp:

def __init__(self):
self.port = 8081
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.bind(("", self.port))
print "Listening on port", self.port

def recvdata(self):
data, addr = self.s.recvfrom(1024)
return data

def netThread():
netudp = NetUdp()
while True:
data = netudp.recvdata()
frame.ParseAndDisplay(data)

if __name__ == '__main__':
firstrun = 0
app = wx.PySimpleApp()
frame = MyFrame()
frame.Show()
# start network thread first
id = thread.start_new_thread(netThread, ())
# main wxpython loop begins
app.MainLoop()

I know the code is ugly, but can anyone really save me here!

If you use threads that update the GUI, you need to take a look at the
following wiki page:
http://wiki.wxpython.org/LongRunningTasks

I've used the techniques therein and they *just work*. I'm not sure if
you can set values on items in a list or not. I've tried that sort of
thing and sometimes it works and sometimes it doesn't.

The wxPython group is probably the best place to ask these questions
anyway: http://www.wxpython.org/maillist.php

Mike
 
J

Jimmy

Hi, wxPython is cool and easy to use, But I ran into a problem
recently when I try to write a GUI.
The thing is I want to periodically update the content of StatixText
object, so after create them, I pack them into a list...the problem
comes when I later try to extract them from the list! I don't know
why?
my code is as following:
import wx, socket
import thread
class MyFrame(wx.Frame):
firstrun = 0
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Notifier')
self.panel = wx.Panel(self, -1)
self.length = 50
self.scale = 0.6
self.count = 5
self.size = wx.Frame.GetSize(self)
self.distance = self.size[1] / self.count
self.labellist = []
self.gaugelist = []
def ParseAndDisplay(self, data):
print "Successful access to main Frame class"
print 'And receive data: ', data
if MyFrame.firstrun == 0:
print 'First time run'
items = 3
for i in range(items):
self.labellist.append(wx.StaticText(self.panel, -1, data+str(i),
(150, 50+i*20), (300,30)))
MyFrame.firstrun = 1
else:
self.labellist[0].SetLabel('AAA')//PROGRAM WILL ABORT HERE!!!
self.labellist[1].SetLabel("Guo")
self.labellist[2].SetLabel("Qiang")
class NetUdp:
def __init__(self):
self.port = 8081
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.bind(("", self.port))
print "Listening on port", self.port
def recvdata(self):
data, addr = self.s.recvfrom(1024)
return data
def netThread():
netudp = NetUdp()
while True:
data = netudp.recvdata()
frame.ParseAndDisplay(data)
if __name__ == '__main__':
firstrun = 0
app = wx.PySimpleApp()
frame = MyFrame()
frame.Show()
# start network thread first
id = thread.start_new_thread(netThread, ())
# main wxpython loop begins
app.MainLoop()
I know the code is ugly, but can anyone really save me here!

If you use threads that update the GUI, you need to take a look at the
following wiki page:http://wiki.wxpython.org/LongRunningTasks

I've used the techniques therein and they *just work*. I'm not sure if
you can set values on items in a list or not. I've tried that sort of
thing and sometimes it works and sometimes it doesn't.

The wxPython group is probably the best place to ask these questions
anyway:http://www.wxpython.org/maillist.php

Mike

Thanks for your help! It seems work!
Another question: I create a progress bar, and on creation, it will be
displayed,
How can I invisualize it when later I no longer need it?
 
K

kyosohma

Hi,
Thanks for your help! It seems work!
Another question: I create a progress bar, and on creation, it will be
displayed,
How can I invisualize it when later I no longer need it?

I think this is also a good way to use threads. Take a look at the
wxPython demo for the ProgressDialog code. Basically, you run it in a
loop and communicate with it every so often to tell it your progress.
When you're done, you can set some sentinel type value which would
kill the dialog.

I've never used it, but that's how I understand its usage. As I
mentioned, you should consider posting these types of questions to the
wxPython group: http://www.wxpython.org/maillist.php

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top