By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,235 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

how to do this?

P: n/a
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.

#### 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?

Regards,

Justin
Sep 4 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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/

Sep 4 '05 #2

P: n/a
Justin Straube wrote:
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.

#### Code Example ####
from time import localtime
from Tkinter import *
# ignore arguments we don't care for in our application
def set_info(*args): x = len(strVar.get())
infVar.set('Length: %i' % (x))

ROOT = Tk()
strVar = StringVar()
infVar = StringVar()
# register set_info() to listen for changes written to strVar
strVar.trace_variable("w", set_info)
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?


Yes, see above.

Peter

Sep 4 '05 #3

P: n/a
Thanks Steve and Peter,

these two methods are a lot of help. Ill have to play with each in my actual app
to find which works best.

I found more info on trace_variable() at http://effbot.org/tkinterbook/variable.htm

and also some more info on events and bindings at
http://effbot.org/tkinterbook/tkinte...d-bindings.htm

Justin
Sep 4 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.