On Sep 16, 10:28 am, "usr.root" <usr.r...@gmail.com> wrote:
Hi Guys,
In my main function:
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = playerframe((200,200))
frame.Show() #how to timing to call my "frame.Update()" here
app.MainLoop()
If I want to call the "frame.Update()" function once/3seconds, can I add some codes here?
What can I do?
FYI: My playframe is the frame of "Python Class" -- player, my player have to think for 3 seconds, then update the frame.
Thanks
usr root
All you need is a wx.Timer instance and set it to run every 3000
milliseconds. Then whenever the timer event is fired, it can update
the frame.
I’m not sure I really follow. Are you saying that you have a main top level frame that launches a second frame (the player) and the player must tell the top level frame to update every so often? If so, you can still use a timer. Just put it in the player frame and connect the two with pubsub.
In other words, when the timer in the player frame fires its event, the timer event handler will send a message via pubsub to the top level frame. When the top level frame receives the message, it will update itself. Does that make sense? If not, maybe you could post a small runnable example (see: http://wiki.wxpython.org/MakingSampleApps)?
Thanks, I has known wx.Timer can be used in the frame to update the frame, but my problem is I have to update the frame in the main fuction.
Because in my project the frame can not know when to update itself, but it’s correlative class knows.
My project is as follows
in main function:
m_player = player(xx,xxx,frame,xxx)
player.frame.show()
while(worldstate != end )
player.thinking()-----------in thinking function it may call player.frame.update()
Thanks
I’m not sure I really follow. Are you saying that you have a main top level frame that launches a second frame (the player) and the player must tell the top level frame to update every so often? If so, you can still use a timer. Just put it in the player frame and connect the two with pubsub.
In other words, when the timer in the player frame fires its event, the timer event handler will send a message via pubsub to the top level frame. When the top level frame receives the message, it will update itself. Does that make sense? If not, maybe you could post a small runnable example (see: http://wiki.wxpython.org/MakingSampleApps)?
If you are not running the MainLoop then there is very little you can do with the UI and there will be *no* events delivered. You need to change how you are thinking about the application. Instead of thinking of it in a procedural way with some "main program" driving the whole thing, think about it as being "event-driven" and you'll probably have a more clear picture of how to implement what you want.
It's really not that difficult once you understand the basic model. Events can come from some action that the user does, or from the system, or be generated by other parts of your program. After set up an event driven program waits for events (this is the MainLoop() in a wxPython application,) responds as quickly as possible to each event as it arrives (your bound event handlers are called) and then returns to waiting for the next event (which may already be waiting in the queue so it is dispatched immediately.) That's really all there is to it. How you respond to the events and what GUI elements you create is what gives your application its features and functionality.
···
On 9/16/09 11:42 PM, usr.root wrote:
Hi Mike,
Thanks.
I can't write a small runnable example because of my unreasonable and
crasy idea.
In generic wxpython project, it's always write their main function as
follows,
if __name__ == '__main__':
app = myApp(False)
app.MainLoop()
Maybe I want to write somethings like this,
if __name__ == '__main__':
begin my own mainloop:
in some case:
app = myApp(False)
app.MainLoop()
elif:
update a frame of myApp
.....
end the mainloop
After read some codes in wxpython, I think there is no simple way to do
this, I plan to give up my idea since I finish my work.