2016-06-09 225 views
1

我试图函数W_Kpower(scpi_comm)集成到一类,但我得到一个错误:呼叫功能

keithley = rm.open_resource(instControl(inst_list)) 
NameError: global name 'self' is not defined 

你能帮我到功能集成到一个类。我将来需要这些。我是初学者,所以我不明白我的代码的所有细节。

import Tkinter,visa,time 
from ttk import * 

root = Tkinter.Tk 
rm = visa.ResourceManager() 

# Split port touple into a list 
inst_list = [] 
str_inst = rm.list_resources() 
for i in str_inst: 
    inst_list.append(i.split(",")) 

# Convert list into a string and replace it 
def delChar(inst,input_inst): 
    str_new = ''.join(str(e) for e in inst_list[input_inst]) 
    find_char = "()u[]" 
    for char in find_char: 
     inst = str_new.replace(char,"") 
    return inst 

def delCharIn(list_del): 
    str_new = ''.join(str(e) for e in list_del) 
    find_char = "()u[]" 
    for char in find_char: 
     list_del = str_new.replace(char,"") 
    return list_del 

# List the instruments and choose one 
def instControl(inst): 
    new_list = [] 
    for index in range(len(inst)): 
     new_list.append(delCharIn(inst[index])) 
    return new_list 


#keithley = rm.open_resource(instControl(inst_list)) 

class InterfaceApp(root): 
    def __init__(self,parent): 
     root.__init__(self,parent) 
     self.parent = parent 
     self.initialize() 

    def initialize(self): 

     frInstrument = Tkinter.Frame(width=800, height=200, bg="", 
            colormap="new") 
     frInstrument.grid(row=0,sticky='EW') 

     separator = Tkinter.Frame(width=800, height=2, bd=1, relief='sunken') 
     separator.grid(row=1) 

     frSettings = Tkinter.Frame(width=800, height = 400, bg="", 
            colormap="new") 
     frSettings.grid(row=3,sticky='EW') 

     self.grid_columnconfigure(0,weight=1) 
     self.resizable(False,False) 

     groupInstrument = Tkinter.LabelFrame(frInstrument, 
              text="Choose an Instruments", 
              padx = 5, pady=5) 
     groupInstrument.grid(row = 0, column = 1, sticky="EW") 
     choices = instControl(inst_list) 
     self.choiceVarPower = Tkinter.StringVar() 
     self.choiceVarPower.set(choices[0]) 


     inst1 =Combobox(groupInstrument, values = choices,textvariable = 
                self.choiceVarPower) 
     inst1.grid(row=0, column=2,sticky="EW",padx=2,pady=2) 



     instLabel1 = Tkinter.Label(groupInstrument, text="Power Supply: ") 
     instLabel1.grid(row=0,column=1,sticky='EW') 

     instLabel2 = Tkinter.Label(groupInstrument, text="Multimeter: ") 
     instLabel2.grid(row=1,column=1,sticky='EW') 

     self.choiceVarMulti = Tkinter.StringVar() 
     self.choiceVarMulti.set(choices[0]) 
     inst2 = Combobox(groupInstrument, values=choices, textvariable = 
          self.choiceVarMulti) 
     inst2.grid(row=1,column=2,sticky='EW',padx=2,pady=2) 

     but_start = Tkinter.Button(frSettings,text='Run', 
           command=self.Run) 
     but_start.grid(row=0,column=1,sticky='EW') 
     but_closeInst = Tkinter.Button(frSettings,text="Close all instruments", 
             command = self.closeInst) 
     but_closeInst.grid(row=0,column=2,sticky='EW') 

    def W_KPower(scpi_comm): 
     return self.keithleyPower.write(":syst:loc") 
    def closeInst(self): 
     W_KPower(":syst:loc") 

    def Run(self): 
     self.keithleyPower = rm.open_resource(self.choiceVarPower.get()) 
     self.keithleyMultimeter = rm.open_resource(self.choiceVarMulti.get()) 


if __name__ == '__main__': 
    app = InterfaceApp(None) 
    app.title("") 
    app.mainloop() 

回答

3

您在方法定义缺少一个self这里:

def W_KPower(scpi_comm): # this should be def W_KPower(self, scpi_comm) 
    return self.keithleyPower.write(":syst:loc") 

,我想在这里,你需要self.

def closeInst(self): 
    W_KPower(":syst:loc") # this should be self.W_KPower(":syst:loc")