GUI Programming 10 – Error checking

As I am currently working on some GUI applications to take input in to text boxes, I have decided to enhance these programs a little further.

baseconv

 

 

 

 

 

 

 

The above program in the form it was when the screen shot was taken, didn’t take in to account what was actually entered in to the text box,  it could be blank, have text neither of which is going to produce any useful output (if any)

number = raw_input("Enter a number ")
i = number.isdigit()
while i != True:
	print("Input MUST be a number")
	number = raw_input("number of sides to shape ")
	i = number.isdigit()
print number

Taking the isdigit function,  I found a few months ago, I set about integrating this with the code for the program above,  this took a bit of trial and error but it now works in so far that if anything other than a numerical value is entered, then the entry box displays and error message.  Another consequence of this, is that if the entry box is blank (null) then the same thing happens.

baseconverror

 

 

 

 

 

 

 

So I have now added the code for this,

def test():
 
	msg = "error : must be a numerical value"
	i = testbox.get()
	y = i.isdigit()
	if y != True:
		testbox.insert(0,(msg))
	else:	
		x = int(testbox.get()) #get user input
		testbox.delete(0, END) # clear user input
		testbox.insert(0,bin(x)) # print user input converted in to requried base

Before I set about doing this to my actual base converter program (screenshot) I wrote a program that had a window, text entry box and a button, so that I could test everything out.  It worked so I then integrated it in to my program and changed variables , object names to suit.

Adding checking like this is important, as it reduces problems with programs.

The same idea could be achieved if text is required as the fuction isalpha is u sed to check if something is alphabetical.

Also if you are going to add new features to something if you can write a stand alone program to make sure it works then that really helps.  I have the basic input test program in 2 versions one text based and one graphical, it adds to my library of routines and solutions that can be called upon at a later date if I need to use them in future projects.

As always any questions please e-mail me or tweet me at @zleap14

I will be at the Exeter Pi jam on the 7th and the Torbay Pi jam on the 14 of June.