By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,814 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,814 IT Pros & Developers. It's quick & easy.

Throwing the error message in Tkinter

100+
P: 440
Hi ,

I would like to throw a error/warning message to the user based on the Inputs given.

Case 1) If the inputs are not given correctly.I have to diplaythe message,saying "Enter the correct inputs"

Case 2) In between the functions,if I am getting a wrong input data ,I have to stop further execution of the application and display the error message " Calculated xyz values are wrong ..."

I hope we have to use "try " and "catch' blocks.

Could anybody provide the snippet of the code for catching the above cases error.

Thanks
PSB
Apr 30 '07 #1
Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Hi ,

I would like to throw a error/warning message to the user based on the Inputs given.

Case 1) If the inputs are not given correctly.I have to diplaythe message,saying "Enter the correct inputs"

Case 2) In between the functions,if I am getting a wrong input data ,I have to stop further execution of the application and display the error message " Calculated xyz values are wrong ..."

I hope we have to use "try " and "catch' blocks.

Could anybody provide the snippet of the code for catching the above cases error.

Thanks
PSB
It seems you want your code to continue, but loop back to give the user a chance to input valid data. This may not be the best way:
Expand|Select|Wrap|Line Numbers
  1. while True:
  2.     data = raw_input("Enter data")
  3.     if test_user_entered_data(data): # pass to a test function, return True if valid
  4.         break # continue to process the valid data
  5.     else:
  6.         print "You entered invalid data. Please enter again."
Apr 30 '07 #2

bartonc
Expert 5K+
P: 6,596
Hi ,

I would like to throw a error/warning message to the user based on the Inputs given.

Case 1) If the inputs are not given correctly.I have to diplaythe message,saying "Enter the correct inputs"

Case 2) In between the functions,if I am getting a wrong input data ,I have to stop further execution of the application and display the error message " Calculated xyz values are wrong ..."

I hope we have to use "try " and "catch' blocks.

Could anybody provide the snippet of the code for catching the above cases error.

Thanks
PSB
Creating your own Exception classes is a very useful thing to learn:
Expand|Select|Wrap|Line Numbers
  1. class MyAppError(exception):
  2.     def __init__(self, message):
  3.         self.message = message
  4.  
  5.     def __repr__(self):
  6.         return self.message
  7.  
  8. try:
  9.     if A + B + C != ExpectedResult:
  10.          raise MyAppError(" Calculated xyz values are wrong ...")
  11. except MyAppError, msg
  12.     print msg
You can also raise any type of built-in exception the same way. Another trick is to:
Expand|Select|Wrap|Line Numbers
  1. try:
  2.     aValue = aList[anIndex]
  3. except IndexError:
  4.     # do something usefull
  5.     raise
Which allows you to intercept the error and then let it continue to propigate up the call chain.
Apr 30 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.