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

Using bound variables in Tkinter events

P: n/a
I'm coming to Tkinter from Tcl/Tk. In Tcl I can get a variable in an event
using the %<X> substitution mechanism. For example, I can set up a command
like:

entry .e -validate 1 -vcmd "checkkey %d"

knowing that the '%d' will be replaced by something useful - whether the
entry widget has recieved an insert or deletion in this case. The checkkey
procedure will recieve "insert", "delete" or whatever as its first
parameter.

What is the Tkinter way of getting that %d value?
Jul 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Derek Fountain wrote:
I'm coming to Tkinter from Tcl/Tk. In Tcl I can get a variable in an event
using the %<X> substitution mechanism. For example, I can set up a command
like:

entry .e -validate 1 -vcmd "checkkey %d"

knowing that the '%d' will be replaced by something useful - whether the
entry widget has recieved an insert or deletion in this case. The checkkey
procedure will recieve "insert", "delete" or whatever as its first
parameter.

What is the Tkinter way of getting that %d value?


Derek,
f = Entry()
....
v = StringVar()
v.set("test")
f["textvariable"] = v

print v.get()

Jul 18 '05 #2

P: n/a
Derek Fountain wrote:
I'm coming to Tkinter from Tcl/Tk. In Tcl I can get a variable in an event
using the %<X> substitution mechanism. For example, I can set up a command
like:

entry .e -validate 1 -vcmd "checkkey %d"

knowing that the '%d' will be replaced by something useful - whether the
entry widget has recieved an insert or deletion in this case. The checkkey
procedure will recieve "insert", "delete" or whatever as its first
parameter.

What is the Tkinter way of getting that %d value?


import Tkinter as tk
root = tk.Tk()

def check(action):
print {"1":"inserting", "0": "deleting"}.get(action, "should never
happen")
return True
checkId = root.register(check)

entry = tk.Entry(root, validate="key", validatecommand=checkId + " %d")
entry.pack()
root.mainloop()

The above was found by trial and error, so no warranties :-)
You could use check() directly, like

entry = tk.Entry(root, validate="key", validatecommand=check)

but then it will be called without parameters.

Peter
Jul 18 '05 #3

P: n/a

"Derek Fountain" <de*****@example.com> wrote in message
news:40***********************@freenews.iinet.net. au...
I'm coming to Tkinter from Tcl/Tk. In Tcl I can get a variable in an event
using the %<X> substitution mechanism. For example, I can set up a command
like:

entry .e -validate 1 -vcmd "checkkey %d"

knowing that the '%d' will be replaced by something useful - whether the
entry widget has recieved an insert or deletion in this case. The checkkey
procedure will recieve "insert", "delete" or whatever as its first
parameter.

What is the Tkinter way of getting that %d value?


It's in the event object that's passed as the (only) parameter
to your event handler. Everything you ever wanted to know
about the event is an attribute.

There are a number of very good references to Tkinter on
the first page of the Python Library Reference section on
Tkinter. (That's section 16 in the Python 2.3 reference)

John Roth
Jul 18 '05 #4

P: n/a
wes weston wrote:
Derek Fountain wrote:
I'm coming to Tkinter from Tcl/Tk. In Tcl I can get a variable in an
event using the %<X> substitution mechanism. For example, I can set up a
command like:

entry .e -validate 1 -vcmd "checkkey %d"

knowing that the '%d' will be replaced by something useful - whether the
entry widget has recieved an insert or deletion in this case. The
checkkey procedure will recieve "insert", "delete" or whatever as its
first parameter.

What is the Tkinter way of getting that %d value?


Derek,
f = Entry()
...
v = StringVar()
v.set("test")
f["textvariable"] = v

print v.get()


Thanks, but that's not what I was after! I want to know the type of action
I've received - the %d binding for the validatecommand.
Jul 18 '05 #5

P: n/a
>> I'm coming to Tkinter from Tcl/Tk. In Tcl I can get a variable in an
event using the %<X> substitution mechanism. For example, I can set up a
command like:

entry .e -validate 1 -vcmd "checkkey %d"

knowing that the '%d' will be replaced by something useful - whether the
entry widget has recieved an insert or deletion in this case. The
checkkey procedure will recieve "insert", "delete" or whatever as its
first parameter.

What is the Tkinter way of getting that %d value?
It's in the event object that's passed as the (only) parameter
to your event handler. Everything you ever wanted to know
about the event is an attribute.


The validate command doesn't get an event.
There are a number of very good references to Tkinter on
the first page of the Python Library Reference section on
Tkinter. (That's section 16 in the Python 2.3 reference)


Indeed, but I couldn't find anything which answered this question.
Jul 18 '05 #6

P: n/a
> import Tkinter as tk
root = tk.Tk()

def check(action):
print {"1":"inserting", "0": "deleting"}.get(action, "should never
happen")
return True
checkId = root.register(check)

entry = tk.Entry(root, validate="key", validatecommand=checkId + " %d")
entry.pack()
root.mainloop()

The above was found by trial and error, so no warranties :-)
Awesome, thank you. I don't understand what's going on though. Can you
explain a bit? I don't understand the mechanism which is expanding the %d.
Is there some magic in the tk.Entry constructor which does it, or is that
root.register() doing something clever?

You could use check() directly, like

entry = tk.Entry(root, validate="key", validatecommand=check)

but then it will be called without parameters.


That's as far as I'd got and I was beginning to think it was impossible to
get the parameters. At least I now know it can be done, even if I don't
understand how!
Jul 18 '05 #7

P: n/a
Derek Fountain wrote:
import Tkinter as tk
root = tk.Tk()

def check(action):
print {"1":"inserting", "0": "deleting"}.get(action, "should never
happen")
return True
checkId = root.register(check)

entry = tk.Entry(root, validate="key", validatecommand=checkId + " %d")
entry.pack()
root.mainloop()

The above was found by trial and error, so no warranties :-)


Awesome, thank you. I don't understand what's going on though. Can you
explain a bit? I don't understand the mechanism which is expanding the %d.
Is there some magic in the tk.Entry constructor which does it, or is that
root.register() doing something clever?


register() wraps a python function into a tcl function and returns the new
tcl function's name (can they start with a number?). If you provide a
string instead of a python function reference as a callback for (e. g.)
validatecommand, it is passed unaltered to tcl.
That's all there is to it - at least so my theory goes.

Peter

Jul 18 '05 #8

P: n/a
In article <c4*************@news.t-online.com>,
Peter Otten <__*******@web.de> wrote:
Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.