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?
An entry other than a number is entered in an entry where you only want to enter numbers
Applicable source codeimport 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
Related articles
- python - i want to exclude numbers from csv created with pandas
- python 3x - button execution action in tkinter when executing code
- python 3x - change font size for python3 tkinter entry
- python 3x - tkinter i want to write code concisely
- i want to output the entry result of tkinter with python
- python - i want to convert the characters obtained from the txt file to numbers
- python - pltshow does not run unless you close the tkinter window
- python 3x - python3 tkinter treeview data update
- python - sum of numbers by inputting a character string
- python - kivy: to refer to other widget elements
- python 3x - python3 tkinter no response when closing window
- python - [tkinter] multiple selectable combobox
- python 3x - is there a way to install plotlypy in python other than pip?
- python amicable numbers
- python - i want to implement scrolled text in tkinter
- python 3x - how to extract row and column numbers with specific values in a dataframe table
- python - i want to buy a tkinter button layout
- python 3x - i want to extract data from a python3 tkinter list
- [python] [tkinter] i want to get multiple checkbox values
- python - how to make a table in tkinter
Related questions
- how in python tkinter via PhotoImage to find out the size of the image?
- python : Creating a list of buttons with different click handler parameters
- python : Label update, in a text quest
- python : Opening a file in a new tab, tkinter
- python : Can't figure out how to implement with tkinter
- python : I need to paint the color of the checkerboard like on a chessboard
- python : How to return variables to the same value that was at the beginning of the program
- python : Why is the interface blurring in tkinter?
- Is there a site that displays the exact position of python Tkinter widgets on a grid? Otherwise, it is somehow inconvenient to s
- python : How can I change the value of all objects in a class?
The tkinter entry has a mechanism called
validatecommand
(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