Take the 2-minute tour ×

Using this code fragment, up and down keys (u"uf700", u"uf701") pass through to the trace:

import Tkinter as tk
import ttk

def tracesv(var):
    print var

class foo()
    def __init__(self, parent=None):
        self.win = tk.Toplevel()
        self.svFrame = ttk.Frame(self.win)

        self.sv = tk.StringVar()
        self.sv.trace('w', lambda nm, idx, mode, var=self.sv: tracesv(var))
        self.svEntry = tk.Entry(self.svFrame, textvariable=self.sv)
..... etc.

If I replace tk.Entry() with ttk.Entry(), the up and down keys do not pass through to the trace. Is there a way to make this work with ttk.Entry()?

share|improve this question
Your code is incorrect as posted; are you certain the above code reproduces the problem? I tweaked the code enough to make it work and couldn't duplicate your results. What you describe is not proper Tkinter behavior -- up and down arrows shouldn't cause the trace to fire in either widget. What platform are you running on? –  Bryan Oakley Sep 21 '12 at 20:14
Yes, I suppose "..... etc." might show up as an error. I said it was a fragment and it's clear it wouldn't run as-is. I'm running on OSX and the up and down keys are passed through; I just tested it on Windows and they don't trigger the trace. The fact that it functions differently on different platforms truly sucks. Be that as it may, is there a way to trap the up and down key and alter the textvariable associated with an Entry widget that is proper behavior and likely to work cross-platform? –  LMO Sep 22 '12 at 5:24
It sounds like you've found a bug in Tkinter. Yes, you can trap up/down keys by creating bindings on them. As phrased, your original question is specifically about how to track them via trace, so try rephrasing your question or asking another question. –  Bryan Oakley Sep 22 '12 at 12:52

1 Answer 1

It sounds like you've found a bug in Tkinter on the mac because arrow keys should not be passed through to the variable trace. The variable trace is designed to be called when the variable is modified. Unless the up and down arrows modify the value then they shouldn't cause the trace to fire.

Since this is a bug, there's no good way to force the bug upon other widgets such as the ttk.Entry widget.

If you're looking for a way to have a function called when the user presses the up or down keys, the proper way is to add bindings to the widget for those keys.

share|improve this answer

Your Answer


