wxTimer problem

A

abcd

I have a python script which creates a wx.App, which creates a wx.Frame
(which has a wx.Timer).

looks sorta like this:

class MyProgram:
def __init__(self):
self.app = MyApp(0)
self.app.MainLoop()

class MyApp(wx.App):
def OnInit(self):
self.myframe= MyFrame()
self.myframe.Show(True)
self.SetTopWindow(self.myframe)
return True

class MyFrame(wx.Frame):
def __init__(self):
# do some other stuff here

# setup the timer
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self.timer.Start(100)

def killTimer(self):
self.Unbind(wx.EVT_TIMER)
self.timer.Stop()
self.timer = None


.....that's the jist of it.

Anyhow, so I startup python and import my script, which loads up the
wx.App, frame and all.

foo = MyProgram()


Then I close the frame. when the frame is closed it calls killTimer.
This method gets called and no exceptions occur.

Then...I create a new instance of the app and try to load the frame
again.

foo = MyProgram()

....and I am getting this error:

"timer can only be started from the main thread"

how can I fix this??

FYI, my script is being started by a new thread each time, so
ultimately we have....

def startProgram():
foo = MyProgram()

threading.Thread(target=startProgram).start()


Thanks in advance.
 
B

Bjoern Schliessmann

abcd said:
...and I am getting this error:

"timer can only be started from the main thread"

how can I fix this??

FYI, my script is being started by a new thread each time

Fix: Start the script from the main thread only.

Regards,


Björn
 
A

abcd

Fix: Start the script from the main thread only.
Regards,


Björn


thanks for NO help.....anyways, I got rid of the Timer b/c all i was
using it for was to check the state of the "shift" key.....but I am now
binding to key up/down.

thanks anyway
 
M

Morpheus

I have a python script which creates a wx.App, which creates a wx.Frame
(which has a wx.Timer).

looks sorta like this:

class MyProgram:
def __init__(self):
self.app = MyApp(0)
self.app.MainLoop()

class MyApp(wx.App):
def OnInit(self):
self.myframe= MyFrame()
self.myframe.Show(True)
self.SetTopWindow(self.myframe)
return True

class MyFrame(wx.Frame):
def __init__(self):
# do some other stuff here

# setup the timer
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
self.timer.Start(100)

def killTimer(self):
self.Unbind(wx.EVT_TIMER)
self.timer.Stop()
self.timer = None


....that's the jist of it.

Anyhow, so I startup python and import my script, which loads up the
wx.App, frame and all.

foo = MyProgram()


Then I close the frame. when the frame is closed it calls killTimer.
This method gets called and no exceptions occur.

Then...I create a new instance of the app and try to load the frame
again.

foo = MyProgram()

...and I am getting this error:

"timer can only be started from the main thread"

how can I fix this??

FYI, my script is being started by a new thread each time, so
ultimately we have....

def startProgram():
foo = MyProgram()

threading.Thread(target=startProgram).start()


Thanks in advance.

You must not access wx from more than one thread. Read their wiki about
this and about the appropriate workaround.

Morpheus
 

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top