wxGlade tutorial - Status bar
(Contributed by Chris Lale, chrislale AT users DOT berlios DOT de)
Table of Contents:
Contents
Create the application in wxglade
Run wxglade. Open the events and buttons project event-glade.wxg that you created in "Events and buttons" (WxGladeEventsButtons). The project consists of a frame containing one button. Set the title to "statusbar-glade" and save it as statusbar-glade.py. In the main (toolbox) window
Properties - <frame_1> -> Widget -> Title: statusbar-glade
File -> Save -> /path/to/your/project/statusbar-glade.wxg
The Tree shows that the application contains one frame, a sizer and a button.
Application
frame_1 (MyFrame)
sizer_1
button_1
About the wx.StatusBar class
A status bar is a narrow window that can be placed along the bottom of a frame to give small amounts of status information. It can contain one or more fields, one or more of which can be variable length according to the size of the window.
The constructor is __init__(self, parent, id, pos, size, style, name), but you only need to specify the first two: parent, and id.
Look for wx.StatusBar's methods in
wxWidgets manual -> Alphabetical class reference -> wx!StatusBar -> Members
and
wxPython API -> Classes -> StatusBar -> Methods summary
Add a status bar to the frame
Tree -> Frame_1(MyFrame)
Properties <frame_1> -> Widget -> Has StatusBar (ticked)
This creates frame_1_statusbar. In frame_1_statusbar's Properties window, accept the defaults:
Properties <frame_1_StatusBar>
- Name: frame_1_statusBar
Class: wx!StatusBar
- Text: frame_1_statusBar
- Size: -1
The status bar instance method is added to MyFrame's __init()__ method:
1 self.frame_1_statusbar = self.CreateStatusBar(1, 0)
The status bar size (width) and text are set in the __set_properties() method
Check the result:
Properties - <frame_1> -> Common -> Preview
Create an event handler to modify the status bar
Set Output path to /path/to/your/project/staturbar-glade.py, generate the code and edit statusbar-glade.py.
Modify the existing event handler for button_1. Change
to
Save statusbar-glade.py and run it.
Appendix: The complete code
1 #!/usr/bin/env python
2 # -*- coding: ISO-8859-1 -*-
3 # generated by wxGlade 0.4.1 on Thu Mar 30 13:53:04 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 self.button_1 = wx.Button(self, wx.ID_OK, "")
13 self.frame_1_statusbar = self.CreateStatusBar(1, 0)
14
15 self.__set_properties()
16 self.__do_layout()
17
18 self.Bind(wx.EVT_BUTTON, self.OnBtn1, id=wx.ID_OK)
19 # end wxGlade
20
21 def __set_properties(self):
22 # begin wxGlade: MyFrame.__set_properties
23 self.SetTitle("statusbar-glade")
24 self.SetSize((350, 250))
25 self.frame_1_statusbar.SetStatusWidths([-1])
26 # statusbar fields
27 frame_1_statusbar_fields = ["frame_1_statusbar"]
28 for i in range(len(frame_1_statusbar_fields)):
29 self.frame_1_statusbar.SetStatusText(frame_1_statusbar_fields[i], i)
30 # end wxGlade
31
32 def __do_layout(self):
33 # begin wxGlade: MyFrame.__do_layout
34 sizer_1 = wx.BoxSizer(wx.VERTICAL)
35 sizer_1.Add(self.button_1, 0, wx.ADJUST_MINSIZE, 0)
36 self.SetAutoLayout(True)
37 self.SetSizer(sizer_1)
38 self.Layout()
39 # end wxGlade
40
41 def OnBtn1(self, event): # wxGlade: MyFrame.<event_handler>
42 self.frame_1_statusbar.SetStatusText("Ouch!")
43 event.Skip()
44
45 # end of class MyFrame
46
47
48 class MyApp(wx.App):
49 def OnInit(self):
50 wx.InitAllImageHandlers()
51 frame_1 = MyFrame(None, -1, "")
52 self.SetTopWindow(frame_1)
53 frame_1.Show()
54 return 1
55
56 # end of class MyApp
57
58 if __name__ == "__main__":
59 app = MyApp(0)
60 app.MainLoop()