how to do this?

Steve Holden steve at holdenweb.com
Sun Sep 4 01:45:58 EDT 2005


Justin Straube wrote:
> Greetings Pythonistas.
> Im looking for a way to write this but not sure where or how to begin.
> As the user enters or removes characters into/from sEnt I would like
> for set_info() to set infVar with the correct value. The same as how
> IDLE shows the line and column in the lower right corner.
> 
First of all, it might have been better to provide a more meaningful 
title like "Update display as text entry changes", but not to worry - 
this is a matter of experience.

> #### Code Example ####
> from time import localtime
> from Tkinter import *
> 
> def set_info():
>      x = len(strVar.get())
>      infVar.set('Length: %i' % (x))
> 
> ROOT = Tk()
> strVar = StringVar()
> infVar = StringVar()
> 
> sLab = Label(ROOT, text='String')
> sLab.grid(row=0, column=0)
> sEnt = Entry(ROOT, textvariable=strVar, width=15)
> sEnt.grid(row=0, column=1, columnspan=2)
> qBut = Button(ROOT, text='Quit', command=ROOT.quit)
> qBut.grid(row=1, column=2, pady=2, padx=2, sticky=E)
> iLab = Label(ROOT, textvariable=infVar, width=21,
>               relief=SUNKEN, anchor=W)
> iLab.grid(row=2, column=0, columnspan=3)
> 
> set_info() # example to show what will be displayed.
> ROOT.mainloop()
> #### End Example ####
> 
> Can anyone point me in the right direction for how to do this?
> 
The key here is to bind keystroke events to the routine to change the 
display. The following program uses your set_info() function to update 
the display every time a key is released (I chose this event because by 
the time it is raised the keystroke has been processed by the Entry widget.

I don;t guarantee this code will work across several Entry widgets 
without your keeping track of which one has focus when the event is 
raised (it's late, and I'm about to turn in for the night), but at least 
it will give you something to play with.

You'll note that set_info() has acquired an argument - Tkinter provides 
an event as an argument when a callback is called. So the manual call 
gets a bogus event of "None" just to avoid exceptions. Hope this gets 
you started.

#### Code Example ####
from time import localtime
from Tkinter import *

def set_info(event):
      x = len(strVar.get())
      infVar.set('Length: %i' % (x))

ROOT = Tk()
strVar = StringVar()
infVar = StringVar()

sLab = Label(ROOT, text='String')
sLab.grid(row=0, column=0)
sEnt = Entry(ROOT, textvariable=strVar, width=15)
sEnt.grid(row=0, column=1, columnspan=2)
sEnt.bind("<KeyRelease>", set_info)
qBut = Button(ROOT, text='Quit', command=ROOT.quit)
qBut.grid(row=1, column=2, pady=2, padx=2, sticky=E)
iLab = Label(ROOT, textvariable=infVar, width=21,
               relief=SUNKEN, anchor=W)
iLab.grid(row=2, column=0, columnspan=3)

set_info(None) # example to show what will be displayed.
ROOT.mainloop()
#### End Example ####

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC             http://www.holdenweb.com/




More information about the Python-list mailing list