例外打印只有一个字

时间:2016-07-26 20:55:57

标签: python exception

我有一个看起来像这样的课程的一部分:

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

作为错误处理的一些测试的一部分,我尝试在没有首先初始化类变量set_new_mode的情况下调用self.keithley函数。在这种情况下,我希望print self.keithley语句会引发AttributeError: keithgui instance has no attribute 'keithley'。但是,print eself.print_to_log('\nERROR:set_new_mode: %s' % e)表示e仅包含“keithley”一词。

print e更改为print type(e)会发现e仍然具有AttributeError类型,但该变量不再包含有关该异常的任何有用信息。为什么?如何将e返回到预期的格式?

编辑:这是一个重现错误的MEW。 要重现错误,请启动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()

1 个答案:

答案 0 :(得分:1)

如果您修改代码:

import Tkinter as tk

class Fnord(tk.Tk):
    def set_new_mode(self,mode):
        try:
            import pdb; pdb.set_trace()
            #this will fail, since self.keithley is never initialized
            print self.keithley

Fnord().set_new_mode('whatever')

然后开始使用s,您会看到窗口上有__getattr__个功能。我正在寻找现在导致问题的原因,但这实际上是你的答案。

在调用堆栈之后,它引导我拨打电话self.tk = _tkinter.create,最终导致我获得here。归根结底,这个例外发生在C领域,所以它产生了一个不同的AttributeError消息。