python 2.7
I use raspberry pi to monitor some digital inputs as a hobby. I want to have a large display that shows the accumulated values ββand updates them using global variables that will track the input signals. At the moment, I am going to use the buttons on the layout, so I tried to use the holding variable so that the counters would not increase each cycle. I am very new to Python, as I am sure this is obvious, so please keep it simple if possible. I call one input PE_MATTRESSES , another PE_COMBINED , and another RESET , I want to display a third value that subtracts PE_MATTRESSES from PE_COMBINED . Of course, RESET will be zero all.
My pseudo code though process:
- set I / O and global variables
- create GUI
- while 1
- see I / O
- update global variables
- refresh shortcuts to display them
- loop
My code is as follows:
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()
source share