When the last top-level window in your application is closed by the user,the wxPython application exists.By top-level window ,it's not just the frame designated using SetTopWindow()method,it mean any frame without parents.To trigger a shutdown programtically,you can call Close() on all top-level window.
During the shutdown process,wxPython takes care of deleting all its windows and freeing their resources.Sometimes you want to perform your own cleanup ,for example close db conection,close file resources and so on,then you have one hook when application exists,you can override the OnExist() method to do you own process. Even if the application is closed by wx.Exist(),the OnExist method is still triggered.
If for somereasons you want to your application continue to run after last window closes,you can change the default behavious using the method of wx.App,SetExistOnFrameDelete(flag).If the flag is False the application will continue to run even if the last window of you application is closed.This means the wx.App instance will continue to live,and event loop will continue to process events. The application will remain untill the method wx.Exist invoke.
You can close your application in controlled way sometimes,maybe you need to end your application immediately and don't care that your program cleans up itself fully,for example a critical resource may have close.In this situation ,you can call the method of wx.App ,it's ExistMainLoop(),this method explicitily causes the main message loop to break,causing the control to leave the MainLoop().This will generally end the application.
Thursday, October 23, 2008
Monday, October 13, 2008
Redirection in wxPython
Many time we need debug our application and we want to know which step is our app run at .a good practis is add some log information and traces it.then the question is how to keep them.the answer is redirection.there are two import paramter of wx.App
- redirection:the optional value is Ture of False,if it's True,the information message will display in a Frame that you can see.if it's false,the message will display in Console.
- filename:if you set first paramter as False,then you can config it,and specify how to save your message in a file.
import wx
import sys
class MyFrame(wx.Frame):
def __init__(self,parent,id,title):
print "init frame" #entry of frame init
wx.Frame.__init__(self,parent,id,title)
class MyApp(wx.App):
def __init__(self,redirect=True,filename=None):
print "init app" #entry of app init
wx.App.__init__(self,redirect, filename)
def OnInit(self):
print "Oninit"
self.mf=MyFrame(parent=None,id=-1,title="Show it")
self.mf.Show(True)
self.SetTop1Window(self.mf)
#put the message to sys.stderr,we can also put it into sys.stdout
print >>sys.stderr, "A pretend error message"
return True
def OnExit(self):
print "OnExit"
if __name__=="__main__":
app=MyApp(redirect=False)#set redirect as False,then the message display at console
app=MyApp(redirect=True,filename="c:/aa.txt")#set redirect as True and specify the path of redirect.
print "before MainLoop"
app.MainLoop()
print "after MainLoop"
Wednesday, October 8, 2008
New wxPython package for python 2.6
A set of wxPython binaries for Python 2.6 on the Mac and Windows (32-bit and 64-bit) are now available. at http://wxpython.org/download.php
How to show the full error stack in wxPython
If you as me, a python developer,you must have this question,when I'm developing wxPython program,and I type a wrong python method or wrong method etc,then I run it,I found I can't get nothing,I expect my program will display a Frame but it didn't,I know it must because I have some wrong,but I can get any error stack.how to deal with it?
The solution is:
1)run it in command module.
2)switch you method ,app=wx.App() to
app=wx.App(False)
through this way,when there is any error in your application you will get a full error stack,it'll helpful for your developing
The solution is:
1)run it in command module.
2)switch you method ,app=wx.App() to
app=wx.App(False)
through this way,when there is any error in your application you will get a full error stack,it'll helpful for your developing
The final hello application
In this section we'll add more feature with our previous application.we'll
1)Extend our previous MyFrame class
2)Add a image as the Frame background.
following is the source code.

