2010-10-22 125 views
-1

我寫在PythonPython:變量未正確初始化?

self._job = None 

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

下面的代碼,我得到這個錯誤信息

AttributeError: 'str' object has no attribute '_job' 

爲什麼呢?我以爲我已經初始化之前變量...

更新 與定時器變量

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

你能展示完整的類定義嗎? – 2010-10-22 11:04:36

+0

此代碼是方法的一部分嗎?什麼是上下文? – 2010-10-22 11:05:03

+0

@Felix Kling它是一個班級 – aneuryzm 2010-10-22 11:11:00

回答

0

編輯: 隨着你新提供的代碼,似乎沒有在類中定義self._job或_job。

根據您當前的信息,出於某種原因,該函數正在爲自己接收一個字符串,而不是您定義的類的一個實例。

0

如果在課堂上正在發生同樣的問題:嘗試改變self._job = None_job = None - 在該步驟self不聲明,我認爲

+0

不會,這會引發NameError – knitti 2010-10-22 11:34:48

1

試試這個:


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

不同的是self,調用sliderCallback已經被綁定到其背景是有用的時候。

0

updateValuesliderCallback必須定義爲類的方法。您將它們定義爲build方法的本地功能。另外,考慮kitti的答案。