简体   繁体   English

不理解 python 错误使用 tkinter 回调 lambda

[英]Not understanding python error using tkinter callback lambda

I am using a lambda in a tkinter callback.我在 tkinter 回调中使用 lambda。 It produces the correct result but throws an error.它会产生正确的结果,但会引发错误。 An error I do not understand and have not been able to isolate and eliminate.一个我不理解且无法隔离和消除的错误。 The goal is to get two user inputs via Entry widgets, the second one has a trace on it.目标是通过 Entry 小部件获得两个用户输入,第二个有跟踪。 The trace takes the two Entry widgets and calculates the mark up (100*((var1-var2)/var2)) returning that value.跟踪采用两个 Entry 小部件并计算返回该值的标记 (100*((var1-var2)/var2))。 The Form shows the mark up correctly but I also get an error and I want to eliminate that error.表单正确显示标记,但我也收到错误,我想消除该错误。 The error indicates it is is expecting a DoubleVar and is getting ''.该错误表明它需要一个 DoubleVar 并且正在获取 ''。 All three variables are DoubleVar (var1, var2, mu).所有三个变量都是 DoubleVar (var1, var2, mu)。 I am down to it having something to do with self but I do not understand enough to be able to get to the finish line.我认为这与自我有关,但我对能够到达终点线的了解还不够。 Any suggestions or pointers would be greatly appreciated.任何建议或指示将不胜感激。

from tkinter import *

def var2_callback(var1, var2, mu):
    #print('var1: %d' % var1.get())
    #print('var2: %d' % var2.get())
    #print('mu: %d' % mu.get())
    markup = var1.get() * var2.get()
    mu.set(markup)
    #print('times: %d' % mu.get())
    return()


def do_something():
    mu = DoubleVar(value=0)
    root.grid_columnconfigure((0,1,2,3,4,5), weight=1)
    root.grid_rowconfigure((0,1,2,3,4,5,6,7,8,9,10), weight=1)
    cf = Frame(root)
    cf.grid(row=0, column=2)

    lbl1 = Label(cf, textvariable=mu)
    lbl1.grid(row=0, column=2)

    var1 = DoubleVar(value=0)
    e1 = Entry(cf, textvariable=var1)
    e1.grid(row=2, column=2)

    var2 = DoubleVar(value=0)
    var2.trace('w',lambda name, index, mode, var2=var2: var2_callback(var1,var2,mu))
    e2 = Entry(cf, textvariable=var2)
    e2.grid(row=3, column=2)


root = Tk()
root.geometry("1000x600+0+0")
do_something()
root.mainloop()

The error I get is:我得到的错误是:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.6/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "for_entry.py", line 28, in <lambda>
var2.trace('w',lambda name, index, mode, mu=mu: var2_callback(var1,var2,mu))
  File "for_entry.py", line 7, in var2_callback
markup = var1.get() * var2.get()
  File "/usr/lib/python3.6/tkinter/__init__.py", line 529, in get
    return self._tk.getdouble(self._tk.globalgetvar(self._name)_tkinter.TclError: expected floating-point number but got ""

I am wanting to eliminate the error.我想消除错误。 I know if I comment out the mu.set line I can eliminate the error (and the mu calc...).我知道如果我注释掉 mu.set 行,我可以消除错误(以及 mu calc...)。 I do not understand what rule I am breaking in passing back the mu result.我不明白我在传回 mu 结果时违反了什么规则。

*** UPDATE *** 更新

The problem is that some how the trace callback is called twice.问题是跟踪回调如何被调用两次。 The first time var2 is empty even with var2 being set to 0. The second time through it works.即使 var2 设置为 0,第一次 var2 也是空的。第二次通过它可以工作。 Trying to figure out why var2 is empty on the first time through.试图弄清楚为什么 var2 第一次是空的。 I have verified this by adding print statements at the beginning of the callback function.我已经通过在回调 function 的开头添加打印语句来验证这一点。 I have tried to avoid the problem by setting var2 (var2.set(0)) at the beginning of the callback function - that avoids the problem but obviously prevents the function from preforming the calculation.我试图通过在回调 function 的开头设置 var2 (var2.set(0)) 来避免该问题 - 这可以避免该问题,但显然会阻止 function 执行计算。 I can not figure out how to use an 'if' statement to test var2.我不知道如何使用“if”语句来测试 var2。 'if var2.get() == None: or if var2.get().= None or if var2.get() > 0' all result in the same error that var2 is '' and was expected to be a DoubleVar? 'if var2.get() == None: or if var2.get().= None or if var2.get() > 0' 都导致与 var2 为 '' 且预期为 DoubleVar 相同的错误? Is this problem caused by using a function and not a Class?这个问题是使用 function 而不是 Class 引起的吗?

Thank you acw1668 for pointing me in the direction of being able to mask the issue using try/except.感谢 acw1668 指出我可以使用 try/except 来掩盖问题。 That allowed me to hide the error on the trace.这让我可以隐藏跟踪上的错误。 The trace was firing on the decimal (as in.5) and that created an illegal calculation (. times something makes no sense).跟踪在小数点上触发(如 in.5),这产生了非法计算(.times 什么没有意义)。 That in turn led me to using a bind (bind on the Tab key, UP arrow, down arrow) on the Event not the variable and that worked out much better.这反过来又导致我在事件而不是变量上使用绑定(在 Tab 键、向上箭头、向下箭头上绑定),并且效果要好得多。

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

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