1

I have tried to integrate the function W_Kpower(scpi_comm) into a class, but I get an error:

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

Could you help me to integrate the function into a class. I need that for the other classes in the future. I'm beginner, so I don't understand all the details of my code.

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()
zondo
20.4k8 gold badges50 silver badges89 bronze badges
asked Jun 9, 2016 at 11:05

1 Answer 1

3

You are missing a self in the method definition here:

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

and I think here, you need self.:

def closeInst(self):
 W_KPower(":syst:loc") # this should be self.W_KPower(":syst:loc")
answered Jun 9, 2016 at 11:10
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.