gpt4 book ai didi

python - 在类内部调用函数

转载 作者:行者123 更新时间:2023-11-30 22:54:34 25 4
gpt4 key购买 nike

我尝试将函数 W_Kpower(scpi_comm) 集成到一个类中,但出现错误:

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

你能帮我将该函数集成到一个类中吗?我将来的其他类(class)都需要这个。我是初学者,所以我不理解代码的所有细节。

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()

最佳答案

您在此处的方法定义中缺少 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")

关于python - 在类内部调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37724209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com