Python Forum
Entry Validation in tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Entry Validation in tkinter
#1
Hi there..

I want to know how the inputs to an entry widget in tkinter is validated so as to accommodate only digits 0-20. This widget is going to accept marks of students (minimum is 0 and maximum is 20) from the user.

Thanks in advance!
Reply
#2
Any validation must be written by you.
The entry widget provided by tkinter is a raw widget and will accept anything that is entered.
Which makes sense, as it's not known what the target use will be until a user decides what is or isn't allowed.
What have you tried so far?
Reply
#3
(Oct-27-2020, 04:06 PM)Larz60+ Wrote: Any validation must be written by you.
The entry widget provided by tkinter is a raw widget and will accept anything that is entered.
Which makes sense, as it's not known what the target use will be until a user decides what is or isn't allowed.
What have you tried so far?

I wrote the following function to validate the Entry widget. It only accepts numbers. But I want to allow numbers between 0 and 20 only. How to achieve this?

# function to validate mark entry
def only_numbers(char):
    return char.isdigit()
validation = root.register(only_numbers)

#text box to enter marks
t2=Entry(root,validate="key", validatecommand=(validation, '%S'))
t2.pack()
Reply
#4
please show enough (or all) code so that it can be run.

Basically:
  • create an event callback when data entered in widget.
    (do this on widget creation with command=callback_name, or in bind command)
  • if any of the following occurr: Fail, clear widget, show error message (using tkinter.messagebox), return without setting value
    • Not a digit
    • accumulated digits > 20
    • Number of digits > 2
  • if ok set value and exit event


Please note;, In event callback, you must keep track number of digits entered as an entry widget creates an event each time a value is entered.
Reply
#5
here's an example on stack overflow validation example
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Making entry global in tkinter with multiprocessing luckyingermany 2 2,284 Jan-21-2022, 03:46 PM
Last Post: deanhystad
  Tkinter Exit Code based on Entry Widget Nu2Python 6 2,872 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  [Tkinter] Update variable using tkinter entry methon drSlump 6 5,093 Oct-15-2021, 08:01 AM
Last Post: drSlump
  Tkinter | entry output. Sap2ch 1 1,949 Sep-25-2021, 12:38 AM
Last Post: Yoriz
  .get() from generated Entry widgets in tkinter snakes 4 4,155 May-03-2021, 11:26 PM
Last Post: snakes
  [Tkinter] Getting Input from Tkinter Entry juliabrushett 6 21,216 May-30-2020, 03:29 PM
Last Post: Larz60+
  Converting Entry field value to integer in tkinter scratchmyhead 2 4,883 May-11-2020, 03:41 PM
Last Post: scratchmyhead
  [Tkinter] Tkinter adding entry values scratchmyhead 1 2,164 May-04-2020, 05:21 AM
Last Post: Yoriz
  [Tkinter] Entry box not showing 2 decimal places Chuck_Norwich 3 5,617 Apr-24-2020, 05:28 PM
Last Post: deanhystad
  Always lowercase entry in tkinter ReturnName 8 5,898 Apr-18-2020, 09:41 AM
Last Post: PeroPuri

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020