Python tkinter 跟踪错误 [英] Python tkinter trace error
问题描述
我正在尝试为我的代码编写 GUI.我的计划是使用tkinter的StringVar
、DoubleVar
等实时监控我的输入.所以我发现了 DoubleVar.trace('w', callback)
函数.但是,每次我进行更改时都会出现异常:
I'm trying to write a GUI for my code. My plan is to use tkinter's StringVar
, DoubleVar
, etc. to monitor my input in real time. So I found out the DoubleVar.trace('w', callback)
function. However, every time I make the change I get an exception:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Anaconda2\lib\lib-tk\Tkinter.py", line 1542, in __call__
return self.func(*args)
TypeError: 'NoneType' object is not callable
我不知道出了什么问题.我正在使用 python 2.7我的代码如下:
I have no idea what's going wrong. I'm using python 2.7 My code is as follows:
from Tkinter import *
class test(Frame):
def __init__(self,master):
Frame.__init__(self,master=None)
self.main_frame = Frame(master);
self.main_frame.pack()
self.testvar = DoubleVar()
self.slider_testvar = Scale(self.main_frame,variable = self.testvar,from_ = 0.2, to = 900, resolution = 0.1, orient=HORIZONTAL,length = 300)
self.slider_testvar.grid(row = 0, column = 0, columnspan = 5)
self.testvar.trace('w',self.testfun())
def testfun(self):
print(self.testvar.get())
root = Tk()
root.geometry("1024x768")
app = test(master = root)
root.mainloop()
推荐答案
考虑这行代码:
self.testvar.trace('w',self.testfun())
这和这个完全一样:
result = self.testfun()
self.testvar.trace('w', result)
由于函数返回 None
,跟踪将尝试调用 None
,因此您得到 'NoneType' 对象不可调用
Since the function returns None
, the trace is going to try to call None
, and thus you get 'NoneType' object is not callable
trace
方法需要一个可调用.也就是说,对函数的引用.您需要将该行更改为以下内容(注意末尾缺少的 ()
):
The trace
method requires a callable. That is, a reference to a function. You need to change that line to be the following (notice the missing ()
at the end):
self.testvar.trace('w',self.testfun)
此外,您需要修改 testfun
以采用跟踪机制自动传递的参数.有关详细信息,请参阅 Tkinter 变量跟踪方法回调的参数是什么?
Also, you need to modify testfun
to take arguments that are automatically passed by the tracing mechanism. For more information see What are the arguments to Tkinter variable trace method callbacks?
这篇关于Python tkinter 跟踪错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!