Wednesday, October 8, 2008

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:
import wx

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()
The 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.
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?

No comments: