GUI development with wxglade

Similar documents
Sample Table. Columns. Column 1 Column 2 Column 3 Row 1 Cell 1 Cell 2 Cell 3 Row 2 Cell 4 Cell 5 Cell 6 Row 3 Cell 7 Cell 8 Cell 9.

SECTION 5: Finalizing Your Workbook

Company Setup 401k Tab

Task Force on Technology / EXCEL

Easy Calculations in Excel

Hands-on Guide. FileMaker Pro. Using FileMaker Pro with Microsoft Office

Microsoft Publisher 2010 What s New!

Migrating to Excel 2010 from Excel Excel - Microsoft Office 1 of 1

Getting Started with Excel Table of Contents

Excel Intermediate Session 2: Charts and Tables

itunes Basics Website:

Tips and Tricks for Printing an Excel Spreadsheet

Managing Your Desktop with Exposé, Spaces, and Other Tools

Create a Poster Using Publisher

Gestation Period as a function of Lifespan

UOFL SHAREPOINT ADMINISTRATORS GUIDE

INTRODUCTION TO DESKTOP PUBLISHING

To add a data form to excel - you need to have the insert form table active - to make it active and add it to excel do the following:

Programming in Access VBA

Content Author's Reference and Cookbook

To Begin Customize Office

S A M P L E C H A P T E R

State of Illinois Web Content Management (WCM) Guide For SharePoint 2010 Content Editors. 11/6/2014 State of Illinois Bill Seagle

Writer Guide. Chapter 15 Using Forms in Writer

Introduction to MS WINDOWS XP

Scientific Visualization with wxpython and Matplotlib. Scott Pearse CSCI 5448 Spring 2011

Introduction to Microsoft Access 2003

Intro to Excel spreadsheets

Formulas, Functions and Charts

Excel 2007 Basic knowledge

USER MANUAL (PRO-CURO LITE, PRO & ENT) [SUPPLIED FOR VERSION 3]

Using FileMaker Pro with Microsoft Office

Working with sections in Word

Dreamweaver. Introduction to Editing Web Pages

Smart Connection 9 Element Labels

Chapter 15 Using Forms in Writer

Introduction to dobe Acrobat XI Pro

Microsoft Word defaults to left justified (aligned) paragraphs. This means that new lines automatically line up with the left margin.

Using Microsoft Project 2000

Excel basics. Before you begin. What you'll learn. Requirements. Estimated time to complete:

Microsoft Access 2010 Overview of Basics

Database Concepts (3 rd Edition) APPENDIX D Getting Started with Microsoft Access 2007

MS WORD 2007 (PC) Macros and Track Changes Please note the latest Macintosh version of MS Word does not have Macros.

Microsoft Excel 2013 Tutorial

Microsoft Word 2010 Training

An Introduction to Modern Software Development Tools Creating A Simple GUI-Based Tool Appleʼs XCode Version 3.2.6

5. Tutorial. Starting FlashCut CNC

An Overview of Outlook

Microsoft PowerPoint 2011

Microsoft Access 2010 handout

Working with SmartArt

Click on various options: Publications by Wizard Publications by Design Blank Publication

Working with Video in PowerPoint 2010

Excel Pivot Tables. Blue Pecan Computer Training Ltd - Onsite Training Provider :: :: info@bluepecan.co.

paragraph(s). The bottom mark is for all following lines in that paragraph. The rectangle below the marks moves both marks at the same time.

In this session, we will explain some of the basics of word processing. 1. Start Microsoft Word 11. Edit the Document cut & move

Microsoft Access 2010: Basics & Database Fundamentals

Microsoft PowerPoint Exercises 4

Building Qualtrics Surveys for EFS & ALC Course Evaluations: Step by Step Instructions

Site Maintenance. Table of Contents

Scientific Graphing in Excel 2010

AODA Mouse Pointer Visibility

Intermediate PowerPoint

Creating a Newsletter with Microsoft Word

Excel -- Creating Charts

Acrobat X Pro Accessible Forms and Interactive Documents

In This Issue: Excel Sorting with Text and Numbers

Kentico CMS 7.0 User s Guide. User s Guide. Kentico CMS

1.5 MONITOR. Schools Accountancy Team INTRODUCTION

1-Step Appraisals Jewelry Appraisal Software

Rapid Application Development with GNOME and Python

Using Spry Widgets. In This Chapter

Dreamweaver. Links and Tables

