Tkinter: spinbox command and percent substitutions

T

Tony Eva

The spinbox widget in Tk 8.4 has an option to allow a command
to be specified that is invoked when either the up or down buttons
of the spinbox are pressed, as in:

...
self.sb = Tkinter.Spinbox( self, command = self.sbcmd )
...

def sbcmd( self, *args ):
print args

However, it seems that no parameters are passed to the command (the
print statement outputs "()"), and so it is not possible to tell
which button was pressed to invoke the command. The Tk man pages
say that the "%d" substitution will be either "up" or "down", but
I cannot see how to get at these substitutions.

The same applies to other command options, e.g. validatecommand,
where the Tk man page implies that percent substitutions are used
to pass info into the command. How does one do this?

I'd like to use the command option to implement a spinbox to select
from a large range of values that are not regular (so the 'to', 'from',
'step' and 'value' options can't be used or are very clunky.)

Can anyone help? (This is Python 2.3, Windows install, if it's
relevant.)
 
J

Jeff Epler

You'll have to arrange for the Tk -command to contain %x and %y
substitutions, and similarly for the -validatecommand.

The sequence would look something like this:
tkcmd = self.register(self.sbcmd)
self.sb = Tkinter.Spinbox(self, command=(tkcmd, "%x", "%y"))
...
def sbcmd(self, x, y):
print x, y
I don't have 8.4, so I can't test this out exactly.

Jeff
 

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
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top