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] Getting Input from Tkinter Entry juliabrushett 6 12,678 May-30-2020, 03:29 PM
Last Post: Larz60+
  Converting Entry field value to integer in tkinter scratchmyhead 2 551 May-11-2020, 03:41 PM
Last Post: scratchmyhead
  [Tkinter] Tkinter adding entry values scratchmyhead 1 391 May-04-2020, 05:21 AM
Last Post: Yoriz
  [Tkinter] Entry box not showing 2 decimal places Chuck_Norwich 3 638 Apr-24-2020, 05:28 PM
Last Post: deanhystad
  Always lowercase entry in tkinter ReturnName 8 871 Apr-18-2020, 09:41 AM
Last Post: PeroPuri
  Can't Get Entry Var in Tkinter OCaraDoBaixo 1 829 Feb-18-2020, 06:08 PM
Last Post: Larz60+
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 1,362 Jan-23-2020, 09:00 PM
Last Post: HBH
  Newbie question with Tkinter Entry mariolopes 2 572 Oct-12-2019, 11:02 PM
Last Post: Larz60+
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 1,280 Mar-18-2019, 05:36 PM
Last Post: woooee
  [Tkinter] Tkinter Entry widget and KeyPad intrgration chiragarya1992 0 3,029 Aug-11-2018, 01:09 AM
Last Post: chiragarya1992

Forum Jump:

User Panel Messages

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