0

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()
10
  • What happens when you run the script, do you receive an error message?
    – Matt
    Oct 18, 2018 at 3:50
  • In the example you posted, root does not exist - I assume you have oversimplified your code to try to get the point across?
    – Matt
    Oct 18, 2018 at 3:51
  • I don't get error messages. Yes, I oversimplified it. I'm not showing the whole code. Oct 18, 2018 at 3:54
  • I'm not familiar with the RPI GPIO module but I do know tkinter + threading. Can you show more of your code, from what you posted it looks like it should be running fine.
    – Matt
    Oct 18, 2018 at 3:56
  • I have posted the whole code. Oct 18, 2018 at 4:01

1 Answer 1

0

There is nothing wrong with your thread call. The error resides somewhere in your functions, probably to do with the RPi.GPIO or serial modules which I'm unfamiliar with. In the code below, I simplified each of your functions to output a simple print statement and removed the GPIO setup code at the start. I made no changes to your threaded call. Note that it runs fine.

import tkinter as tk
from threading import Thread
import time

def update_timeText():
    print("Update")
    root.after(10, update_timeText)

# To start the timer
def start():
    print("Start")

# To pause the kitchen timer
def pause():
    print("Pause")

# To reset the timer to 00:00:00
def reset():
    print("Reset")

# To exit our program
def exit():
    root.destroy()

def exitt(event):
    root.destroy()

def push_buttons():
    print("PUSH")

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()

I would suggest that you add back in your functions one by one to my code to diagnose which function is causing the issue. I'm happy to discuss more in chat if you need further help.

1
  • I added a while loop and time.sleep in my push_buttons function, and that way it worked. Oct 18, 2018 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.