Home>

I am a python beginner.
Please let me know if there are any deficiencies.

I thought it was close to this, but the solution seems to be different ...
https://www.tutorialfor.com/go.php?id=60174

I'm making an attendance management app.
I have a 4-digit employee number entered in the entry, but I noticed that it could contain other numbers.
How do you prevent entries other than numbers from being entered?

Error message

An entry other than a number is entered in an entry where you only want to enter numbers

Applicable source code
import tkinter as tk
root = tk.Tk ()
#entry set
Number = tk.Entry (font = ("15", "25"), textvariable = sv)
Number.place (x = 175, y = 100)
InEnter = tk.Label (root, text = "attendance", font = ("25", "40"), bg = "blue")
InEnter.place (x = 200, y = 175)
InEnter.bind ("<Button-1>", InWork)

def InWork (event):
    Status = "Attendance"
    EmpNum = Number.get () # EmpNum is the employee number
    EmpNum_Count = len (EmpNum)
        if EmpNum_Count == 4:
            d_now = str (format (datetime.now (). strftime ("% Y /% m /% d")))
            t_now = str (format (datetime.now (). strftime ("% H:% M:% S")))
            print (EmpNum, d_now, t_now, Status)
root.mainloop ()


Results
Enter anything other than 4 digits

Change the code to the following
Change 1

def OutWork (event):
    Status = "Working away"
    EmpNum = Number.get ()
    EmpNum_Count = len (EmpNum)
    if EmpNum_Count == 4 and type (EmpNum) is int: # number = int
        d_now = str (format (datetime.now (). strftime ("% Y /% m /% d")))
        t_now = str (format (datetime.now (). strftime ("% H:% M:% S")))
        print (EmpNum, d_now, t_now, Status)


Results
No more 4 digits

Change 2

def InWork (event):
    Status = "Attendance"
    EmpNum = Number.get () # EmpNum is the employee number
    EmpNum_Count = len (EmpNum)
    if EmpNum = int (EmpNum): # Here the employee number is set to int
        if EmpNum_Count == 4 and type (EmpNum) is int: #if statement passes 4 digits and only int?
            d_now = str (format (datetime.now (). strftime ("% Y /% m /% d")))
            t_now = str (format (datetime.now (). strftime ("% H:% M:% S")))
            print (EmpNum, d_now, t_now, Status)


Results
Enter if number is 4 digits
The following error when entering the alphabet

Exception in Tkinter callback
Traceback (most recent call last):
  File "C: \ Users \ User \ AppData \ Local \ Programs \ Python \ Python38-32 \ lib \ tkinter \ __ init__.py", line 1883, in __call__
    return self.func (* args)
  File "C: \ python \ tkinter \ tkinter_PG.py", line 24, in InWork
    EmpNum = int (EmpNum)
ValueError: invalid literal for int () with base 10: 'aaaa'
Supplemental information (FW/tool version etc.)

It is python Ver3.8.0

  • Answer # 1

    The tkinter entry has a mechanism calledvalidatecommand(vcmd) that validates the input character string, so it is recommended to use this.

    https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/entry-validation.html

    import tkinter as tk
    root = tk.Tk ()
    # String validation function
    def validation (before_word, after_word):
        return ((after_word.isdecimal ()) and (len (after_word)<= 4)) or (len (after_word) == 0)
    def InWork (event):
        Status = "Attendance"
        EmpNum = Number.get () # EmpNum is the employee number
        EmpNum_Count = len (EmpNum)
        if EmpNum_Count == 4:
            d_now = str (format (datetime.now (). strftime ("% Y /% m /% d")))
            t_now = str (format (datetime.now (). strftime ("% H:% M:% S")))
            print (EmpNum, d_now, t_now, Status)
    #entry set
    sv = tk.StringVar ()
    Number = tk.Entry (font = ("15", "25"), textvariable = sv)
    #% s is the string before change,% P is the string after change
    vcmd = (Number.register (validation), '% s', '% P')
    # Set Validation command ('key' fires event every time a character is entered)
    Number.configure (validate = 'key', vcmd = vcmd)
    Number.place (x = 175, y = 100)
    InEnter = tk.Label (root, text = "attendance", font = ("25", "40"), bg = "blue")
    InEnter.place (x = 200, y = 175)
    InEnter.bind ("", InWork)
    root.mainloop ()