4

蟒蛇2.7

我正在使用树莓派来监控一些数字输入信号作为一个爱好项目。我想要一个大显示屏来显示累积值并使用全局变量更新它们,这些变量将跟踪输入信号。我现在要在面包板上使用按钮,所以我尝试使用一个保持变量来防止计数器在每个周期递增。我对 Python 很陌生,因为我确信这很明显,所以请尽可能保持简单。我正在调用一个输入PE_MATTRESSES,另一个PE_COMBINED,另一个RESET,我想显示PE_MATTRESSES从中减去的第三个值PE_COMBINED。当然RESET会把一切归零..

我的伪代码虽然过程是:

  • 建立 I/O 和全局变量
  • 创建图形用户界面
  • while 1
    -- 查看 I/O
    -- 更新全局变量
    -- 更新标签以显示它们
    -- 循环

我的代码如下:

import Tkinter as tk
import tkFont
import time
import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)


PE_MATTRESS = GPIO.input(17)
PE_MATTRESS_LATCH = 0
PE_MATTRESS_ACCUMULATOR = 0
PE_COMBINED = GPIO.input(21)
PE_COMBINED_LATCH = 0
PE_COMBINED_ACCUMULATOR = 0
BOXSPRING_ACCUMULATOR = 0
BOXSPRING_ACCUMULATOR = PE_COMBINED_ACCUMULATOR - PE_MATTRESS_ACCUMULATOR 
RESET = GPIO.input(22)



class App:
    def __init__(self):
        root=tk.Tk()
        # create a custom font
        self.customFont = tkFont.Font(family="Helvetica", size=117)


        # create widgets 

        label1 = tk.Label(root, text="MATTRESSES: %s" % PE_MATTRESS_ACCUMULATOR , font=self.customFont)
        label1.pack()

        spacer1 = tk.Label(root, text="", font=self.customFont)
        spacer1.pack()

        Quit = tk.Button(root, text="QUIT", command=quit)
        Increment = tk.Button(root, text="Test", command=Inc)
        Quit.pack()
        Increment.pack()


        label2 = tk.Label(root, text="BOXSPRINGS: %s" % BOXSPRING_ACCUMULATOR, font=self.customFont)
        label2.pack()

        spacer2 = tk.Label(root, text="", font=self.customFont)
        spacer2.pack()


        label3 = tk.Label(root, text="      COMBINED: %s" % PE_COMBINED_ACCUMULATOR, font=self.customFont)
        label3.pack()



        root.geometry('1800x1000+0+0')
        root.after(10,Inputs)

        root.mainloop()

    def Inc():

        PE_MATTRESS_ACCUMULATOR = PE_MATTRESS_ACCUMULATOR + 1
    label1.setText("MATTRESSES: %s" % PE_MATTRESS_ACCUMULATOR)
    label2.setText("BOXSPRINGS: %s" % BOXSPRING_ACCUMULATOR)
    label3.setText("      COMBINED: %s" % PE_COMBINED_ACCUMULATOR)

        root.update_idletasks()


    def Inputs():

        if PE_MATTRESS == True and PE_MATTRESS_LATCH == False:
            PE_MATTRESS_ACCUMULATOR = PE_MATTRESS_ACCUMULATOR + 1
            PE_MATTRESS_LATCH = True


        elif PE_MATTRESS == False and PE_MATTRESS_LATCH == True:
            PE_MATTRESS_LATCH = False

        elif PE_COMBINED == True and PE_COMBINED_LATCH == False:
            PE_COMBINED_ACCUMULATOR = PE_COMBINED_ACCUMULATOR + 1
            PE_COMBINED_LATCH = True

        elif PE_COMBINED == False and PE_COMBINED_LATCH == True:
            PE_COMBINED_LATCH = False

        elif RESET == True:
            PE_COMBINED_ACCUMULATOR = 0
            PE_MATTRESS_ACCUMULATOR = 0

app=App()
4

1 回答 1

0

您的伪代码包含一个while 1循环,但您的实际代码没有。此外,Inc()从不调用。我建议您将以下几行添加到末尾Inputs()

self.Inc()
self.root.after(10, self.Inputs)

您还必须创建root一个实例变量,即包含self.root = root在其中__init__()以使其工作。

于 2013-02-26T13:32:01.347 回答