I'm creating a python program on my RPi3 that changes the frequency of a GPIO pin depending on a Tkinter scale.
Here is my code:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
from Tkinter import *
import time
freq = 1.0
master = Tk()
def update():
period = 1.0/float(freq)
GPIO.output(8, GPIO.HIGH)
time.sleep(period/2.0)
GPIO.output(8, GPIO.LOW)
time.sleep(period/2.0)
master.after(0, update)
scale = Scale(master, from_=1, to=20000, orient=HORIZONTAL, variable=freq)
scale.pack()
GPIO.setup(8, GPIO.OUT)
master.after(0, update)
master.mainloop()
GPIO.cleanup()
For some reason, master.after(0, update) runs forever and master.mainloop() never runs. I can tell because the scale never shows up and pin 8 turns on for half a second, then turns off for half a second, and the cycle repeats. If I press Ctrl+C then master.after(0, update) stops running and master.mainloop() starts running, the scale appears, but nothing happens when I drag the slider left and right.
I ran the program by typing sudo python tone.py in the terminal then pressing enter.
Fix/Alternative?
Processing events
You are making two somewhat common mistakes: you shouldn't do after(0, ...), and you shouldn't call sleep.
after(0, ...) means that every time you process the event, you immediately add another event. The event loop never has a chance to process other events in the queue, including events to handle the slider, screen updates, etc.
When you call sleep, the GUI does exactly that: it sleeps. While it is sleeping it can't process any events.
The solution is to use only after with a reasonable time span, and not call sleep at all.
For example:
def update():
...
# set the pin high
GPIO.output(8, GPIO.HIGH)
# set the pin low in half the period
master.after(period/2, GPIO.output, 8, GPIO.LOW)
# do this again based on the period
master.after(period, update)
Another way, if you continually want to toggle the pin every half second, would be this:
def update(value=GPIO.HIGH):
GPIO.output(8, value)
next_value = GPIO.LOW if value == GPIO.HIGH else GPIO.HIGH
master.after(500, update, next_value)
Using the slider
When you use the variable attribute of the slider, the variable must be an instance of one of the special tkinter variables, such as IntVar. You will then need to call get or set to get or set the value.
For example:
freq = IntVar()
freq.set(1)
def update(value=GPIO.HIGH):
period = 1.0/float(freq.get())
...
mainloop() is running, but Tkinter won't update views unless it is idle. KeyboardInterrupt will tell it to cleanup, at which point it finishes its event queue (which includes updating interface) and exits.
Solution: Give mainloop time to be idle- you really just need to change your after(0, update) to have some milliseconds inside OR tell master to .update_idletasks() to update the GUI.
A slightly better solution would be to make your high/low parts into their own functions which call each after the needed delay- sleeping in the mainloop gui is a bad idea, as your GUI cannot update whatsoever if the program is sleeping. (nor can it take input et al.) You would have millisecond frames to change input before it updated again, while using two functions that call each other after the chosen milliseconds would let you adjust the timings et al while it's waiting to flip to the other on/off.
Related
I am using a system based on a raspberry to control some stuff. At the moment I am just testing it with turning a led on and off.
My plan is: Press a button to open a valve. Press the button again to close it - but if the button is not pressed, close it after a set time. My current script is as follows: (I know that this will not turn the led off on the second press)
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
import time,sched
s = sched.scheduler(time.time, time.sleep)
def button_callback(channel):
print("Button was pushed!")
print(time.time())
GPIO.output(18,GPIO.HIGH)
s.enter(10, 1, turnoff,argument='')
s.run()
def turnoff():
print "LED off"
print(time.time())
GPIO.output(18,GPIO.LOW)
btpin=22
ledpin=18
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(btpin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(ledpin,GPIO.OUT)
GPIO.add_event_detect(btpin,GPIO.RISING,callback=button_callback)
message = input("Press enter to quit\n\n")
GPIO.cleanup()
If I press the button and then leaves things alone, the led will turn off after 10 secs. But, if I press the button again immideately, nothing happens before the scheduler has finished, then a new press is registered. I had expected that the scheduler was spun of in the background so that when I pressed the button again, the callback would have ran again so I would have gotten the "Button was pushed" message (and everything happening afterwards would not have had any effect as GPIO 18 already was high and the scheduled call to turnoff would have happened after turnoff already had run.
Is it possible to use the sched library to do what I want or do I have to use some other techniques? I know I can either do it the simple way, by looping looking for a pressed button rather than registering a callback, or probably a more complicated way by creating a new thread that will pull the GPIO down after a given time - but is there something I have not understood in sched or is there some other library that gives me what I want - a way to tell python do do something a bit in the future without interfering with what else is going on.
(I do not need a very accurate timing - and also, this is just a part of what I intend to make a more complex control system, I manage to do exactly what I want using an arduino, but that will limit the further development)
Thanks to the tip from #stovfl, I rewrote the first part of my code:
import time,threading
def button_callback(channel):
pin=18
print("Button was pushed!")
print(time.time())
GPIO.output(pin,GPIO.HIGH)
t = threading.Timer(10.0, turnoff)
t.start()
and it works just like I wanted
I have this code:
def on_click(event=None):
c.unbind('<Button-1>')
c.config(background="red")
print ("You clicked the square")
time.sleep(delay)
c.config(background="green")
c.bind('<Button-1>', on_click)
root.update()
root = tk.Tk()
c = tk.Canvas(root, width=200, height=200, background="green")
c.pack()
c.bind('<Button-1>', on_click)
root.mainloop()
And when I click the canvas while it is red (unbound) it prints "You clicked the square" when the sleep is done.
I already tried the approach here: Deleting and changing a tkinter event binding
but got no results because I'm still able to click the square and obtain a print from it when it's red
You're calling unbind, then freezing the app. While it is frozen, events continue to get added to the queue without being processed. Immediately after the sleep is finished you re-establish the binding before the queue has a chance to process the events. By the time the events are handled, the binding will have already been re-established.
As a general rule of thumb you should never call sleep in a GUI program, and this is one good illustration why.
If you want to cancel the binding for a short period of time and then reset it, cancel the binding and then use after to reset it after the given time period.
def on_click(event=None):
c.unbind('<Button-1>')
c.config(background="red")
c.after(delay, enable_binding)
def enable_binding():
c.config(background="green")
c.bind('<Button-1>', on_click)
When you click, your function is called and you change the color and unbind the event. Then, the event loop has a chance to process the color change and process additional events. Once the time has elapsed, your function will be called and the event will be re-bound.
How come when I run my code, it will sleep for 3 seconds first, then execute the 'label' .lift() and change the text? This is just one function of many in the program. I want the label to read "Starting in 3...2...1..." and the numbers changing when a second has passed.
def predraw(self):
self.lost=False
self.lossmessage.lower()
self.countdown.lift()
self.dx=20
self.dy=0
self.delay=200
self.x=300
self.y=300
self.foodx=self.list[random.randint(0,29)]
self.foody=self.list[random.randint(0,29)]
self.fillcol='blue'
self.canvas['bg']='white'
self.lossmessage['text']='You lost! :('
self.score['text']=0
self.countdown['text']='Starting in...3'
time.sleep(1)
self.countdown['text']='Starting in...2'
time.sleep(1)
self.countdown['text']='Starting in...1'
time.sleep(1)
self.countdown.lower()
self.drawsnake()
It does this because changes in widgets only become visible when the UI enters the event loop. You aren't allowing the screen to update after calling sleep each time, so it appears that it's sleeping three seconds before changing anything.
A simple fix is to call self.update() immediately before calling time.sleep(1), though the better solution is to not call sleep at all. You could do something like this, for example:
self.after(1000, lambda: self.countdown.configure(text="Starting in...3"))
self.after(2000, lambda: self.countdown.configure(text="Starting in...2"))
self.after(3000, lambda: self.countdown.configure(text="Starting in...1"))
self.after(4000, self.drawsnake)
By using after in this manner, your GUI remains responsive during the wait time and you don't have to sprinkle in calls to update.
I am trying to displey a few labels for exact amount of time and than forget them. I tried with sleep() and time.sleep(), but the program started after time I have defined and than executes lines. Here is part of my program:
from time import sleep
from tkinter import*
from tkinter import ttk
root = Tk()
root.geometry('700x700+400+100')
root.overrideredirect(1)
myFrame=Frame(root)
label1=Label(myFrame, text='Warning!', font=('Arial Black', '26'), fg='red')
myFrame.pack()
label1.pack()
sleep(10)
myFrame.pack_forget()
label1.pack_forget()
But when I run the program it wait for 10 seconds and than executes the lines (frame and label are packed and than immediatly forget).
I hope it is clear, what problem I have.
Use the Tkinter after method instead of time.sleep(), as time.sleep() should almost never be used in a GUI. after schedules a function to be called after a specified time in milliseconds. You could implement it like this:
myFrame.after(10000, myFrame.pack_forget)
label1.after(10000,label1.pack_forget)
Note that after does not ensure a function will occur at precisely the right time, it only schedules it to occur after a certain amount of time. As a result of Tkinter being single-threaded, if your app is busy there may be a delay measurable in microseconds (most likely).
I have a Raspberry Pi with the Piface adaptor board. I have made a GUI which controls the LED's on the Piface board.
I wrote a small piece of code to make the LED's run up and down continuously, like Knight Riders car, using a While loop.
I then wrote another piece of code that created a GUI. In the GUI is a button that starts the LED's running up and down continuously with the While loop piece of code.
What I want to do is to have that GUI button start the LED running sequence, and then the same button stop the sequence at any time.
I do understand that the code is sitting/stuck in the While loop. And hence any buttons in the GUI are not going to have an effect.
So is there a better way of doing it? Any pointers would be appreciated.
Thanks in advance.
Another option is to run the LED while loop in a separate thread. Like in
the next code. The while loop is stopped by toggling the shared led_switch
variable.
"""
blinking LED
"""
import tkinter as tk
import threading
import time
led_switch=False
def start_stop():
global led_switch
led_switch=not led_switch
if led_switch:
t=threading.Thread(target=LED)
t.start()
def LED():
while led_switch:
print('LED on')
time.sleep(1)
print('LED off')
time.sleep(1)
root=tk.Tk()
button=tk.Button(root,command=lambda: start_stop(),text='start/stop')
button.pack()
tk.mainloop()
If you have a while loop and a GUI you can use generators to still use the loop and let the GUI run properly.
I sketch the Idea here and create an example for the Tkinter GUI.
You want to write your code as a loop and still use it in a GUI:
from Tkinter import *
from guiLoop import guiLoop # https://gist.github.com/niccokunzmann/8673951
#guiLoop
def led_blink(argument):
while 1:
print("LED on " + argument)
yield 0.5 # time to wait
print("LED off " + argument)
yield 0.5
t = Tk()
led_blink(t, 'shiny!') # run led_blink in this GUI
t.mainloop()
Output while the GUI is responsive:
LED on shiny!
LED off shiny!
LED on shiny!
LED off shiny!
...
Sadly Tkinter is the only GUI I know and it is a bad example because you can always update the GUI in your loop with the update() method of GUI elements:
root = Tk()
while 1:
print("LED on")
t = time.time() + 0.5
while t > time.time(): root.update()
print("LED off")
t = time.time() + 0.5
while t > time.time(): root.update()
But with such a guiLoop you can have multiple loops:
t = Tk()
led_blink(t, 'red')
led_blink(t, 'blue')
led_blink(t, 'green')
t.mainloop()
Here are some examples for starting and stopping the loop with a button.
If you're using Tkinter, there's a very easy pattern for running a loop. Given that the UI (in just about every UI toolkit) is already running an infinite loop to process events, you can leverage this to run code periodically.
Let's assume you have a python object "led" which has a method for toggling it on and off. You can have it switch from on to off every 100ms with something as simple as these three lines of code:
def blink(led):
led.toggle()
root.after(100, blink, led)
The above code will run forever, causing the led to blink every 100ms. If you want to be able to start and stop the blinking with a button, introduce a flag:
def blink(led):
if should_blink:
led.toggle()
root.after(100, blink, led)
When you set the toggle to True, the led will start blinking. When it's False, it will stop blinking.
The main thing to take away from this is that you already have an infinite loop running, so there's no need to create one of your own, and no need to use something as complex as threading. Simply create a function that does one frame of animation, or calls some function or does some unit of work, then have the function request that it be run again in the future. How far in the future defines how fast your animation or blink will run.