[ Number Dialog Image ]

Number Dialog


Using W within the IDE
Number Dialog / Calculator
Exercises

In this section we'll write a simple dialog box which accepts a number. To make things a little more interesting, we'll try to validate the number. If it's a number, we enable the OK button; otherwise we disable it.

We start by opening a new window in the IDE. We'll enter the program in there, and will run it using the IDE's run all command.

First we need to import some modules: Wkeys, because it contains useful constants and WASTEconst so that we can change the alignment of the text field.

import W
import Wkeys

import WASTEconst

We also need to set up a regular expression which matches a number. The one here is not perfect (it allows "-0", which is dodgy), but anything it matches will be recognised as a number by Python, and any number can be entered.

import re
num = re.compile("^-?([1-9][0-9]*|0)(.[0-9]+)?$")

We now start setting up the widgets. We use a modeless dialog window, and have a static text box giving the user a prompt.

w = W.Dialog((200, 92), "Number Dialog")
w.text = W.TextBox((4, 4, -4, 18), "Please enter a number:",
		fontsettings = ("charcoal",0,12,(0,0,0)))

The next section is the heart of the dialog. We set up a callback do_key() which checks the entry field's contents with the regular expression, and either enables or disables the OK button based on whether there is a match. We then create the field itself, passing the callback in as a parameter.

def do_key(char, modifiers):
	if (char != Wkeys.returnkey and char != Wkeys.enterkey):
		content = w.display.get()
		w.OK.enable(num.match(content))

w.display = W.EditText((4,22,-4,22), "0", callback = do_key,
		fontsettings = ("geneva",0,12,(0,0,0)))

Finally set set up the buttons. We have a callback done() for the OK button whcih prints the final value and closes the window; the "Cancel" button simply closes the window.

def done():
	print w.display.get()
	w.close()

w.OK = W.Button((-70, 60, 58,20), "OK", done)
w.cancel = W.Button((-140,60,58,20), "Cancel", w.close)

Then we set up some auxilliary information for the OK button. We make it the window's default button, so that it gets a highlight ring around it; and we bind the enter and return keys to the button's push() method.

w.setdefaultbutton(w.OK)
w.bind(Wkeys.returnkey, w.OK.push)
w.bind(Wkeys.enterkey, w.OK.push)

Finally we open the dialog, and as a last tweak, make the text field right-justified (we have to wait until after the text field opens before we can directly tweak the WASTE editor like this).

w.open()
w.display.ted.WESetAlignment(WASTEconst.weFlushRight)

You can get the dialog to display and run by running the contents of the IDE window. Running it multiple times will create multiple independent dialog windows. The complete source code is available to save you some typing.

Exercises

  1. If you want to guarantee that the number entered is a valid numeric type in Python, a better way to test for validity is to use the builtin coercion functions float(), int() or long() within a try-except structure. Modify the program to guarantee that Python will like the number entered.
  2. Modify the dialog so that it only accepts keystrokes which can contribute to writing a number.
  3. Modify the dialog to make a date-verifying dialog. Do the same for a time-verifying dialog.