Python:变量未正确初始化?

时间:2010-10-22 11:00:04

标签: python

我在python中编写了以下代码

self._job = None

#slider
def sliderCallback(self):
   if self._job:

我收到此错误消息

AttributeError: 'str' object has no attribute '_job'
为什么?我以为我在......之前初始化了变量。

更新 与Timer变量相同的问题

import Tkinter as tk
import vtk
from time import *
from threading import *
from vtk.tk import *
from Visualization import *
from Histogram import *
from ListItem import *

class UI(tk.Frame): 

    def build(self, root):

        #left column
        self.leftFrame = tk.Frame(root, width=400, height=550, bg="black") #.grid(column=4, row=0)
        self.leftFrame.pack(fill=tk.BOTH, expand=True, side=tk.LEFT)

        #right column
        self.rightFrame = tk.Frame(root, width=400, height=550, bg="black") #.grid(column=4, row=0)
        self.rightFrame.pack(fill=tk.BOTH, expand=True, side=tk.RIGHT)
        #self.rightBottomFrame = tk.Frame(rightFrame, width=400, height=550, bg="red") #.grid(column=4, row=0)

        #visualization
        self.vis = Visualization(self.rightFrame, 400, 350, tk.RIGHT)
        #self.vis.updateContourValue(400)

        #left column
        self.middleFrame = tk.Frame(root, width=400, height=550, bg="black") #.grid(column=0, columnspan=4, row=0)
        self.middleFrame.pack(fill=tk.Y, expand=True)

        #isosurfaces list
        def addItem(color, volume, surface):        
            listitem = ListItem(self.listFrame, color, volume, surface)

        self.listFrame = tk.Frame(self.middleFrame, width=400, height=500, bg="black") #.grid(column=0, columnspan=4, row=0)
        self.listFrame.pack(fill=tk.BOTH, expand=True, side=tk.TOP)
        addItem("#243", self.vis.getVolume(), self.vis.getSurface())

        #preview
        self.preview = Visualization(self.middleFrame, 200, 200, tk.BOTTOM)
        #self.preview.updateContourValue(1500)

        #histogram
        self.histFrame = Histogram(self.leftFrame, 400, 400, tk.TOP, self.preview.getData())


        #slider
        def updateValue(self):
            self.preview.updateContourValue(float(self.slider.get() ))
            print "updated value"

        self.timer = Timer(5.0, updateValue)

        def sliderCallback(self):
            self.timer.cancel()
            self.timer.start() # after 30 seconds, "hello, world" will be printed
            #if self._job:
                #root.after_cancel(self._job)
                #print "remove it"
            #self._job = root.after(500, self.updateValue)

        #def updateValue(value):
            #print('horizontal: {v}'.format(v=value))

        self.slider = tk.Scale(self.leftFrame, from_=0, to=256, orient=tk.HORIZONTAL, command=sliderCallback) #.grid(column=0, columnspan=3, row=1)
        self.slider.pack(in_=self.leftFrame, fill=tk.X)
        self.slider.set(200)

        #add Isosurface button
        def addIso():
            addItem("#243", self.vis.getVolume(), self.vis.getSurface())

        self.addButton = tk.Button(self.leftFrame, text="Add", command=addIso) #.grid(column=3, row=1)
        self.addButton.pack(in_=self.leftFrame, side="right", pady=2)

4 个答案:

答案 0 :(得分:1)

试试这个:


self.slider = tk.Scale(self.leftFrame, from_=0, to=256, orient=tk.HORIZONTAL, command=self.sliderCallback)

差异是self,当调用时,sliderCallback必须绑定到其实例才有用。

答案 1 :(得分:0)

如果在课堂上发生这种情况:请尝试将self._job = None更改为_job = None - 在此步骤self未声明,我认为

答案 2 :(得分:0)

编辑: 使用新提供的代码,类中似乎没有self._job或_job的定义。

根据您当前的信息,由于某种原因,该函数正在接收自己的字符串而不是您定义的类的实例。

答案 3 :(得分:0)

必须将

updateValuesliderCallback定义为类的方法。您将它们定义为build方法的本地函数。另外,请考虑kitti的答案。