如何在Python中创建不断更新的标签

如何在Python中创建不断更新的标签,python,user-interface,tkinter,label,updating,Python,User Interface,Tkinter,Label,Updating,我有一个树莓圆周率和使用Python我已经写了一个GUI来控制风扇使用PWM和计算出转速从风扇脉冲输出。GUI顶部有一个标签,按钮用于选择不同的占空比,LED徽标指示器用于显示选择的按钮,按钮用于“停止风扇”、“退出”、“显示RPM” 我使用“启动风扇”按钮启动风扇。然后我可以选择不同的占空比。当我单击“显示RPM”按钮时,GUI顶部的标签显示RPM(小数点后有许多数字),这只是RPM的快速快照。我的代码运行正常 我的问题是。如何让标签不断显示不断变化的转速?我想删除“显示RPM”按钮,让标签始

我有一个树莓圆周率和使用Python我已经写了一个GUI来控制风扇使用PWM和计算出转速从风扇脉冲输出。GUI顶部有一个标签,按钮用于选择不同的占空比,LED徽标指示器用于显示选择的按钮,按钮用于“停止风扇”、“退出”、“显示RPM”

我使用“启动风扇”按钮启动风扇。然后我可以选择不同的占空比。当我单击“显示RPM”按钮时,GUI顶部的标签显示RPM(小数点后有许多数字),这只是RPM的快速快照。我的代码运行正常

我的问题是。如何让标签不断显示不断变化的转速?我想删除“显示RPM”按钮,让标签始终显示实时变化的RPM。 我在谷歌上搜索了几个小时寻求帮助,并尝试了“after”和“function calls”等,但我似乎无法理解我需要做什么,或者它应该放在代码中的什么地方

非常感谢您的时间和帮助。 代码可能不是我所知道的最好的,但我正在学习。我很乐意接受任何关于如何改进的建议

# Fan PWM Control
import time
from time import sleep
import piface.pfio as pfio
pfio.init()

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
GPIO.setup(18,GPIO.IN,pull_up_down=GPIO.PUD_UP)
my_pwm = GPIO.PWM(7,1000)
last_time = time.time()
this_time = time.time()
RPM = 0


from Tkinter import *

import Tkinter as tk

a=0

