'how can i get a integar value from python tkinter entry?
so I know how entry in python works and i coded it in some projects. but in one project , there was need to get numeric ( number ) value from user in entry. but it gives the error that entry only supports string data. can you solve this problem ? thanks.
Solution 1:[1]
To add further to this, you can use validation to ensure that you can only write values that evaluate to an integer in your entry:
import tkinter as tk
root = tk.Tk()
root.geometry("200x100")
# Function to validate the Entry text
def validate(entry_value_if_allowed, action):
if int(action) == 0: # User tried to delete a character/s, allow
return True
try:
int(entry_value_if_allowed) # Entry input will be an integer, allow
return True
except ValueError: # Entry input won't be an integer, don't allow
return False
# Registering the validation function, passing the correct callback substitution codes
foo = (root.register(validate), '%P', '%d')
my_entry = tk.Entry(root, width=20, validate="key", validatecommand=foo)
# Misc
text = tk.Label(root, text="This Entry ^ will only allow integers")
my_entry.pack()
text.pack()
root.mainloop()
Some more info on Entry validation can be found here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jezza_99 |
