I want to limit the input of an Entry widget to 2 characters maximum and to only accept numbers as an input.
I have managed to limit to 2 characters maximum this works, but what i cant get to work is the limiting it to only numbers
When I change my variable to IntVar() there is no longer a character limit enforced.
#Import TKinter module for GUI
import tkinter as tk
from tkinter import ttk
from tkinter import font
from tkinter import messagebox
from tkinter import *
#Create GUI Screen
#Create Notebook GUI Widget
root = Tk()
note = ttk.Notebook(root)
#------------------------------------------------------------------------------
def limit_size(*args): #Limit Entry Field to 2 Characters
value1 = str(Rev_Maj_Value1.get())
if len(value1) >2:Rev_Maj_Value1.set(value1[:2])
def limit_size1(*args):
value2 = str(Rev_Min_Value2.get())
if len(value2) >2:Rev_Min_Value2.set(value2[:2])
def limit_size2(*args):
value3 = str(Rev_Maj_Value3.get())
if len(value3) >2:Rev_Maj_Value3.set(value3[:2])
def limit_size3(*args):
value4 = str(Rev_Min_Value4.get())
if len(value4) >2:Rev_Min_Value4.set(value4[:2])
#------------------------------------------------------------------------------
Tab5 = ttk.Frame(note)
canvas5 = Canvas(Tab5, width=550, height=350)
canvas5.pack()
#Entry Field
Rev_Maj_Value1 = StringVar()
Rev_Maj_Value1.trace('w', limit_size)
Rev_Min_Value2 = StringVar()
Rev_Min_Value2.trace('w', limit_size1)
Rev_Maj_Value3 = StringVar()
Rev_Maj_Value3.trace('w', limit_size2)
Rev_Min_Value4 = StringVar()
Rev_Min_Value4.trace('w', limit_size3)
Rev_Maj1 = Entry(Tab5, justify="center", width=10, textvariable=Rev_Maj_Value1)
Rev_Maj1.insert(0, "00") #Default Value
Rev_Maj1__Window = canvas5.create_window(425, 175, anchor = 'sw', window = Rev_Maj1)
Rev_Min2 = Entry(Tab5, justify="center", width=10, textvariable=Rev_Min_Value2)
Rev_Min2.insert(0, "00") #Default Value
Rev_Min2__Window = canvas5.create_window(425, 225, anchor = 'sw', window = Rev_Min2)
Rev_Maj3 = Entry(Tab5, justify="center", width=10, textvariable=Rev_Maj_Value3)
Rev_Maj3.insert(0, "00") #Default Value
Rev_Maj3__Window = canvas5.create_window(425, 275, anchor = 'sw', window = Rev_Maj3)
Rev_Min4 = Entry(Tab5, justify="center", width=10, textvariable=Rev_Min_Value4)
Rev_Min4.insert(0, "00") #Default Value
Rev_Min4__Window = canvas5.create_window(425, 325, anchor = 'sw', window = Rev_Min4)
#------------------------------------------------------------------------------
#Create Notepad tabs
note.add(Tab5, text = " 5 ")
note.pack()
#------------------------------------------------------------------------------
root.mainloop()
I want to limit the input of an Entry widget to 2 characters maximum and to only accept numbers as an input.
Don't use StringVar()
. Use simply Entry.get()
You would do something like this by using isdigit()
and do same as other methods.
Snippet:
def limit_size(*args): #Limit Entry Field to 2 Characters
if not Rev_Maj1.get().isdigit():
messagebox.showinfo('warning', 'Please enter number')
Rev_Maj1.delete(0, END)
value1 = Rev_Maj1.get()
if len(value1) >2:Rev_Maj_Value1.set(value1[:2])
def limit_size1(*args):
if not Rev_Min2.get().isdigit():
messagebox.showinfo('warning', 'Please enter number')
Rev_Min2.delete(0, END)
value2 = str(Rev_Min_Value2.get())
if len(value2) >2:Rev_Min_Value2.set(value2[:2])
def limit_size2(*args):
if not Rev_Maj3.get().isdigit():
messagebox.showinfo('warning', 'Please enter number')
Rev_Maj3.delete(0, END)
value3 = str(Rev_Maj_Value3.get())
if len(value3) >2:Rev_Maj_Value3.set(value3[:2])
def limit_size3(*args):
if not Rev_Min4.get().isdigit():
messagebox.showinfo('warning', 'Please enter number')
Rev_Min4.delete(0, END)
value4 = str(Rev_Min_Value4.get())
if len(value4) >2:Rev_Min_Value4.set(value4[:2