Reputation: 45
I am currently creating a GUI with tkinter for a tilt table in my lab. I have up and down buttons programmed to turn a pin on until the table reaches a certain angle, which is read in from an inclinometer via Arduino, and then turn the pin off. So currently the function associated with each button repeatedly reads in the angle until the correct angle is reached, but I also want to be able to turn off the pin whenever I choose. The problem is that while the function associated with Up is running, the program is not checking for any button presses. How can I get the pause button to interrupt the function?
I've tried to implement interrupts with the threading library, but it seems tkinter won't let any other code run while the function associated with a button() is running.
import tkinter as tk
from tkinter import *
import RPi.GPIO as GPIO
import time
import serial
global read_serial
win = Tk()
def Up():
if read_serial < target:
global read_serial #read_serial is edited elsewhere not included here
GPIO.output(40,GPIO.HIGH)
time.sleep(.05)
read_serial=ser.readline().rstrtip().decode("utf-8")
read_serial=float(read_serial)
Up()
else:
GPIO.otuput(40,GPIO.LOW)
def Pause():
GPIO.output(40,GPIO.LOW)
upButton = Button(win,text='UP',command=Up)
pauseButton = Button(win,text='PAUSE',command=Pause)
upButton.grid(row=1)
pauseButton.grid(row=2)
win.mainloop()
I didn't want to paste too much code but if I'm missing any crucial parts I can include more. I want to interrupt Up() when I press Pause, but once I press Up, the program ignores any input until read_serial is greater than target. Is it possible to implement an interrupt to check for other button presses with tkinter?
Upvotes: 3
Views: 300
Reputation: 311
The easiest to run a function in the background when using tkinter is to use the .after() method, this allows for you not to need the threading module. The 0 in the .after() is the number of ms to wait until it executes the function given.
example (not the best way as it now has another function):
def bnt_up():
win.after(0, Up)
upButton = Button(win,text='UP',command=bnt_up)
Upvotes: 1