I am currently making an input form and need an entry box to be limited to 2 characters. How do I do this?
#Date+time day entry boxes
day_entry1=Entry(List1, bg="#282B2B", fg="white", width=2)
day_entry1.place(x=77, y=58)
I am currently making an input form and need an entry box to be limited to 2 characters. How do I do this?
#Date+time day entry boxes
day_entry1=Entry(List1, bg="#282B2B", fg="white", width=2)
day_entry1.place(x=77, y=58)
I suppose you are using Tkinter to create a graphical interface. This being the case the solution is to use StringVar(). This is like a string variable but they can call a fuction when they are changed. So this would be an example:
def limitSizeDay(*args):
value = dayValue.get()
if len(value) > 2: dayValue.set(value[:2])
dayValue = StringVar()
dayValue.trace('w', limitSizeDay)
day_entry1=Entry(List1, bg="#282B2B", fg="white", width=2, textvariable=dayValue)
day_entry1.place(x=77, y=58)
So basically you create a function that reads and checks the length of the day value. This function is called limitSizeDay. Then you define a StringVar instance called dayValue. You 'bind' (call trace method) a function to dayValue which fires up when the contents are changed. And lastly when you create the Entry widget set textvariable=dayValue. This will bind the dayValue to the widget which basically makes dayValue to store any content written in the entry.
Hope this solves it and explains some concepts about StringVar class.