473,384 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,384 software developers and data experts.

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
7 18168
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Schmidt | last post by:
I'm trying to make a custom entry widget, as in the code that follows. There are two problems I'm trying to fix: 1) I would like the widget to behave as myEntry.Escape() does now, except that it...
2
by: Srinath Avadhanula | last post by:
Hello, Sorry to be bringing up what seems to be a somewhat beaten up topic... This is what I wanted to do: Create a _simple_ text editor widget which supports VI(M) style keybindings but...
1
by: phil | last post by:
In a Tkinter entry field (or Pmw entry) how could I eat charactres? Say a certain char is keyed in. Say & I notice in the event handler for <key>. I don't want any more charactres to display or...
2
by: Dustan | last post by:
How do I limit what the user can enter in an Entry Widget? I know I can set it to display '*' to hide a password, but what I want to do is limit the contents to numeric characters. What is the...
10
by: sconeek | last post by:
hi all, i have a textfield where i would like the user to input only Y or N. can somebody tell me how can i restrict the user from entering any other character, number or special character....
21
by: Niu Xiao | last post by:
I see a lot of use in function declarations, such as size_t fread(void* restrict ptr, size_t size, size_t nobj, FILE* restrict fp); but what does the keyword 'restrict' mean? there is no...
0
by: Marek S. | last post by:
Hi, I am new in Python and I have no experience in object-oriented programming. I want to make an easy GUI for my application. What is my problem? I have the data-file like this (file...
5
by: Dakrat | last post by:
Allow me to preface this post by saying that this is my first database project, and while I have learned a lot, any concepts I have learned are hit and miss as I have found new requirements and...
0
by: Leonhard Vogt | last post by:
Hello I have the following problem in Python 2.5 on Windows XP. On Ubuntu I do not see the problem. I have a Tkinter application as in the following example The entry-widget is somehow...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.