how to do this?

J

Justin Straube

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
 
S

Steve Holden

Justin said:
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
 
P

Peter Otten

Justin said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,318
Members
48,002
Latest member
EttaPfeffe

Latest Threads

Top