class App:

    def __init__(self, master):
            self.master=master
            frame = Frame(master)
            frame.pack()

            self.TACHO = Label(frame, text=RPM)
            self.TACHO.grid(row=0, column=0)

            self.button0 = Button(frame, text='Start Fan', command=self.convert0)
            self.button0.grid(row=2, column=0)
            self.LED0 = Label(frame, image=logo2)
            self.LED0.grid(row=2, column=1)

            self.button1 = Button(frame, text='Fan 30% Duty Cycle', command=self.convert1)
            self.button1.grid(row=3, column=0)
            self.LED1 = Label(frame, image=logo2)
            self.LED1.grid(row=3, column=1)

            self.button2 = Button(frame, text='Fan 45% Duty Cycle', command=self.convert2)
            self.button2.grid(row=4, column=0)
            self.LED2 = Label(frame, image=logo2)
            self.LED2.grid(row=4, column=1)

            self.button3 = Button(frame, text='Fan 60% Duty Cycle', command=self.convert3)
            self.button3.grid(row=5, column=0)
            self.LED3 = Label(frame, image=logo2)
            self.LED3.grid(row=5, column=1)

            self.button4 = Button(frame, text='Fan 75% Duty Cycle', command=self.convert4)
            self.button4.grid(row=6, column=0)
            self.LED4 = Label(frame, image=logo2)
            self.LED4.grid(row=6, column=1)

            self.button5 = Button(frame, text='Fan 90% Duty Cycle', command=self.convert5)
            self.button5.grid(row=7, column=0)
            self.LED5 = Label(frame, image=logo2)
            self.LED5.grid(row=7, column=1)

            self.button6 = Button(frame, text='Fan 100% Duty Cycle', command=self.convert6)
            self.button6.grid(row=8, column=0)
            self.LED6 = Label(frame, image=logo2)
            self.LED6.grid(row=8, column=1)

            self.button7 = Button(frame, text='STOP FAN', command=self.convert7)
            self.button7.grid(row=9, column=0)

            self.button8 = Button(frame, text='Exit', command=self.convert8)
            self.button8.grid(row=10, column=0)


            self.button9 = Button(frame, text='Display RPM', command=self.Tach)
            self.button9.grid(row=11, column=0)


    def convert0(self):

            print('Fan 15% Duty Cycle')
            global a
            a = 1
            self.button0.config(text='Fan 15% Duty Cycle')
            self.LED0.config(image = logo)
            my_pwm.start(15)
            pfio.digital_write(0,1) #turn on
            self.LED1.config(image = logo2)
            self.LED2.config(image = logo2)
            self.LED3.config(image = logo2)
            self.LED4.config(image = logo2)
            self.LED5.config(image = logo2)
            self.LED6.config(image = logo2)
            pfio.digital_write(1,0) #turn off
            pfio.digital_write(2,0) #turn off
            pfio.digital_write(3,0) #turn off
            pfio.digital_write(4,0) #turn off
            pfio.digital_write(5,0) #turn off
            pfio.digital_write(6,0) #turn off
            pfio.digital_write(7,0) #turn off


    def convert1(self):

        if a==0:

            print('Please Start the Fan')

        else:

            print('Fan 30% Duty Cycle')
            self.button1.config(text='Fan 30% Duty Cycle')
            self.LED1.config(image = logo)
            my_pwm.ChangeDutyCycle(30)
            pfio.digital_write(1,1) #turn on
            self.LED0.config(image = logo2)
            self.LED2.config(image = logo2)
            self.LED3.config(image = logo2)
            self.LED4.config(image = logo2)
            self.LED5.config(image = logo2)
            self.LED6.config(image = logo2)
            pfio.digital_write(0,0) #turn off
            pfio.digital_write(2,0) #turn off
            pfio.digital_write(3,0) #turn off
            pfio.digital_write(4,0) #turn off
            pfio.digital_write(5,0) #turn off
            pfio.digital_write(6,0) #turn off
            pfio.digital_write(7,0) #turn off

    def convert2(self):

        if a==0:

            print('Please Start the Fan')

        else:

            print('Fan 45% Duty Cycle')
            self.button2.config(text='Fan 45% Duty Cycle')
            self.LED2.config(image = logo)
            my_pwm.ChangeDutyCycle(45)
            pfio.digital_write(2,1) #turn on
            self.LED0.config(image = logo2)
            self.LED1.config(image = logo2)
            self.LED3.config(image = logo2)
            self.LED4.config(image = logo2)
            self.LED5.config(image = logo2)
            self.LED6.config(image = logo2)
            pfio.digital_write(0,0) #turn off
            pfio.digital_write(1,0) #turn off
            pfio.digital_write(3,0) #turn off
            pfio.digital_write(4,0) #turn off
            pfio.digital_write(5,0) #turn off
            pfio.digital_write(6,0) #turn off
            pfio.digital_write(7,0) #turn off

    def convert3(self):

        if a==0:

            print('Please Start the Fan')

        else:

            print('Fan 60% Duty Cycle')
            self.button3.config(text='Fan 60% Duty Cycle')
            self.LED3.config(image = logo)
            my_pwm.ChangeDutyCycle(60)
            pfio.digital_write(3,1) #turn on
            self.LED0.config(image = logo2)
            self.LED1.config(image = logo2)
            self.LED2.config(image = logo2)
            self.LED4.config(image = logo2)
            self.LED5.config(image = logo2)
            self.LED6.config(image = logo2)
            pfio.digital_write(0,0) #turn off
            pfio.digital_write(1,0) #turn off
            pfio.digital_write(2,0) #turn off
            pfio.digital_write(4,0) #turn off
            pfio.digital_write(5,0) #turn off
            pfio.digital_write(6,0) #turn off
            pfio.digital_write(7,0) #turn off

    def convert4(self):

        if a==0:

            print('Please Start the Fan')

        else:

            print('Fan 75% Duty Cycle')
            self.button4.config(text='Fan 75% Duty Cycle')
            self.LED4.config(image = logo)
            my_pwm.ChangeDutyCycle(75)
            pfio.digital_write(4,1) #turn on
            self.LED0.config(image = logo2)
            self.LED1.config(image = logo2)
            self.LED2.config(image = logo2)
            self.LED3.config(image = logo2)
            self.LED5.config(image = logo2)
            self.LED6.config(image = logo2)
            pfio.digital_write(0,0) #turn off
            pfio.digital_write(1,0) #turn off
            pfio.digital_write(2,0) #turn off
            pfio.digital_write(3,0) #turn off
            pfio.digital_write(5,0) #turn off
            pfio.digital_write(6,0) #turn off
            pfio.digital_write(7,0) #turn off

    def convert5(self):

        if a==0:

            print('Please Start the Fan')

        else:

            print('Fan 90% Duty Cycle')
            self.button5.config(text='Fan 90% Duty Cycle')
            self.LED5.config(image = logo)
            my_pwm.ChangeDutyCycle(90)
            pfio.digital_write(5,1) #turn on
            self.LED0.config(image = logo2)
            self.LED1.config(image = logo2)
            self.LED2.config(image = logo2)
            self.LED3.config(image = logo2)
            self.LED4.config(image = logo2)
            self.LED6.config(image = logo2)
            pfio.digital_write(0,0) #turn off
            pfio.digital_write(1,0) #turn off
            pfio.digital_write(2,0) #turn off
            pfio.digital_write(3,0) #turn off
            pfio.digital_write(4,0) #turn off
            pfio.digital_write(6,0) #turn off
            pfio.digital_write(7,0) #turn off

    def convert6(self):

        if a==0:

            print('Please Start the Fan')

        else:

            print('Fan 100% Duty Cycle')
            self.button6.config(text='Fan 100% Duty Cycle')
            self.LED6.config(image = logo)
            my_pwm.ChangeDutyCycle(100)
            pfio.digital_write(6,1) #turn on
            self.LED0.config(image = logo2)
            self.LED1.config(image = logo2)
            self.LED2.config(image = logo2)
            self.LED3.config(image = logo2)
            self.LED4.config(image = logo2)
            self.LED5.config(image = logo2)
            pfio.digital_write(0,0) #turn off
            pfio.digital_write(1,0) #turn off
            pfio.digital_write(2,0) #turn off
            pfio.digital_write(3,0) #turn off
            pfio.digital_write(4,0) #turn off
            pfio.digital_write(5,0) #turn off
            pfio.digital_write(7,0) #turn off

    def convert7(self):


            print('STOP FAN')
            global a
            a = 0
            self.button7.config(text='STOP FAN')
            my_pwm.stop()
            pfio.digital_write(7,1) #turn on
            self.LED0.config(image = logo2)
            self.LED1.config(image = logo2)
            self.LED2.config(image = logo2)
            self.LED3.config(image = logo2)
            self.LED4.config(image = logo2)
            self.LED5.config(image = logo2)
            self.LED6.config(image = logo2)
            self.button0.config(text='Start Fan')
            pfio.digital_write(0,0) #turn off
            pfio.digital_write(1,0) #turn off
            pfio.digital_write(2,0) #turn off
            pfio.digital_write(3,0) #turn off
            pfio.digital_write(4,0) #turn off
            pfio.digital_write(5,0) #turn off
            pfio.digital_write(6,0) #turn off


    def convert8(self):


            print('EXIT PROGRAM')
            my_pwm.stop()
            quit()

    def EventsPerTime(self):
        global RPM, this_time, last_time
        if GPIO.input(self) > 0.5:
            this_time = time.time()
            RPM = (1/(this_time - last_time))*30
            last_time = this_time
            #print 'RPM is %d' % RPM



    GPIO.add_event_detect(18, GPIO.RISING, callback=EventsPerTime, bouncetime=3)


    def Tach(self):
        global RPM
        if RPM > 1:
            print 'RPM is %d' % RPM
            self.TACHO.config(text=RPM)




root = Tk()
logo2 = PhotoImage(file="/home/pi/Off LED.gif")
logo = PhotoImage(file="/home/pi/Red LED.gif")


root.wm_title('FAN PWM Control program')
app = App(root)

root.mainloop()

您已经有了一个获取当前RPM的函数。最简单的方法是立即更新标签:

def EventsPerTime(self):
    global RPM, this_time, last_time
    if GPIO.input(self) > 0.5:
        ...
        RPM = (1/(this_time - last_time))*30
        ...

        self.TACHO.configure(text=RPM)
问题的另一部分是,您没有正确指定回拨。您需要更改此代码:

GPIO.add_event_detect(..., callback=EventsPerTime, ...)
为此:

GPIO.add_event_detect(..., callback=self.EventsPerTime, ...)
您还必须重新定义
EventsPerTIme
以接受附加参数:

def EventsPerTime(self, channel):
   ...

以下是我在Raspberry Pi上显示RPM的PWM GUI控件的代码更改。由于这篇文章和网站提供了良好的反馈和帮助,我成功地运行了这个程序并实现了我的目标。该代码可能没有它可能是好的或有效的,所以请随时提供改进建议

感谢您的帮助和分享知识

        #self.button9 = Button(frame, text='Display RPM', command=self.Tach)    Stopped using this button
        #self.button9.grid(row=11, column=0)    Stopped using this button

        self.TimerInterval = 500    #This sets the time interval between RPM readings
        self.Tach()         #This will start the 'Tach' method when the GUI starts

def convert0(self):
我还为“Tach”编写了一个方法,它使用了“after”函数,该函数将在设定的时间间隔后调用给定的函数。所以我把它设置为每500毫秒调用一次

def Tach(self):
    print 'RPM is %d' % RPM
    self.TACHO.config(text='RPM is %d' % RPM)
    self.master.after(self.TimerInterval,self.Tach) #Used 'after' to repeat this method at the time interval

我还不知道如何停止“after”函数,但我会继续学习

如果您已经从
EventsPerTime
定期获取RPM,那么是什么阻止了您更新标签呢?我最初是使用我的一个谷歌结果尝试的。我猜是self.Tach()。但是我不能让它工作,如果它是正确的东西放进去。请做一个。如果您的问题是显示RPM,那么您不需要所有与RPM显示无关的代码。我尝试了它,但得到了一个错误-AttributeError:“int”对象没有属性“TACHO”。@blast_uk:您问题中的代码不可能给出该错误。你必须把self.TACHO重新定义为一个整数。这是我得到的错误,但我同意你说的,我就是看不出来。我一直在看“时间”、转速计算等等@blast_uk:我说错了。出现“int”错误的原因是您错误地指定了回调。在功能中,
self
接收GPIO模块提供的通道。我已经更新了我的答案。我尝试将其添加到中,但现在我在回调中得到错误'NameError:name'self'未定义。我认为这与它的缩进有关,它不是函数的一部分?恐怕我所知甚少。