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 - <frame_1> -> Widget -> Title: simple2-glade
You can check the result without having to generate and run the Python code:
Properties - <frame_1> -> 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 - <frame_1> -> Common -> Size: (tick) 300,250
Check the result:
Properties - <frame_1> -> 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 - <frame_1> -> Common -> Tooltip: (tick) This is a frame
Check the result:
Properties - <frame_1> -> 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.
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:
1 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
1 #!/usr/bin/env python
2 # -*- coding: ISO-8859-1 -*-
3 # generated by wxGlade 0.4.1 on Mon Mar 27 15:47:00 2006
4
5 import wx
6
7 class MyFrame(wx.Frame):
8 def __init__(self, *args, **kwds):
9 # begin wxGlade: MyFrame.__init__
10 kwds["style"] = wx.DEFAULT_FRAME_STYLE
11 wx.Frame.__init__(self, *args, **kwds)
12
13 self.__set_properties()
14 self.__do_layout()
15 # end wxGlade
16
17 def __set_properties(self):
18 # begin wxGlade: MyFrame.__set_properties
19 self.SetTitle("simple2-glade")
20 self.SetSize((300, 250))
21 self.SetToolTipString("This is a frame")
22 # end wxGlade
23 self.SetCursor(wx.StockCursor(wx.CURSOR_NO_ENTRY))
24
25 def __do_layout(self):
26 # begin wxGlade: MyFrame.__do_layout
27 sizer_1 = wx.BoxSizer(wx.VERTICAL)
28 self.SetAutoLayout(True)
29 self.SetSizer(sizer_1)
30 self.Layout()
31 # end wxGlade
32
33 # end of class MyFrame
34
35
36 class MyApp(wx.App):
37 def OnInit(self):
38 wx.InitAllImageHandlers()
39 frame_1 = MyFrame(None, -1, "")
40 self.SetTopWindow(frame_1)
41 frame_1.Show()
42 return 1
43
44 # end of class MyApp
45
46 if __name__ == "__main__":
47 app = MyApp(0)
48 app.MainLoop()