2016-06-09 4 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") 
Смежные вопросы