Help with <key> event in python Entry widget

By : Owen

I'm writing some code in python and I'm having trouble when trying to retrieve content of an Entry widget. The thing is: I want to limit the characters that can be typed, so I'm trying to clear the Entry widget when I reach the specific number of characters (2 in this case), but it looks like I always miss the last typed character. I added the lost character in a print to show.

Here's the sample code:

from Tkinter import *
class sampleFrame:
    def __init__(self, master):
        self.__frame = Frame(master)
    def get_frame(self):
        return self.__frame

class sampleClass:
    def __init__(self, master):
        self.__aLabel = Label(master,text="aLabel", width=10)
        self.__aEntry = Entry (master, width=2)
        self.__aEntry.bind('<Key>', lambda event: self.callback(event, self.__aEntry))

    def callback(self, event, widgetName):
        self.__value = widgetName.get()+event.char
        print self.__value
        if len(self.__value)>2:

root = Tk()
aSampleFrame = sampleFrame(root)
aSampleClass = sampleClass(aSampleFrame.get_frame())

Any help will be much appreciated!

Thanks in advance

By : Owen


To be a bit more specific, Tk widgets have what are called "bindtags". When an event is processed, each bindtag on the widget is considered in order to see if it has a binding. A widget by default will have as its bindtags the widget, the widget class, the root widget, and "all". Thus, bindings to the widget will occur before the default bindings. Once your binding has been processed you can prevent any further bindtags from being considered by returning a "break".

The ramifications are this: if you make a binding on the widget, the class, root window and "all" bindings may fire as well. In addition, any binding you attach to the widget fires before the class binding which is where the default behavior (eg: the insertion of a character) happens. It is important to be aware of that in situations where you may want to handle the event after the default behavior rather than before.

My preference is to display the most critical components of the URL. This is the file being requested and the domain of the request are what I consider critical, the intermediate path and the query string are things I consider non-critical.

So if you had, I would truncate it as

Of course, there are scenarios where this wont work. Take the URL of this page:

In this case, I would truncate the last portion of the URL to a reasonable number of characters (preferably breaking on a non-alpha), such as:

By : coderGeek

bmdhacks is right that if you're using MySQL or PostgreSQL it doesn't matter as much since connections are pretty cheap. But no matter your database you will have speed gains by using persistent connections.

But if you do decide to go with persistent connections you will need to worry about connection timeouts. These will happen at any time during the life of your program depending on your server settings and the amount of traffic you're getting. ping() is your friend here. And if you need more help, look at how Apache::DBI does it.

By : mpeters

This video can help you solving your question :)
By: admin