I have created this program, but the function does not get called. I'm learning how to use threads. Any suggestion is well appreciated.
#!/usr/bin/env python3
import tkinter as tk
from threading import Thread
import RPi.GPIO as GPIO
import time
import serial
# GPIO BOARD PIN numbers
# setup GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
ser = serial.Serial ("/dev/ttyS0", 9600)
GPIO.setup(21, GPIO.IN)
GPIO.setup(20, GPIO.IN)
GPIO.setup(16, GPIO.IN)
GPIO.setup(26, GPIO.IN)
GPIO.setup(19, GPIO.IN)
GPIO.setup(13, GPIO.IN)
# Simple status flag
# False mean the timer is not running
# True means the timer is running (counting)
running = False
# Note: Python 2.6 or higher is required for .format() to work
def update_timeText():
global running
global timer
if running == False and GPIO.input(16) == False:
running = True
#print('Start')
ser.write(b'3')
elif running == True and GPIO.input(13) == False:
running = False
#print('Stop')
ser.write(b'6')
time.sleep(0.3)
elif timer != [0, 0, 0] and running == False and GPIO.input(13) == False:
timer = [0, 0, 0]
timeText.configure(text='00:00:00')
elif timer != [0, 0, 0] and running == False and GPIO.input(16) == False:
running = True
print('Start')
ser.write(b'3')
time.sleep(0.3)
if running:
# Every time this function is called, we will increment 1 centisecond (1/100 of a second)
timer[2] += 1
# Every 100 centisecond is equal to 1 second
if (timer[2] >= 100):
timer[2] = 0
timer[1] += 1
# Every 60 seconds is equal to 1 min
if (timer[1] >= 60):
timer[0] += 1
timer[1] = 0
# We create our time string here
timeString = pattern.format(timer[0], timer[1], timer[2])
# Update the timeText Label box with the current time
timeText.configure(text=timeString)
# Call the update_timeText() function after 1 centisecond
root.after(10, update_timeText)
# To start the timer
def start():
global running
running = True
# To pause the kitchen timer
def pause():
global running
running = False
# To reset the timer to 00:00:00
def reset():
global timer
timer = [0, 0, 0]
timeText.configure(text='00:00:00')
# To exit our program
def exit():
root.destroy()
def exitt(event):
root.destroy()
def push_buttons():
if running == True and GPIO.input(20) == False:
#print('Speed up')
ser.write(b'2')
time.sleep(0.3)
elif running == True and GPIO.input(21) == False:
#print('Speed down')
ser.write(b'1')
time.sleep(0.3)
root = tk.Tk()
root.attributes("-fullscreen",True)
root.wm_attributes("-topmost",1)
root.bind("<Escape>", exitt)
button_frame = tk.Frame(root)
button_frame.pack(fill=tk.X, side=tk.BOTTOM)
start_button = tk.Button(button_frame, text='Start', command=start, width = 12 , height = 3,bg="green")
stop_button = tk.Button(button_frame, text='Stop', command=pause, width = 12 , height = 3,bg="red")
reset_button = tk.Button(button_frame, text='Reset', command=reset, width = 12 , height = 3,bg="yellow")
quit_button = tk.Button(button_frame, text='Quit', command=exit, width = 12 , height = 3,bg="grey")
button_frame.columnconfigure(0, weight=1)
button_frame.columnconfigure(1, weight=1)
button_frame.columnconfigure(2, weight=1)
button_frame.columnconfigure(3, weight=1)
start_button.grid(row=0, column=0, sticky=tk.W+tk.E)
stop_button.grid(row=0, column=1, sticky=tk.W+tk.E)
reset_button.grid(row=0, column=2, sticky=tk.W+tk.E)
quit_button.grid(row=0, column=3, sticky=tk.W+tk.E)
# Our time structure [min, sec, centsec]
timer = [0, 0, 0]
# The format is padding all the
pattern = '{0:02d}:{1:02d}:{2:02d}'
# Create a timeText Label (a text box)
timeText = tk.Label(root, text="00:00:00", font=("Helvetica", 150))
timeText.pack()
thread = Thread(target=push_buttons)
thread.start()
update_timeText()
root.mainloop()
root
does not exist - I assume you have oversimplified your code to try to get the point across?