TABLE OF CONTENTS SURUDESIGNER YEARBOOK TUTORIAL. IMPORTANT: How to search this Tutorial for the exact topic you need.

National RTAP Marketing Transit Toolkit Customizing Templates in Microsoft Publisher

Microsoft Word 2010 Prepared by Computing Services at the Eastman School of Music July 2010

Content Author's Reference and Cookbook

Avery Wizard: Using the wizard with Microsoft Word. This is a simple step-by-step guide showing how to use the Avery wizard in word

Introduction to Microsoft Excel 2007/2010

Basic Excel Handbook

Enhanced Formatting and Document Management. Word Unit 3 Module 3. Diocese of St. Petersburg Office of Training Training@dosp.

Microsoft Excel 2010 Part 3: Advanced Excel

Computer Training Centre University College Cork. Excel 2013 Pivot Tables

WHAT S NEW IN MS EXCEL 2013

A Beginner s Guide to PowerPoint 2010

MAS 90 MAS 200 Tips, Tricks and Frequently Asked Questions (FAQ s) Prepared by: The Fitzgerald Group August 11, 2004

Offline Files & Sync Center

Microsoft Excel 2013 Splitting Windows and Fixing Panes (Level 3)

Introduction to Programming with Xojo

Using Microsoft Access

warpct.com Basic Computer Skills MS Windows XP Workbook courseware by WARP! Computer Training

Scan Physical Inventory

8 CREATING FORM WITH FORM WIZARD AND FORM DESIGNER

Create Charts in Excel

