简体   繁体   English

Tkinter.Entry()向上/向下键进行跟踪...有没有办法使用ttk.Entry()?

[英]Tkinter.Entry() passes up/down keys to trace… Is there a way to do this with ttk.Entry()?

Using this code fragment, up and down keys (u"uf700", u"uf701") pass through to the trace: 使用此代码片段,向上和向下键(u“ uf700”,u“ uf701”)传递到跟踪:

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.svFrame.grid()

        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)
        self.svEntry.grid()
..... etc.

If I replace tk.Entry() with ttk.Entry(), the up and down keys do not pass through to the trace. 如果将tk.Entry()替换为ttk.Entry(),则向上和向下键不会传递到跟踪中。 Is there a way to make this work with ttk.Entry()? 有没有办法使它与ttk.Entry()一起工作?

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. 这听起来像你已经找到了在Tkinter的一个错误的MAC,因为箭头键应该通过对变量跟踪传递。 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. 由于这是一个错误,因此没有很好的方法将该错误强加给其他小部件,例如ttk.Entry小部件。

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. 如果您正在寻找一种当用户按下向上或向下键时调用函数的方法,则正确的方法是为这些键向小部件添加绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM