调用另一个函数时运行函数

时间:2018-05-23 09:35:03

标签: python python-3.x tkinter

我打算做的是在fault()每30分钟运行一次时运行getvoltage()函数。

Fault()将根据运行getvoltage()检索到的电压值打印不同的警告消息。

我在fault()的末尾插入了updatelabel()但是我收到了以下错误:NameError: name 'fault' is not defined

抱歉,这是我第一次开发一个需要Python编码的项目,所以我对它的工作方式还不熟悉。我感谢我能得到的每一个帮助。

以下是我的代码片段:

def updatesetpoints():
    lowvolt = lowvoltentry.get()
    chargefail = chargerfailentry.get()
    failsafe = failsafeentry.get()
    failsafereset = failsaferesetentry.get()
    fault()

def fault():
    if (voltage < lowvolt and voltage > chargefail):
        print ("Battery Voltage Low")
    elif (voltage < chargefail and voltage > failsafe):
        print ("Battery Charger Fail")
    elif ( voltage < failsafe):
        print ("Fail Safe Vent Open")



def update_label(label):

    def getvoltage():

        f=open("VoltageReadings.txt", "a+")
        readings = [0]*100
        maxsample = 100
        counter = 0

        while (counter < maxsample):

            reading = adc.read_adc(0, gain=GAIN)
            readings.append(reading)
            counter += 1

        avg = sum(readings)/100
        global voltage
        volt= (avg * 0.1259)/100
        voltage = str(volt)       
        f.write("%.2f" % (volt) + "\r\n")
        readings.clear()

        label.config(text=str('Voltage: {0:.2f}'.format(volt)))
        label.after(1800000, getvoltage)
    getvoltage()
    fault()

class Batterysetpts(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        self.configure(bg="white")

        #Variables
        global lowvolt
        global chargefail
        global failsafe
        global failsafereset
        lowvolt = tk.DoubleVar()
        chargefail = tk.DoubleVar()
        failsafe = tk.DoubleVar()
        failsafereset = tk.DoubleVar()

        #Entry Label
        global lowvoltentry
        global chargerfailentry
        global failsafeentry
        global failsaferesetentry
        lowvoltentry = tk.Entry(self, textvariable = lowvolt)
        chargerfailentry = tk.Entry(self, textvariable = chargefail)
        failsafeentry = tk.Entry(self, textvariable = failsafe)
        failsaferesetentry = tk.Entry(self, textvariable = failsafereset)
        lowvoltentry.place(relx = 0.67, rely = 0.35, anchor = "center")
        chargerfailentry.place(relx = 0.67, rely = 0.5, anchor = "center")
        failsafeentry.place(relx = 0.67, rely = 0.65, anchor = "center")
        failsaferesetentry.place(relx = 0.67, rely = 0.8, anchor = "center")

        #Labels
        LV = tk.Label(self, text = "Low Voltage Set Point", font = LARGE_FONT, bg="white")
        CF = tk.Label(self, text = "Charger Fail Set Point", font = LARGE_FONT, bg="white")
        FS = tk.Label(self, text = "Fail Safe Open Set Point", font = LARGE_FONT, bg="white")
        FSR = tk.Label(self, text = "Fail Safe Reset Set Point", font = LARGE_FONT, bg="white") 
        LV.place(relx = 0.37, rely = 0.35, anchor = "center")
        CF.place(relx = 0.37, rely = 0.5, anchor = "center")
        FS.place(relx = 0.37, rely = 0.65, anchor = "center")
        FSR.place(relx = 0.37, rely = 0.8, anchor = "center")  


        #Confirm Button
        confirmupdatebutton = tk.Button(self, text = "Confirm Changes", command = updatesetpoints)
        confirmupdatebutton.place(relx= 0.5, rely= 0.9, anchor = "center")

提前感谢您考虑这个问题并帮助我:)

1 个答案:

答案 0 :(得分:0)

我快速修改您的代码(免责声明,我还没有对其进行测试):

def updatesetpoints():
lowvolt = lowvoltentry.get()
chargefail = chargerfailentry.get()
failsafe = failsafeentry.get()
failsafereset = failsaferesetentry.get()
fault()

def fault(voltage):
    # Here voltage is a parameter (and thus local to the function)
    if (voltage < lowvolt and voltage > chargefail):
        print ("Battery Voltage Low")
    elif (voltage < chargefail and voltage > failsafe):
        print ("Battery Charger Fail")
    elif ( voltage < failsafe):
        print ("Fail Safe Vent Open")

def update_label(label):
    def getvoltage():
        f=open("VoltageReadings.txt", "a+")
        readings = [0]*100
        maxsample = 100
        counter = 0
        while (counter < maxsample):
            reading = adc.read_adc(0, gain=GAIN)
            readings.append(reading)
            counter += 1
        avg = sum(readings)/100
        volt= (avg * 0.1259)/100
        # Global variable voltage should not be needed any more
        f.write("%.2f" % (volt) + "\r\n")
        readings.clear()
        label.config(text=str('Voltage: {0:.2f}'.format(volt)))
        label.after(1800000, getvoltage)
        # We return the voltage
        return volt

    value = getvoltage()
    fault(value)

注意:全局变量voltage似乎是string。这有什么特别的原因吗?使用float对我来说似乎更合乎逻辑。