Widgets & Event Handlers
IronPython & Windows Forms, Part III
This is part of a series of tutorials on using IronPython with Windows Forms.
Follow my exploration of living a spiritual life and finding the kingdom at Unpolished Musings.
In order for a program to do anything useful, it needs to provide a way for user input and be able to respond appropriately to that input.
For "rich client GUI" programs, the different ways of receiving input are fondly known as widgets. These include the menus, buttons, toolbars, check buttons, and text entry boxes (and so on) that we are all so used to. In .NET speak these are called controls.
In order to respond to user input, your program needs to layout these widgets within the GUI, and set functions to be called when the user performs actions like selecting a menu item or clicking on a button.
The Windows Form API makes creating widgets and registering the callback functions very easy, the layout can sometimes be a bit weird; but such is life.
The fact that all widgets are controls, means that they all inherit from the .NET base class Control. When (if) you browse the documentation for individual widgets, you will often see the phrase Inherited from Control beside properties and methods. This is very convenient. Once you know how to set the Width, Height or Location of one widget, you know how to do it for all of them.
OK, enough waffle, let's play with a widget. The most basic widget is the Label. It displays text on the GUI.
If you look in the example code on the Label class documentation page, you can see the following in C# :
Label label1 = new Label();
So we can see that the Label constructor takes no arguments.
In order to set the displayed text, location and size of a label in its parent control, we use the following properties :
If we don't specifically set any of these, then defaults will be used. The default text will look pretty dull.
In a minute we are going to add the following code to our example :
self.label.Text = "Please Click Me"
self.label.Location = Point(50, 50)
self.label.Height = 30
self.label.Width = 200
The last line of this snippet is important. To add a widget to a GUI you need to add it to the list of controls on it's parent widget. So uhmm... how do you do that ? Like that...
The order you add controls to the parent affects the layout the GUI.
For buying techie books, science fiction, computer hardware or the latest gadgets: visit The Voidspace Amazon Store.
Last edited Fri Nov 27 18:32:35 2009.