467,207 Members | 1,340 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,207 developers. It's quick & easy.

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

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

Jul 19 '05 #1
  • viewed: 13894
Share:
7 Replies
Michael Onfrek wrote:
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
Jul 19 '05 #2
I'm using tkinter

Jul 19 '05 #3
Michael Onfrek wrote:
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

Jul 19 '05 #4
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? :)

Thanks for help!

Jul 19 '05 #5
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? :)

import Tkinter as tk
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.
var = tk.StringVar()
entry = tk.Entry(root, textvariable=var)
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.
var.trace_variable("w", on_write)


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

Jul 19 '05 #6
VK
Peter Otten wrote:
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? :)


import Tkinter as tk

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.

var = tk.StringVar()
entry = tk.Entry(root, textvariable=var)

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.

var.trace_variable("w", on_write)

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.
Jul 19 '05 #7
Ideed, good idea!

Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Phil Schmidt | last post: by
2 posts views Thread by Srinath Avadhanula | last post: by
1 post views Thread by phil | last post: by
10 posts views Thread by sconeek@gmail.com | last post: by
21 posts views Thread by Niu Xiao | last post: by
reply views Thread by Marek S. | last post: by
reply views Thread by Leonhard Vogt | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.