Objectives. Chapter 2: Operating-System Structures. Operating System Services (Cont.) Operating System Services. Operating System Services (Cont.

Produced by Flinders University Centre for Educational ICT. PivotTables Excel 2010

Microsoft PowerPoint 2010 Handout

PowerPoint 2007 Basics Website:

Transcription:

GUI development with wxglade Johan Vromans Squirrel Consultancy <jvromans@squirrel.nl> This document is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. In the beginning... In the beginning was the command line. To get the computer to do something, the user typed a command to the computer. In the early days, the commands were typed on punched paper tape, or punched cards. Later, the commands could be typed in at a typewriter-like interactive terminal. Although powerful, using this method to control a computer required knowledge of a vast collection of commands, with their arguments, options, and variations. Around 1980 a new method to interact with a computer was developed: the Graphical User Interface, conveniently called GUI. GUIs displays visual elements such as icons, windows, and other gadgets. As we all know, the precursor to the GUI was invented by researchers at the Stanford Research Institute, and later brought to maturity by researchers at Xerox PARC. It was adapted by Apple in 1983, then by Microsoft in 1985, and the rest, as they say, is history. Examples of systems that currently use GUIs are Mac OS, Microsoft Windows, NEXTSTEP, Palm OS and the X Window System. GUI Basics A typical GUI application looks something like this: 24/10/2005 1

Generally, this is called the application window. It has a title bar at the top, with icons to close and resize it. In the window itself you can see a menu bar with pull-down menus, some windows with information, scrollbars, buttons, and a status bar at the bottom. Technically, all the items on the screen are windows, some large, some small. Some contain other windows. A button is a small window that has a border and a text in it. A scrollbar is a window that has small triangle-buttons and a slider. The buttons and the slider are also windows. When a button is clicked, it is temporary replaced by another window that looks like a pressed button. Simultaneously, it has to trigger some application function. To build an application by grouping windows like this would be a tedious job, and a great pain to accomplish. For this purpose, window systems have been providing toolkits with pre-constructed items called widgets. Using the appropriate toolkit, building the application becomes significantly easier. A button is now a widget that knows how to display its text, and automatically handles the changing appearance when clicked. The scrollbar is a single widget that knows how to move the slider, and feeds back on the actual position. But there is more. A scrollable window plus its scrollbars is itself a widget and knows how to deal with content and slider changes. Building a GUI application using a suitable toolkit becomes a feasible job. Microsoft Windows and Mac OS have standard toolkits to be used for GUI applications. For the X Window System many popular widget toolkits exist, such as Motif (CDE), Qt (KDE) and GTK+ (GNOME). Another important toolkit is the wxwidgets 1 toolkit, that provides a platform-independent set of widgets implemented on X, Motif, GTK, Mac OS, Palm OS, Microsoft Windows, and several more. A note on GUI design The design of the GUI is very important since the GUI guides the user though the application logic. A badly designed GUI will confuse the user, making it hard to know what to do next. Fortunately, there are many good documents and books on this topic. The Macintosh user interface would not have become so successful without a strict style guide that all application were to follow. Projects like Gnome and KDE also have rather strict guidelines. Unfortunately, many application designers do not seem to read this information, or just assume they know better 2. Introducing Most widget toolkits can be used from several programming languages, and wxwidgets is no exception. It is written in C++ but can be used from virtually any language that has the basic capabilities to interface with C++ libraries. A Perl extension called wxperl allows Perl 1 Formerly called wxwindows. 2 See, e.g., http://www.frankmahler.de/mshame/ 24/10/2005 2

programs to use the wxwidgets toolkit. wxperl is written and maintained by Mattia Barbon 3, hosted on SourceForge 4, and supported by an active group of users 5. A typical wxperl program consists of two parts: the main program, and the wxapplication. This is a user defined class that derives from the wxwidgets class Wx::App. The wxperl program then uses it as follows: # Create an instance of the Wx::App-derived class. my $app = MyApp->new(); # Start processing events. $app->mainloop(); After calling MainLoop, Wx takes over control and starts listening for events. When an event happens, e.g., the user clicks on a button, Wx will dispatch control to the appropriate event handler and start waiting for the next event, and so on. A simple application could consist of a simple window, a so called frame, and a piece of text. The definition of the Wx::App derived class, in this example MyApp, would look like: package MyApp; use base qw(wx::app); sub OnInit { my ($self) = shift; # Create a new frame. my $frame = MyFrame->new(); # Set it as top frame. $self->settopwindow($frame); # Show it. $frame->show(1); The only method defined in this class is OnInit, a method that gets called automatically when an instance of the class is created. In this example, OnInit instantiates the MyFrame class and displays it. MyFrame, as you can guess, implements the application window (Frame) and its contents: package MyFrame; use base qw(wx::frame); # This imports some constants. use Wx qw(wxdefaultposition wxdefaultsize wxhorizontal); sub new { # New frame with no parent, id -1, title 'Hello, world!'. # Default position and size 300, 150. my $self = shift->super::new(undef, -1, 'Hello, world!', wxdefaultposition, [350, 100]); my $sz = Wx::BoxSizer->new(wxHORIZONTAL); my $tx = Wx::StaticText->new($self, -1, 'Hello, World!', wxdefaultposition, wxdefaultsize); $sz->add($tx); $self->setsizer($sz); return $self; 3 mattia.barbon@libero.it 4 http://wxperl.sourceforge.net 5 wxperl-users@lists.sourceforge.net 24/10/2005 3

Ignoring most details, the constructor of the MyFrame class creates a new Frame, a Box- Sizer (a kind of generic container) and a StaticText widget. It connects the text widget and sizer to the frame and returns the newly created MyFrame object. When run, this is how it looks: To turn this into a full application would require adding a vast amount of widgets, connect them, add the logic, and so on. While this is certainly doable (I ve done it a couple of times ) it s a tedious job. Introducing wxglade is a user interface designer program for wxwidgets. It is written and maintained by Alberto Griggio 6, hosted on SourceForge 7 and actively supported by an active group of users 8. It is capable of generating wxperl, wxpython and C++ code. wxglade works like building blocks, you start with a blank piece of window, called a canvas, and you put the individual widgets on it. widgets tree view canvas properties preview 6 alberto.griggio@gmail.com 7 wxglade.sourceforge.net 8 wxglade-general@lists.sourceforge.net 24/10/2005 4

In this overview picture, you can see the most relevant components that play a part in wxglade. First, on the top left, the wxglade widgets panel. It enumerates the widgets supported and you can click and drop widgets from here on the canvas. Below the widgets panel is the properties panel. It displays a number of characteristics of the widget currently being worked upon. At the right, on top, is the tree structure of the application. As you can see, the application consists of a frame (frame_1), that contains a sizer (sizer_1), that on its turn contains the static text widget (label_1). Below the tree structure is the actual canvas being worked on, called the design view. It more or less looks like the actual application, but it is not quite WYSIWYG since it contains some additional handles to play tricks with. wxglade provides for an exact preview, you can see it s window below the design view. Finally, at the bottom, is the window of the actual application. Not surprisingly it looks exactly as the manually crafted application shown earlier (since that is what we tried to achieve). However, this version took just a couple of mouse clicks and keystrokes instead of having to write a lot of lines of code. Maintenance and re generation Generating a program is just one step. As you can guess, the program logic, i.e., what must be done when the buttons are pushed, must still be filled in. And what if you need to change parts of the GUI afterwards? wxglade uses a well-known technique of guarded regions. In the generated code, you can see several comments that are actually instructions for wxglade. For example, a constructor: sub new { my( $self, $parent, $id, $title, $pos, $size, $style, $name ) = @_;... # begin wxglade: MyFrame::new $style = wxdefault_dialog_style unless defined $style;... # end wxglade return $self; The parts between # begin wxglade and # end wxglade are private property. Every time the program is re-generated this part will completely be overwritten. On the other hand, everything outside these regions may be freely modified and augmented. This way it is possible to add application code to the program and still have wxglade control its own parts. Using sizers One of the major problems with building GUIs is that the user can, or at least should be able to, move and resize the windows. So it is not just a matter of placing the widgets on the canvas, more important is what to do when the canvas changes size. Several tech- 24/10/2005 5

niques have been developed over the years to solve this problem. Two popular techniques are springs, where the widgets are tied to the canvas, and to each other, using stretchable springs, and layout managers, where the widgets are laid out using external constraints like all on top, or north east south west. wxglade uses sizers for this purpose. As already mentioned, sizers are some kind of container. They can contain other widgets, including sizers. But sizers have two important properties, called spread and expand. To demonstrate these, let s assume we have a canvas that contains a BoxSizer, that s a sizer that divides the area into one or more horizontal or vertical areas. Initially, wxglade shows this sizer like this: The area is (evenly) divided in two, empty, parts. Now we drop a text widget into each of the areas. This is how it looks like now: Since each of the text widgets has its own default size, which is rather small, the sizer shrinks to accommodate this. Now, let s take a look at the properties of the text widget: The important properties are Option and Expand. Option controls how the available space is distributed (spread) horizontally over the participating widgets. Currently, it is set to zero, indicating that no spread is applied for this widget, and its default size must be used. Expand controls whether the widget will expand to the available space vertically. 24/10/2005 6

If we want both widgets to get equal space, we need to set Option to an equal value, e.g., 1. Finally, by checking Expand for both widgets: When run, or previewed, this is how it now looks like: Now, we can resize the window, keeping the placement of the widgets in tact: Complex situations can be accomplished by carefully crafting sizers within sizers. For example, this dialog: 24/10/2005 7

It is constructed as follows: spacer Just a vertical sizer with three slots, the middle slot contains stretchable space. The top area contains a sizer with a text and border. This sizer is further divided using a GridSizer. It has rows and columns and you can specify one or more rows and columns to be growable (stretchable): text/border sizer flexible grid sizer growable column The bottom area is divided horizontally using a sizer, where the third slot is a spacer: small border spacer When we resize the window, we see all the parts play together: 24/10/2005 8

Events In a GUI application events play a crucial role. When the user clicks a button, or types some text in a text area, events are generated that must be acted upon by the application. There are many types of events, and complex widgets can generate even more complex event sequences, but the basic principle is the same: the application program has to designate an event handler for a particular event, i.e., a subroutine that gets called by the Wx framework when the event happens. And wxglade can help with event handlers as well. Examine a trivial button. On the property sheet, the Event tab, there s a single event entry: EVT_BUTTON. This is the event that happens when the user clicks the button. Here we can supply the name of the callback routine that is to handle the event. Traditionally, these handlers have names like OnClicked, OnClose and so on. We ll choose OnOKClicked here. When wxglade has re-generated the code for the application, it has taken care of setting up the event handler, and also generated a piece of code for the handler, to be used as a starting point. # wxglade: MyDialog2::OnOKClicked <event_handler> sub OnOKClicked { my ($self, $event) = @_; $event->skip; This subroutine effectively does nothing, $event->skip just means: not for me, pass this event to someone else who may be interested. A trivial thing to add would be a warn message: # wxglade: MyDialog2::OnOKClicked <event_handler> sub OnOKClicked { my ($self, $event) = @_; warn("user pressed the OK button\n"); Conclusions While building a GUI application still remains a time consuming task, much of the more tedious parts of the job can be handled by wxglade. In fact, it would be a very bad idea to manually code the widgets, sizers, and events since the smallest change in design or layout might require enormous modifications. On the other hand, there s a limit to what a tool like wxglade can do. In the end, it will always be the programmer who decides. 24/10/2005 9