1)Extend our previous MyFrame class
2)Add a image as the Frame background.
following is the source code.
### Another simple App with image###we have a wxPython.jpg file,we want to add it to Frame as a background.because of we'll use the wx.StaticBitmap component on the frame to display the image,so we need convert the jpeg file to bitmap file.the way is use ConvertToBitmap() method,then we get the width and height of image file and set it as frame's size.And then we need assign a param to our custom Frame class to tell it which image will be display.
import wx
class MyFrame(wx.Frame):
###We define a new Frame with many param,especially it could contain a image ###
def __init__(self,image,parent=None,id=-1,pos=wx.DefaultPosition,
title='Hello WxPython'):
temp=image.ConvertToBitmap()
size=temp.GetWidth(),temp.GetHeight()
wx.Frame.__init__(self,parent,id,title,pos,size)
self.bmp=wx.StaticBitmap(parent=self,bitmap=temp)
class MyApp(wx.App):
def OnInit(self):
image=wx.Image('c:/wxPython.jpg',wx.BITMAP_TYPE_JPEG)
self.frame=MyFrame(image)
self.frame.Show()
self.SetTopWindow(self.frame)
return True
if __name__ == '__main__':
app=MyApp()
app.MainLoop()
Define our own frame and set it on Top of Application
In our previous example,we define a very simple application,it only have one Frame.In this section we'll extend our first example,we'll use our own Frame class and set it on the top of application.following me and I'll show it you.
here is the source code:
Then in our MyApp class we use our defined MyFrame class,because of maybe there were many window to display in a same application ,so we should tell application we want to show this frame on the top of all other the SetTopWindow() is a method of wx.App,it told programe the self.frame will be displayed on the top.
the final important thing I want to introduce is
here is the source code:
import wxThe first new is we define our new Frame class which named MyFrame,it's subclass the wx.Frame.for the simple reason we do nothing in this class,so we put a pass key word in it.
class MyFrame(wx.Frame):
pass
class MyApp(wx.App):
def OnInit(self):
self.frame=MyFrame(parent=None,title="Top window")
self.frame.Show()
self.SetTopWindow(self.frame)
return True
if __name__ == '__main__':
app=MyApp()
app.MainLoop()
Then in our MyApp class we use our defined MyFrame class,because of maybe there were many window to display in a same application ,so we should tell application we want to show this frame on the top of all other the SetTopWindow() is a method of wx.App,it told programe the self.frame will be displayed on the top.
the final important thing I want to introduce is
if __name__ == '__main__':I called it with "application entry",it used to test wether the module is being run as program or was imported by another moudle,if the moudle was imported it's __name__ attribute will be same as its file name,but if the module is being executed,rather than imported,Python overrides the __name__ with '__main__'.Then it's giving us a chance to have the module behave differently when executed directly,it's cool,isn't it?
Tuesday, October 7, 2008
Our first simple App in Wxpython
WxPython is a very popular and powerful GUI library for Python.Today we'll begin our wxPython travelors.before we start our training,please ensure,that you have setup following software.
1)Pyton :The lastest version is 2.5
2)wxPython:The lastest version is wxPython2.8-win32-unicode-2.8.9.1-py25.exe
3)A favorate text editor,or a IDE like eclipe:I like to use Eclipse with PyDev plugin,it's really helpful.
Or now let's start our first step.I'll provide a very simple example.
#code begin-------------
import wx
class MyApp(wx.App):
def OnInit(self):
frame=wx.Frame(parent=None,title="Simple Window")
frame.Show()
return True
app=MyApp()
app.MainLoop()
#code end---------------
Then run command
->>python simple.py
You will see a very simple blank frame,with a title string "hello world".The real purpose of this program is to make sure you can create a Python source file, verify that wxPython is installed properly, and allow us to introduce more complex aspects of wxPython programming one step at a time.

OK,Now let examine this simple program step-by-step"
1)The first thing we need to do is import necessary library,it's the main wxPython package,which is named wx:
import wx
Once the package is imported,then we can refer to wxPython class,functions etc.
2)Now it's time to create our new App.every wxPython program must have one object and at least one Application,so we need to subclass wx.App class.
class MyApp(wx.App):
Then we need to extend the OnInit() method of parent class,then it's will be call when applicaiton run.
def OnInit(self):
frame=wx.Frame(parent=None,title="Simple Window")
frame.Show()
return True
As you can see we define the tile of Frame with string "Simple Window"and we invoke the method frame.Show(),it let Frame show,although we create a Frame object,but if we don't invoke the method show().we'll never see it.
3)The final step is to create an instance of the wx.App subclass, and invoke its Main-Loop() method:
app = App()
app.MainLoop()
1)Pyton :The lastest version is 2.5
2)wxPython:The lastest version is wxPython2.8-win32-unicode-2.8.9.1-py25.exe
3)A favorate text editor,or a IDE like eclipe:I like to use Eclipse with PyDev plugin,it's really helpful.
Or now let's start our first step.I'll provide a very simple example.
#code begin-------------
import wx
class MyApp(wx.App):
def OnInit(self):
frame=wx.Frame(parent=None,title="Simple Window")
frame.Show()
return True
app=MyApp()
app.MainLoop()
#code end---------------
Then run command
->>python simple.py
You will see a very simple blank frame,with a title string "hello world".The real purpose of this program is to make sure you can create a Python source file, verify that wxPython is installed properly, and allow us to introduce more complex aspects of wxPython programming one step at a time.
OK,Now let examine this simple program step-by-step"
1)The first thing we need to do is import necessary library,it's the main wxPython package,which is named wx:
import wx
Once the package is imported,then we can refer to wxPython class,functions etc.
2)Now it's time to create our new App.every wxPython program must have one object and at least one Application,so we need to subclass wx.App class.
class MyApp(wx.App):
Then we need to extend the OnInit() method of parent class,then it's will be call when applicaiton run.
def OnInit(self):
frame=wx.Frame(parent=None,title="Simple Window")
frame.Show()
return True
As you can see we define the tile of Frame with string "Simple Window"and we invoke the method frame.Show(),it let Frame show,although we create a Frame object,but if we don't invoke the method show().we'll never see it.
3)The final step is to create an instance of the wx.App subclass, and invoke its Main-Loop() method:
app = App()
app.MainLoop()
Subscribe to:
Posts (Atom)
