1ワードのみ:エラー処理のいくつかのテストの一環として、私が最初にself.keithley
クラスは、変数初期化せずにset_new_mode
関数を呼び出して試してみた例外印刷私はこのようになりますクラスの一部を持っている
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
。この場合、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
をその期待された形に戻すのですか?
編集:エラーを再現するための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()
問題を再現するための最小限のコードを提供できるので、同じ問題があるかどうかを確認できますか? – depperm
'logging'モジュールをチェックアウトしたいと思うかもしれません - 特に' logging.exception() 'は非常に便利です –
私はこれを再現できません、' ERROR:set_new_mode:Fnordインスタンスに属性がありません 'keithley' Fnordインスタンス'keithley'属性がありません –