|| [[http://wiki.wxpython.org/index.cgi/WxGladeFirstSteps|Prev]] || [[http://wiki.wxpython.org/index.cgi/WxGladeTutorial|Up]] || [[http://wiki.wxpython.org/index.cgi/WxGladeEventsButtons|Next]] || = wxGlade tutorial - wx.window base class = (Contributed by Chris Lale, chrislale AT users DOT berlios DOT de) '''Table of Contents:''' <> == Create the application in wxglade == Run wxglade. Open the simple project simple-glade.wxg that you created in "first steps" (WxGladeFirstSteps). The project consists of one empty frame. Save it as simple2-glade.wxg. In the main (toolbox) window * `File -> Save -> /path/to/your/project/simple2-glade.wxg` The Tree shows that the application contains one frame (window)and a sizer to added automatically by wxGlade. * `Application` * `frame_1 (MyFrame)` * `sizer_1` == About the wx.Window base class == wx.Window is the base class for all windows and represents any visible object on the screen. All controls, top level windows and so on are wx.Windows. Sizers and device contexts are not however, as they don't appear on screen themselves. wx.Window's descendents inherit its methods. This means that if you are looking for a method for a particular widget, it may be documented under wx.Window rather than with the widget's own methods. There are two reference documents which describe the wxPython API. The main document is the wxWidgets manual (http://wxwidgets.org/manuals/2.6.2/wx_contents.html). It is presented using C syntax, so wx.Window appears as wxWindow, and method appears as member. A translation in Python syntax (the wxPython API) is available at http://www.wxpython.org/docs/api/wx.Window-class.html, but may be out-of-date. You may find it helpful to refer to both documents. Look for wxWindow's methods in * wxWidgets manual -> Alphabetical class reference -> wxWindow -> Members and * wxPython API -> Classes -> Window -> Methods summary == SetTitle() method == Look up wx.Window's `SetTitle()` method. You should find {{{ wxWindow::SetTitle virtual void SetTitle(const wxString& title) Sets the window's title. Applicable only to frames and dialogs. Parameters title The window's title. }}} in the wxWidgets manual and {{{ SetTitle(self, title) Sets the window's title. Applicable only to frames and dialogs. Parameters: title (type=String) }}} in the wxPython API. These tell you that `SetTitle()` sets the window's title and has one string parameter. In the wxGlade Tree window, double-click on `frame_1(MyFrame)`. This opens the Design window and the Properties window for `frame_1`. Its class `MyFrame` was derived from `wx.Frame` which in turn was derived from `wx.Window`. The `SetTitle()` method is set in the Widget tab of the Properties window. Set the title to "simple2-glade": * Properties - -> Widget -> Title: simple2-glade You can check the result without having to generate and run the Python code: * Properties - -> Common -> Preview == SetSize() method == Sets the size of the window in pixels. Parameters: * width (type=int) * height (type=int) The `SetTitle()` method is set in the Common tab of the Properties window. Set width to 300 and height to 250: * Properties - -> Common -> Size: (tick) 300,250 Check the result: * Properties - -> Common -> Preview == SetToolTipString() method == Attach a tooltip to the window. Parameters: * tip (type=String) The `SetToolTipString()` method is set in the Common tab of the Properties window. Set the tooltip text to "This is a frame". * Properties - -> Common -> Tooltip: (tick) This is a frame Check the result: * Properties - -> Common -> Preview Move the mouse pointer anywhere inside the frame to activate the tooltip. == Generate the Python code == Save the project. In the main window * `File -> Save` In the Tree window, select Application. In the Properties window, enter a new filename for the python file. * `Output path: /path/to/your/project/simple2-glade.py` Click on * `Generate code` == Run the application == Change to the directory containing simple2-glade.py. Run the file from the commandline {{{ ./simple2-glade.py }}} or {{{ python simple2-glade.py }}} Alternatively you can open the file in Idle and press F5 to run it. == Check the generated code == Open the simple2-glade.py file in Idle or a text editor. Look at `MyFrame`'s `__set_properties()` method (lines 17-22). Here are the three methods that you set in wxGlade. {{{ #!python def __set_properties(self): # begin wxGlade: MyFrame.__set_properties self.SetTitle("simple2-glade") self.SetSize((300, 250)) self.SetToolTipString("This is a frame") # end wxGlade }}} == Using methods not available in wxGlade == You can add code manually to simple2-glade.py. wxGlade will not overwrite your added code providing that you untick "Overwrite existing sources" before you regenerate the code and write your added code outside the begin wxGlade ... end wxGlade blocks. * Tree window -> Application * Properties window -> Application -> Overwrite existing sources (untick) For example, you can set the cursor to a different stock cursor. (Look up the "wx.Cursor" class and the wx.Window `SetCursor()` method.) Edit simple2-glade.py and insert after line 21 but outside the begin wxGlade ... end wxGlade block: {{{ #!python self.SetCursor(wx.StockCursor(wx.CURSOR_NO_ENTRY)) }}} Save simple2-glade.py and run it. Move the mouse pointer inside the frame to see the new cursor. = Appendix: The complete code = {{{ #!python #!/usr/bin/env python # -*- coding: ISO-8859-1 -*- # generated by wxGlade 0.4.1 on Mon Mar 27 15:47:00 2006 import wx class MyFrame(wx.Frame): def __init__(self, *args, **kwds): # begin wxGlade: MyFrame.__init__ kwds["style"] = wx.DEFAULT_FRAME_STYLE wx.Frame.__init__(self, *args, **kwds) self.__set_properties() self.__do_layout() # end wxGlade def __set_properties(self): # begin wxGlade: MyFrame.__set_properties self.SetTitle("simple2-glade") self.SetSize((300, 250)) self.SetToolTipString("This is a frame") # end wxGlade self.SetCursor(wx.StockCursor(wx.CURSOR_NO_ENTRY)) def __do_layout(self): # begin wxGlade: MyFrame.__do_layout sizer_1 = wx.BoxSizer(wx.VERTICAL) self.SetAutoLayout(True) self.SetSizer(sizer_1) self.Layout() # end wxGlade # end of class MyFrame class MyApp(wx.App): def OnInit(self): wx.InitAllImageHandlers() frame_1 = MyFrame(None, -1, "") self.SetTopWindow(frame_1) frame_1.Show() return 1 # end of class MyApp if __name__ == "__main__": app = MyApp(0) app.MainLoop() }}}