我有一节课是这样的:
def set_new_mode(self,mode):
try:
#this will fail, since self.keithley is never initialized
print self.keithley
self.keithley.setzerocheck(on=True)
self.keithley.selectmode(mode,nplc=6)
self.keithley.setzerocheck(on=False) #keithcontrol class will
#automatically turn on zero correction when zchk is disabled
self.mode = mode
self.print_to_log('\nMode set to %s' % self.mode)
except Exception as e:
self.print_to_log('\nERROR:set_new_mode: %s' % e)
print e
作为一些错误处理测试的一部分,我尝试在不初始化类变量self.keithley
的情况下调用set_new_mode
函数。在本例中,我希望print self.keithley
语句将引发AttributeError: keithgui instance has no attribute 'keithley'
。但是,print e
和self.print_to_log('\nERROR:set_new_mode: %s' % e)
表示e
只包含单词“keithley”。你知道吗
将print e
更改为print type(e)
会显示e
仍然具有类型AttributeError,但该变量不再包含有关异常的任何有用信息。为什么?如何将e
返回到预期的形式?你知道吗
编辑:这里是一个新的复制错误。要重现错误,请启动GUI,将模式更改为VOLT以外的模式,然后单击“更新”按钮。
import Tkinter
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
class keithgui(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
#we are not initially connected to the keithley
self.connected = False
self.pauseupdate = False
#set up frames to distribute widgets
#MASTER FRAME
self.mframe = Tkinter.Frame(self,bg='green')
self.mframe.pack(side=Tkinter.TOP,fill='both',expand=True)
#LEFT AND RIGHT FRAMES
self.Lframe = Tkinter.Frame(self.mframe,bg='red',borderwidth=2,relief='raised')
self.Lframe.pack(side='left',fill='both',expand=True)
self.Rframe = Tkinter.Frame(self.mframe,bg='blue',borderwidth=2,relief='raised')
self.Rframe.pack(side='right',fill='both',expand=False)
#create the log text widget to keep track of what we did last
#also give it a scrollbar...
scrollbar = Tkinter.Scrollbar(master=self.Lframe)
scrollbar.pack(side=Tkinter.RIGHT,anchor='n')
self.logtext = Tkinter.Text(master=self.Lframe,height=3,yscrollcommand=scrollbar.set)
scrollbar.config(command=self.logtext.yview)
self.logtext.pack(side=Tkinter.TOP,anchor='w',fill='both')
#Button to update all settings
updatebutton = Tkinter.Button(master=self.Rframe,text='Update',command=self.update_all_params)
updatebutton.grid(column=2,row=0)
#Option menu & label to select mode of the Keithley
modes = ['VOLT','CHAR','CURR']
modelabel = Tkinter.Label(master=self.Rframe,text='Select Mode:')
modelabel.grid(column=0,row=2,sticky='W')
self.mode = 'VOLT'
self.modevar = Tkinter.StringVar()
self.modevar.set(self.mode)
modeselectmenu = Tkinter.OptionMenu(self.Rframe,self.modevar,*modes)
modeselectmenu.grid(column=1,row=2,sticky='W')
def print_to_log(self,text,loc=Tkinter.END):
self.logtext.insert(loc,text)
self.logtext.see(Tkinter.END)
def update_all_params(self):
self.set_refresh_rate()
if self.modevar.get() != self.mode:
self.set_new_mode(self.modevar.get())
else:
self.print_to_log('\nAlready in mode %s' % self.mode)
def set_refresh_rate(self):
try:
self.refreshrate = np.float(self.refreshrateentryvar.get())
self.print_to_log('\nRefresh rate set to %06.3fs' % self.refreshrate)
except Exception as e:
self.print_to_log('\nERROR:set_referesh_rate: %s' % e)
def set_new_mode(self,mode):
try:
print self.keithley
self.keithley.setzerocheck(on=True)
self.keithley.selectmode(mode,nplc=6)
self.keithley.setzerocheck(on=False) #keithcontrol class will
#automatically turn on zero correction when zchk is disabled
self.mode = mode
self.print_to_log('\nMode set to %s' % self.mode)
except Exception as e:
self.print_to_log('\nERROR:set_new_mode: %s' % e)
print e
print type(e)
if __name__ == "__main__":
app = keithgui(None)
app.title('Keithley GUI')
app.mainloop()
如果修改代码:
然后开始使用
s
,您将看到在您的窗口中有一个__getattr__
函数。我想看看是什么导致了这个问题,但这实际上是你的答案。你知道吗在调用堆栈之后,它引导我调用
self.tk = _tkinter.create
,最终导致我得到here。归根结底,这个异常发生在C-territory中,因此它产生了一个不同的AttributeError
消息。你知道吗相关问题 更多 >
编程相关推荐