Tutorial Boa Constructor
Tutorial Boa Constructor
Tutorial Boa Constructor
and labels.
Save both the App1.py file and the Frame1.py file to the directory you created earlier. You can use the Save button on the Editor toolbar. Notice that the asterixes (*) disappear from the namewhen it is saved. These indicate that there are unsaved changes in the file. You now have an application, which just shows a blank frame. Use the Run Application button on the Editor toolbar to execute the application.
The above shows you in the Editor section the two new files
will
just
Getting Started Guide for Boa Constructor Next:Using the Designer to set the Title Up:Getting Started Guide for Boa Constructor Previous:Getting Started Guide for Boa Constructor
Getting Started Guide for Boa Constructor Next:Add a Status Bar Up:Getting Started Guide for Boa Constructor Previous:Creating a new Application
The Frame will be displayed as a drawing area (initially the title will show Frame1 Two new panes will be displayed in the Editor labeled the Data pane and the Sizer pane. The Inspector Windowwill display the Constr or Constructor pane. This pane allows you to edit the size, position, style, variable nameand title of a component. Edit
you to edit the size, position, style, variable nameand title of a component. Edit the field called Title. Give the frame the nameNotebook, when pressing return you will see that the title of the Designer window has changed. You have to save the changes using the Post button. You press the Post button either on the Inspector toolbar or the Editor toolbar. The Designer Windowwill close. You will notice that the source code has been updated to reflect the title. The Editor shows that the Source Code is changed by asterix on the notebook tabs, so you need to press the save button.
Getting Started Guide for Boa Constructor Next:Add a Status Bar Up:Getting Started Guide for Boa Constructor Previous:Creating a new Application
Getting Started Guide for Boa Constructor Next:Add a MenuBar Up:Getting Started Guide for Boa Constructor Previous:Using the Designer to Set the Title
Nowmove the mousecursor over the drawing frame. Press the mouseleft button in the drawing area. This will create a status bar in the frame. The status line within the Inspector will display on the left the nameof the current widget statusBar1, and on the right it shows which wxWidget class it is derived from, in this case wx.StatusBar . In the Inspector select the Properties pane. This pane is used to configure the properties of our status bar. Click to edit the value of Fields. The field will show a button with +++. Click the button. This opens the Collection Editor.
The Collection Editor is a tool, which is used to add multiple subcomponentsto components where required. Wewill add one field to the status bar, however you could add multiple fields. Press the New button on the Collection Editor. This creates a new field the status bar. This becomesthe current field in the Inspector. Edit the field Text. Set the namefrom Fields0 to status. The Collection Editor toolbar contains a Refresh button see the Inspector change in the Collection Editor window. within
Close the Collection Editor window. Select the Designer window. Click the mouse anywhere in the drawing area to make the frame (Frame1) the current component. Select the properties pane in the Inspector. Edit the property StatusBar. The drop down menuwill show our new status bar. Select the status bar, this is necessary so that the frame can managethe status bar, i.e. its possition at the bottom of the screen and its size.
Update the source code with the changes using either Post button . Save the source code changes using the Save button on the Editor toolbar.
Getting Started Guide for Boa Constructor Next:Add a MenuBar Up:Getting Started Guide for Boa Constructor Previous:Using the Designer to Set the Title
Getting Started Guide for Boa Constructor Next:Adding the Text Control Up:Getting Started Guide for Boa Constructor Previous:Add a Status Bar
Nowclick the left mousebutton either in the Editors Data View or in the Designer, in which case you have to be carefull to ensure that you click on Frame1 area and not on the statusbar. The menuwill not be visible on the frame. However, there will now be an entry on the Data View. Repeat the procedure. You should now have two wx.Menusin the Data View, called menu1 and menu2. Select menu1using the mouse. The Inspector will now display the Nameand Title for menu1. Edit the nameof the first wx.Menuto call it menuFile. Call the second wx.Menu menuHelp. Set the titles to File and Help respectively. Double click the menuHelpentry in the Data View. This opens the Collection Editor. The Collection Editor is used to add items to our menus. Press the New button on the Collection Editor. This creates a new menuitem within the dropdown menu. This becomesthe current item in the Inspector. Edit the field Label. Set the namefrom Items0 to About and I would also recommend set the ItemId from ITEMS0 to ABOUT. to Edit the field Help String to Display general information about Notebook. The Collection Editor toolbar contains a Refresh button. Press this button to see the Inspector change in the Collection Editor window.
In the Inspector, select the Events Pane. This is the pane used to configure events. Weneed to configure the action which occurs when the About menuitem is selected. Whenthe About menuitem is selected, and event called EVT_MENU generated and is sent to our program. Wemust add a method to our class to handle this event.
The left hand side of the events pane shows the groups of events which are available. For the menuitem, there is only MenuEvent group. Select this group using the mouse. The right hand side of the events pane now shows the events in the selected group. For the menuitem, there is only one event EVT_MENU the MenuEvent group. in Doubleclick on this Event using the mouse. The bottom of the Events pane shows the event handlers in your application for the current component (the About menuitem). You should now have a new handler called OnMenuHelpAboutMenu. This is the nameof the method which will be invoked when the About option is selected from the Help menu. Notice the naming of the event handler. Boa Constructor generates the namesin this manner. The event is the last part (Menu). The component is the middle part and here it is the About subcomponentof the menuHelp component. Finally, Boa Constructor follows the convention of prefixing all event handlers with the word On. Close the collection Editor.
menu.
From the Data View in the Editor, double click on the menuFile item to open the Collection Editor. Add five new items. Select each menuitem in turn, and label them Open, Save, Save As, Close and Exit, and again I would recommend also change the ItemId. Enter some help text to for each menuitem. Press the Collection Editor refresh button to display the new labels. Select each menuitem in turn. For each item select the Events pane in the Inspector, and add an event handler for EVT_MENU each item. to Close the collection Editor.
Select the main Frame, Frame1 on the Designer. The frame should now be the current control in the Inspector. Select the Properties pane (Props) in the Inspector. Edit the field MenuBar. This is a popup menu. Select your new menubar self.menuBar1. This property defines which menuBarto associate with the frame.
Save the changes using either Post button to close the Designer and let Boa generate the source code. Save the source code for your generated source file Frame1.py Run the program. You should now see the menusand the status bar. Whenyou select a menuoption, the help text should appear in the status bar.
Getting Started Guide for Boa Constructor Next:Adding the Text Control Up:Getting Started Guide for Boa Constructor Previous:Add a Status Bar
Getting Started Guide for Boa Constructor Next:Adding the File MenuFunctionality Previous:Add a MenuBar Up:Getting Started Guide for Boa Constructor
Rename the text field. The default nameis textCtrl1. Set the nameto textEditor. In the Constructor pane there is a field called Value. This field contains the default value of the control. Blank out this field. Update the source code with your new control using either Post button Save your source code changes. Run your application.
The textEditor field is automatically sized to the available space. If you resize the frame, the control resizes. Notice that wxWidgets provides you with a scrollbar. The field will automatically, scroll if you go beyond the bottom. If you type line longer than the width of the edit window, they will wrap. You also have cut and paste functionality, and block marking by default.
Getting Started Guide for Boa Constructor Next:Adding the File MenuFunctionality Previous:Add a MenuBar Up:Getting Started Guide for Boa Constructor
Getting Started Guide for Boa Constructor Next:Creating a Dialog WindowUp:Getting Started Guide for Boa Constructor Previous:Adding the Text Control
".",
"",
"*.*",
wx.OPEN)
This skeleton code creates a dialog. It interacts with the user. Whenthe dialog is finished it is destroyed. The words # Your code mark the position where we are to insert our own code. This code is triggered when the dialog returns wx.ID_OK, i.e. when the user clicked on the Open button. Wewill insert our code here. The wx.TextCtrl has a method which we will use to load a file into the edit window, for this we use the method LoadFile. You can delete the trailing event.Skip() as no other event will need to be called in this case. The event.Skip() nameis a bit confusing, you should call event.Skip() in events were other event handlers should ALSObe executed. It was needed when the code was generated because Python reports an error if there is a method with no body. As part of the functionality of our application we must be able to access the file nameso that the Save menuoption can save to this file, therefore we added the line self.FileName=filename. The self.SetTitle((Notebook %s) % filename) line changes the title to show what file is being worked on. The listing below shows our new code.
def OnMenuFileOpenMenu(self,event): dlg = wx.FileDialog(self, "Choose a file", ".", "", "*.*", try: if dlg.ShowModal() == wx.ID_OK: filename = dlg.GetPath() # Your code self.textEditor.LoadFile(filename) self.FileName=filename self.SetTitle((Notebook %s) % filename) finally: dlg.Destroy()
wx.OPEN)
dlg.Destroy() Wemust repeat the exercise to provide the Save As functionality. Insert a file dialog into the body of OnFileSaveasMenu. This is a File Save dialog. Changethe prompt, parameter 2 to the wx.FileDialog to "Save File As". Changethe style, parameter 6 to wx.SAVE. The method to save the file is called SaveFile. Again, we save the filename value for use by the Save menuoption. The listing below shows the code. def OnMenuFileSaveasMenu(self, event): dlg = wx.FileDialog(self, " Save file as", ".", "", "*.*", try: if dlg.ShowModal() == wx.ID_OK: filename = dlg.GetPath() # Your code self.textEditor. SaveFile (filename) self.FileName=filename self.SetTitle((Notebook %s) % filename) finally: dlg.Destroy()
wx.SAVE )
Next we will implement the Close menuoption. In this method, we simply clear the textEditor control, the FileName membervariable and reset the title.. def OnMenuFileCloseMenu(self, event): self.FileName = None self.textEditor.Clear() self.SetTitle(Notebook) Next we will implement the Exit menuoption. In this method we need to terminate the application. All wxPython applications are terminated by closing the toplevel window. In our case we only have the Frame1 window. To terminate the application we invoke the Close() method for Frame1. def OnMenuFileExitMenu(self, event): self.Close() Next we will implement the Save menuitem. This menuitem will save the file using the current name, which is stored in the variable self.FileName. Whenthere is no current file, the self.FileName variable is set to None. In this case the Save menuoption must act as the Save As menuoption. The variable FileName must be created when Frame1 is constructed. Wemust add it to the constructor. You can add your application code to the end of the default constructor (__init__). def __init__(self, parent): self._init_ctrls(parent) self.FileName=None Now, we are safe to implement the Save functionality. Wecheck if there is a current filename. If there is we can save the contents to that filename. Otherwise, we just call the Save As method. def OnMenuFileSaveMenu(self, event): if self.FileName == None: return self.OnFileSaveasMenu(event) else: self.textEditor.SaveFile(self.FileName) Wehave now implemented the functionality of the editor. Wecan open files, save them. Your editor should look similar to what is shown on the image below. The file App1.py was opened edit, and
as was selected
Getting Started Guide for Boa Constructor Next:Creating a Dialog WindowUp:Getting Started Guide for Boa Constructor Previous:Adding the Text Control
Getting Started Guide for Boa Constructor Next:Getting Started Guide for Boa Constructor Up:Getting Started Guide for Boa Constructor Previous:Adding File MenuFunctionality
Save the three source files. You can run the application now. Whenyou select the About option from the Help menuyour new Dialog will appear. Notice that the dialog is modal, i.e. you must close it before you can access the Frame1 window. Exit the application and return to Boa Constructor. Nowwe will add some fields to the dialog. For this exercise we will need a bitmap file. For the demonstration I used one called Boa.jpg. You can create your own bitmap using a paint utility. Copy the bitmap to your application directory. Select the Dialog1.py pane. Start the Designer by clicking on the button . First we shall add a label to the Dialog. On the palette select Basic Controls. From this pane select the wx.StaticText control. On the Designer click the mouse button to create the control. On the Inspector edit the field Label. Set the value to Note book Simple Text Editor. Notice that the label in the Designer will grow to accomodate your text. Weuse the style property to configure the alignment of the text within the label. Set the style property to wx.ALIGN_CENTRE select this style after clicking on or the check box to the left of style. Select the Properties pane in the inspector. Edit the field called font. Set the font to a reasonably large font, e.g. 12 or 14 point. Notice that you can change both the font and point size with this property. In the Designer window your label will appear with eight markers on the edges. You click the mouseleft button (and hold it down) on one of these markers then move the mouseto resize the box. You can also click in the centre of the label, and hold down the mousebutton, to move the label. Position the label centrally at the top of the box. Nowadd another label below the first. Set the text to This is my first Boa
Nowadd another label below the first. Set the text to This is my first Boa Contstructor application. In the Inspector, select the Properties pane. Edit the value BackgroundColour. Pick a colour from the set available and press OK. Now reposition and resize your label until it looks balanced. Next we will add the bitmap. From the Basic Controls select the wx.StaticBitmap control. Place this below the second label on your dialog. In the Inspector select the Constructor pane. Edit the Bitmap field. This will give you an Open File dialog. Choose the bitmap you painted earlier. The wx.StaticBitmap field in the Designer will change to accomodate your bitmap. Move the bitmap until it is balanced below the two labels. Finally, we will add a button to the Dialog. In the Palette select the Buttons pane. Select the basic button type, wx.Button. Place this on the form below the bitmap. On the Inspector Constructor pane edit the Label. Set this to Close. Select the Events pane in the Inspector. Add a handler for event type EVT_BUTTON. Hint: select the event group first, then the event. This are all the controls we are going to add to the Dialog. Size the Dialog to accomodate the controls. Reposition and resize the controls until you feel they are nicely balanced. Select the Dialog1 in the Designer. In the Constructor pane in the Inspector, edit the Title field. Set this to About Notebook. Press either post button to update the source code with your changes. Finally, we need to implement the event handler for the Close button. In the Editor select the source for Dialog1. Go to the source for your method OnButton1Button. We will use the same Close method as we used in the Exit menuitem. Note that this closes the window. Closing the root window exits the application. However, closing a child window will simply return to the parent window . def OnButton1Button(self, self.Close() Run the application. event): this.
Congratulations: You have built your first application using Boa Constructor. Your editor is complete. In this tutorial you have used the core components Boa. of
Take time to review what you have done so far. You have learnt how to: Create an application. Create frames, menusand status bars. Create controls such as buttons, text entry fields and labels.
Create controls such as buttons, text entry fields Configure the controls to your requirements. Work with common dialogs. Design your own dialogs.
and labels.
Getting Started Guide for Boa Constructor Next:Getting Started Guide for Boa Constructor Up:Getting Started Guide for Boa Constructor Previous:Adding File MenuFunctionality
Getting Started Guide for Boa Constructor Next:Getting Started Guide for Boa Constructor Up:Getting Started Guide for Boa Constructor Previous:Adding File MenuFunctionality
the Collection Editor for the listctrl. Create the columns "First name, Last name, City and Country". Click on the "Sizers" pane and double click on bsMain to open its Collection Editor. Then click on the self.listCtrl1 and change the Border from 0 to 2 (or what you find appropriate for a border around this control) and change Flag from 0 to wx.ALL | wx.EXPAND change Proportion from 0 to 1. These changes will ensure that you have and 2 pixels space around the listctrl and that it will use up as much space as is available. If you run this little application now you will see that the listctrl takes up all the available space. On the "Sizers" pane open the Collection Editor for the fgsFields sizer and add 12 new items, when you now look at the Designer it will show these items in red. From the "Basic Controls" Palette pane select the wx.StaticText control and drop it onto the top left red area and to the right of it drop a wx.TextCtrl and then repeat this until your Designer screen and the fgsFields collection editor look something along these lines.
Make sure to rename the controls to nameswhich make sense (i.e. firstName, lastName, address, postalCode, city and country). Nowwe need to set the Border, Flag and Proportion for each of these controls. For wx.StaticText I suggest: 2, wx.ALL | wx.ALIGN_CENTER_VERTICAL0 and For wx.TextCtrl I suggest: 2, wx.ALL | wx.EXPAND 0 and On the "Sizers" pane you need to select the fgsFields sizer and make the second column growable which you can do from the Inspector "Props" pane by clicking on the "..." next to "(Growables)". And for this to take any effect you need to change the Flag for the fgsFields sizer in the bsMain sizer from 0 to wx.EXPAND. So, now you should see something like this in the Designer.
If you run it at this point and resize the window you can see the sizers at work. You might also notice that you see scrollbars on the Address field and it is larger then other fields. For this you need to change its style from 0 to wx.TE_MULTILINE and in the Designer you enlarge it to the hight you want to allocate for it. Whenyou run the application you should see something along these lines.
Wewill also need some buttons for this, so we can add, delete, save and close this form. For this open the Designer again and drop another flexGridSizer (I will nameit fgsButtons) onto the "Sizers" tab and then add it to the bsMain sizer. Then add four items to the fgsButtons sizer and then drop wx.Button controls onto the red squares on the designer. In the sizer Collection Editor change the Border to 2, the Flag to wx.ALL for all these buttons. Then select the first button by double clicking its entry in the Collection Editor and in the Inspector "Constr" pane change the label from button1 to "" (blank) and the namefrom button1 to "add" and the Id to wx.ID_ADD. Repeat this for the others but namethem delete, save and close and use the
Repeat this for the others but namethem delete, save and close and use the appropriate wx.ID_ entries (having access to the stock button IDs is new in Boa 0.6.0, it will only work if you blank the label.)
You should now see something like the above when you run it. Obviously you only have the GUI code at this point and one would have to flesh all this with code for each of the buttons, but for the momentthis goes beyond this tutorial. Please note that the file generated during this example is also available directory "Examples\guide" under your Boa installation directory. in the
For coding guide lines you might also want to consult the wxPython style guide http://wiki.wxpython.org/index.cgi/wxPython_Style_Guide . Getting Started Guide for Boa Constructor Next:Getting Started Guide for Boa Constructor Up:Getting Started Guide for Boa Constructor Previous:Adding File MenuFunctionality