I want to know how can I make An Entry Accept Decimal Numbers ONLY
python tkinter programming
CodePudding user response:
You can use isdecimal() function to check if its decimal or not. https://www.geeksforgeeks.org/python-string-isdecimal-method/#:~:text=Python String isdecimal() function,decimal, else it returns False.
CodePudding user response:
Here's an example of basic input validation on an Entry
widget
import tkinter as tk
from tkinter import ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
# register a validation function using the '%P' substitution code
self.vcmd = (self.register(self.validate), '%P')
self.entry = ttk.Entry( # this is the entry you want to validate
self,
validate='key', # trigger validation on a keypress
validatecommand=self.vcmd # set registration
)
self.entry.pack()
def validate(self, P): # P is the text being entered at the time of callback
# this function should return True if the input is valid, or False otherwise
return P.isdecimal() or P == '' # include "or P == ''" to allow empty values
if __name__ == '__main__':
app = App()
app.mainloop() # run the app
There's a lot more information about this available here. I recommend reading through that tutorial.