LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-17-2016, 01:26 PM   #1
leprechaun3
LQ Newbie
 
Registered: May 2016
Posts: 20

Rep: Reputation: Disabled
Check type of input from user - Python3.4/Tkinter


I'm making a small program for learning purposes. The user is asked to input numbers into a few entry boxes, but I would like for the program to give focus to the box that does not contain an appropriate value. I already have it print an error message to the user from ValueError, but this is just in the event that any of the boxes does not contain an appropriate number (I used an if statement to check for entry boxes that contain a 0 or negative when they should only have a positive number since those would not be caught by ValueError). So after it prints the error message I want it to figure out which box is the problem and give that box focus. I found a suggestion for using
Code:
if not v.get():
which works if the box is empty, but does not work if the box contains a non-number (though it still prints the error message). Ideas on how to get around this?

An earlier iteration of the program is already posted here
 
Old 08-22-2016, 12:48 AM   #2
leprechaun3
LQ Newbie
 
Registered: May 2016
Posts: 20

Original Poster
Rep: Reputation: Disabled
Well I have found a way to make it work, but it does not seem very clean or efficient. So if anyone has ideas on how to clean this up it would be appreciated.
Code:
...
except ValueError:
   if not v.get(): #Is there a way for me to group this in with the next if statement?
      v_error()
   else:
      v2 = ord(v.get())
      if not 47 < v2 < 58: #this excludes the numeric characters in case the exception was caused by a different box
         v_error() #Calls a separate function so I only have to write each error message and the commands to go with it once
      elif not w.get():
         w_error()
      else:
         w2 = ord(w.get())
         if not 47 < w2 < 58:
             w_error()
         elif not x.get():
             x_error()
         else:
             x2 = ord(x.get())
             if not 47 < x2 < 58:
                 x_error()
Again, I just feel like there must be something cleaner than this

Last edited by leprechaun3; 08-22-2016 at 12:50 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Please critique my python3.4/tkinter code (short program) leprechaun3 Programming 3 07-21-2016 12:33 PM
[SOLVED] array can't check for user input duplicate integers andrew.comly Programming 8 03-25-2015 07:43 AM
[SOLVED] PHP check&change input type Skyer Programming 4 12-29-2011 05:20 AM
Bash : how do i check the user input and make sure is only chracter or only number ? CheeSen Programming 10 09-07-2008 03:15 PM
check user input for IP ilnli Programming 7 08-08-2007 06:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration