在 Spinbox 上使用 validatecommand 时出现 AttributeError
AttributeError when using validatecommand on a Spinbox
我正在测试编写一个脚本来验证旋转框以在更大的系统上实现它,但我正在为 python 苦苦挣扎,因为它警告说我的 [=12] 上没有旋转框属性=] class。检查我的代码:
from Tkinter import *
class Window:
def __init__(self, toplevel):
self.spinbox = Spinbox(toplevel, from_ = 0, to = 10,
validate = 'all', validatecommand = self.validate)
self.spinbox.pack()
def validate(self):
print self.spinbox.get()
root = Tk()
Window(root)
root.mainloop()
这是它给出的错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__
return self.func(*args)
File "D:\DOCS\FILIPE\PROGRAMMING\PYTHON\Tkinter sandbox - spinbox validate.
py", line 13, in validate
print self.spinbox.get()
AttributeError: Window instance has no attribute 'spinbox'
谁能帮我解决这个问题?
如果您在代码中添加一些 print
语句:
class Window:
def __init__(self, toplevel):
print "A start", self, self.__dict__
self.spinbox = Spinbox(toplevel, from_ = 0, to = 10,
validate = 'all', validatecommand = self.validate)
self.spinbox.pack()
print "A end", self, self.__dict__
def validate(self):
print "B", self, self.__dict__
print self.spinbox.get()
#...
你得到输出:
A start <__main__.Window instance at 0x7fe4f8deec20> {}
B <__main__.Window instance at 0x7fe4f8deec20> {}
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1532, in __call__
return self.func(*args)
File "t.py", line 14, in validate
print self.spinbox.get()
AttributeError: Window instance has no attribute 'spinbox'
A end <__main__.Window instance at 0x7fe4f8deec20> {'spinbox': <Tkinter.Spinbox instance at 0x7fe4f8e0da28>}
这意味着在构造函数内部调用了validate函数来验证初始值,而此时self.spinbox
还没有被设置。您将需要检查您是否仍在构建或延迟设置 validatecommand
,如下所示:
self.spinbox = Spinbox(toplevel, from_ = 0, to = 10,
validate = 'all')
self.spinbox["validatecommand"] = self.validate
self.spinbox.pack()
我设法使用文本变量和跟踪方法解决了这个问题
#Validates
self.countstringvar = StringVar()
self.countstringvar.trace("w", lambda name, index, mode,
sv = self.countstringvar: self.noLettersValidate(sv, self.count))
self.starsstringvar = StringVar()
self.starsstringvar.trace("w", lambda name, index, mode,
sv = self.starsstringvar: self.noLettersValidate(sv, self.stars))
self.scorestringvar = StringVar()
self.scorestringvar.trace("w", lambda name, index, mode,
sv = self.scorestringvar: self.noLettersValidate(sv, self.score))
self.count['textvariable'] = self.countstringvar
self.stars['textvariable'] = self.starsstringvar
self.score['textvariable'] = self.scorestringvar
def removeLetters(self, s):
a = []
for i in s:
if i.isdigit():
a.append(i)
return ''.join(a)
def noLettersValidate(self, sv, w):
w.text(self.removeLetters(sv.get()))
感谢大家的投稿!
我正在测试编写一个脚本来验证旋转框以在更大的系统上实现它,但我正在为 python 苦苦挣扎,因为它警告说我的 [=12] 上没有旋转框属性=] class。检查我的代码:
from Tkinter import *
class Window:
def __init__(self, toplevel):
self.spinbox = Spinbox(toplevel, from_ = 0, to = 10,
validate = 'all', validatecommand = self.validate)
self.spinbox.pack()
def validate(self):
print self.spinbox.get()
root = Tk()
Window(root)
root.mainloop()
这是它给出的错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__
return self.func(*args)
File "D:\DOCS\FILIPE\PROGRAMMING\PYTHON\Tkinter sandbox - spinbox validate.
py", line 13, in validate
print self.spinbox.get()
AttributeError: Window instance has no attribute 'spinbox'
谁能帮我解决这个问题?
如果您在代码中添加一些 print
语句:
class Window:
def __init__(self, toplevel):
print "A start", self, self.__dict__
self.spinbox = Spinbox(toplevel, from_ = 0, to = 10,
validate = 'all', validatecommand = self.validate)
self.spinbox.pack()
print "A end", self, self.__dict__
def validate(self):
print "B", self, self.__dict__
print self.spinbox.get()
#...
你得到输出:
A start <__main__.Window instance at 0x7fe4f8deec20> {}
B <__main__.Window instance at 0x7fe4f8deec20> {}
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1532, in __call__
return self.func(*args)
File "t.py", line 14, in validate
print self.spinbox.get()
AttributeError: Window instance has no attribute 'spinbox'
A end <__main__.Window instance at 0x7fe4f8deec20> {'spinbox': <Tkinter.Spinbox instance at 0x7fe4f8e0da28>}
这意味着在构造函数内部调用了validate函数来验证初始值,而此时self.spinbox
还没有被设置。您将需要检查您是否仍在构建或延迟设置 validatecommand
,如下所示:
self.spinbox = Spinbox(toplevel, from_ = 0, to = 10,
validate = 'all')
self.spinbox["validatecommand"] = self.validate
self.spinbox.pack()
我设法使用文本变量和跟踪方法解决了这个问题
#Validates
self.countstringvar = StringVar()
self.countstringvar.trace("w", lambda name, index, mode,
sv = self.countstringvar: self.noLettersValidate(sv, self.count))
self.starsstringvar = StringVar()
self.starsstringvar.trace("w", lambda name, index, mode,
sv = self.starsstringvar: self.noLettersValidate(sv, self.stars))
self.scorestringvar = StringVar()
self.scorestringvar.trace("w", lambda name, index, mode,
sv = self.scorestringvar: self.noLettersValidate(sv, self.score))
self.count['textvariable'] = self.countstringvar
self.stars['textvariable'] = self.starsstringvar
self.score['textvariable'] = self.scorestringvar
def removeLetters(self, s):
a = []
for i in s:
if i.isdigit():
a.append(i)
return ''.join(a)
def noLettersValidate(self, sv, w):
w.text(self.removeLetters(sv.get()))