How to restrict lenght of entry widget to certain number of character

M

Michael Onfrek

Hi!
I'm playing with entry again and trying to restrict length of entry
widget to certain number of character, so users cannot enter more
character into it. Any ideas?
Reg. Michael Onfrek
 
F

flupke

Michael said:
Hi!
I'm playing with entry again and trying to restrict length of entry
widget to certain number of character, so users cannot enter more
character into it. Any ideas?
Reg. Michael Onfrek

What widget set are you talking about, wxPython pygtk, tkinter?

In wxPython:
<ctrl>.SetMaxLength(length)

Benedict
 
P

Peter Otten

Michael said:
I'm playing with entry again and trying to restrict length of entry
widget to certain number of character, so users cannot enter more
character into it. Any ideas?

import Tkinter as tk
root = tk.Tk()
var = tk.StringVar()

max_len = 5
def on_write(*args):
s = var.get()
if len(s) > max_len:
var.set(s[:max_len])

var.trace_variable("w", on_write)
entry = tk.Entry(root, textvariable=var)
entry.pack()
root.mainloop()

Not very elegant, but better than nothing.

Peter
 
P

Peter Otten

Michael said:
import Tkinter as tk

Hi! Can you explain what line above mean?

I also found : http://effbot.org/zone/tkinter-entry-validate.htm

It works for me, but I not really understand how? :)

Make objects defined in Tkinter available under the tk prefix.
E. g. to access an Entry you can do 'tk.Entry'. Had you imported it
'import Tkinter' you would have to do 'Tkinter.Entry' instead. So you
are saving a few keystrokes. Doing 'from Tkinter import *' saves you still
more keystrokes but is considered bad style except for demonstration
purposes.

Create a StringVar and connect it to the Entry widget. Any changes the user
makes in the Entry are reflected in the StringVar's value which can be
accessed with its get() method.
max_len = 5
def on_write(*args):
    s = var.get()
    if len(s) > max_len:
        var.set(s[:max_len])

Define a function that doesn't care about the arguments passed to it. It
reads the current value of the StringVar 'var' and, if necessary, trims it
to 'max_len_' characters.

Tell the StringVar to call the function on_write() every time its value is
changed. So every time the user edits the data in the Entry, in turn the
Entry changes the data of the StringVar, which calls the on_write()
function which may or may not change the StringVar -- and that change is
reflected in what the Entry displays. This smells like an endless loop, but
so far we seem to be lucky...

If you look again at Fredrik Lundh's ValidatingEntry, you will find all the
elements explained above packed nicely into one class, with the extra
refinement that he keeps another copy of the value which is used to restore
the old state when the new value is found to be invalid.

Peter
 
V

VK

Peter said:
Michael Onfrek wrote:

import Tkinter as tk

Hi! Can you explain what line above mean?

I also found : http://effbot.org/zone/tkinter-entry-validate.htm

It works for me, but I not really understand how? :)


Make objects defined in Tkinter available under the tk prefix.
E. g. to access an Entry you can do 'tk.Entry'. Had you imported it
'import Tkinter' you would have to do 'Tkinter.Entry' instead. So you
are saving a few keystrokes. Doing 'from Tkinter import *' saves you still
more keystrokes but is considered bad style except for demonstration
purposes.



Create a StringVar and connect it to the Entry widget. Any changes the user
makes in the Entry are reflected in the StringVar's value which can be
accessed with its get() method.

max_len = 5
def on_write(*args):
s = var.get()
if len(s) > max_len:
var.set(s[:max_len])


Define a function that doesn't care about the arguments passed to it. It
reads the current value of the StringVar 'var' and, if necessary, trims it
to 'max_len_' characters.



Tell the StringVar to call the function on_write() every time its value is
changed. So every time the user edits the data in the Entry, in turn the
Entry changes the data of the StringVar, which calls the on_write()
function which may or may not change the StringVar -- and that change is
reflected in what the Entry displays. This smells like an endless loop, but
so far we seem to be lucky...

If you look again at Fredrik Lundh's ValidatingEntry, you will find all the
elements explained above packed nicely into one class, with the extra
refinement that he keeps another copy of the value which is used to restore
the old state when the new value is found to be invalid.

Peter

Thank you, man! You should write an tutorial to Tkinter or something
like that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,240
Messages
2,571,211
Members
47,847
Latest member
ShavonneLa

Latest Threads

Top