python 2.7
I'm using a raspberry pi to monitor some digital input signals as an hobby project. I want to have a large display that shows the accumulated values and updates them with global variables that will keep track of the input signals. I'm going to be using push buttons on a breadboard for now so I tried to use a holding variable to keep the counters from incrementing every cycle. I am very new to Python as I'm sure this is obvious so please keep it simple if possible. I am calling one input PE_MATTRESSES
, another PE_COMBINED
, and the other RESET
, I want to display a third value that subtracts PE_MATTRESSES
from PE_COMBINED
. Of course RESET
will zero everything..
My pseudo code though process is:
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()
Your pseudo code includes a while 1
loop, but your actual code does not. Also, Inc()
is never called. I suggest you add the following lines to the end of Inputs()
:
self.Inc()
self.root.after(10, self.Inputs)
You'll also have to make root
an instance variable, ie include self.root = root
in __init__()
for this to work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.