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

Discussion in 'Python' started by Michael Onfrek, May 29, 2005.

  1. 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
    Michael Onfrek, May 29, 2005
    #1
    1. Advertising

  2. Michael Onfrek

    flupke Guest

    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
    flupke, May 30, 2005
    #2
    1. Advertising

  3. I'm using tkinter
    Michael Onfrek, May 30, 2005
    #3
  4. Michael Onfrek

    Peter Otten Guest

    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
    Peter Otten, Jun 2, 2005
    #4
  5. Michael Onfrek, Jun 2, 2005
    #5
  6. Michael Onfrek

    Peter Otten Guest

    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
    Peter Otten, Jun 2, 2005
    #6
  7. Michael Onfrek

    VK Guest

    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.
    VK, Jun 2, 2005
    #7
  8. Ideed, good idea!
    Michael Onfrek, Jun 2, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. puzzlecracker
    Replies:
    3
    Views:
    463
    Greg R. Broderick
    Nov 12, 2006
  2. Joshua Mostafa
    Replies:
    4
    Views:
    2,274
    Joshua Mostafa
    May 11, 2007
  3. Athen
    Replies:
    2
    Views:
    191
    Athen
    Aug 11, 2004
  4. Replies:
    0
    Views:
    322
  5. Frederic Rentsch
    Replies:
    0
    Views:
    224
    Frederic Rentsch
    Jul 8, 2012
Loading...

Share This Page