在Spinbox上使用validatecommand时的AttributeError

时间:2015-01-09 18:56:55

标签: python class attributes tkinter

我正在编写一个测试脚本的测试,该脚本验证一个spinbox在更大的系统上实现它,但我正在努力使用python,因为它警告我的Window类没有spinbox属性。检查我的代码:

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\01 - spinbox validate.
py", line 13, in validate
    print self.spinbox.get()
AttributeError: Window instance has no attribute 'spinbox'

任何人都可以帮我这个吗?

2 个答案:

答案 0 :(得分:0)

如果您在代码中添加了一些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()

答案 1 :(得分:0)

我使用TEXTVARIABLES和TRACE方法来解决问题

    #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()))

感谢大家的贡献!