I'm writing a weather display program for my Raspberry Pi in python that fetches data using weather.com's api. As it stands, I have set it to sleep for 5 minutes after each main 'while' loop. This is because I don't want the Pi constantly using the wifi to fetch the same weather data. The problem with this is if I try to close or alter the program in any way, it waits to finish the time.sleep() function before continuing. I would like to add buttons to create a scroll menu but currently, the program will hang in that time.sleep() function before continuing. Is there an alternative I can use to delay the fetching of data while keeping program responsiveness?
You can do something like this:
import time, threading
def fetch_data():
# Add code here to fetch data from API.
threading.Timer(10, fetch_data).start()
fetch_data()
fetch_data method will be executed inside a thread so you wont have much problem. There is also a delay before calling the method. So you wont be bombarding the API.
Example source: Executing periodic actions in Python
Create a timer with python's time module
import time
timer = time.clock()
interval = 300 # Time in seconds, so 5 mins is 300s
# Loop
while True:
if timer > interval:
interval += 300 # Adds 5 mins
execute_API_fetch()
timer = time.clock()
Pygame has pygame.time.get_ticks() which you can use to check time and use it to execute function in mainloop.
import pygame
# - init -
pygame.init()
screen = pygame.display.set_mode((800, 600))
# - objects -
curr_time = pygame.time.get_ticks()
# first time check at once
check_time = curr_time
# - mainloop -
clock = pygame.time.Clock()
running = True
while running:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
# - updates -
curr_time = pygame.time.get_ticks()
if curr_time >= check_time:
print('time to check weather')
# TODO: run function or thread to check weather
# check again after 2000ms (2s)
check_time = curr_time + 2000
# - draws -
# empty
# - FPS -
clock.tick(30)
# - end -
pygame.quit()
BTW: if fetching web content takes more time then run it in thread.
Related
Im making a keyboard program that repeatedly presses a key every 0.1 seconds and uses esc as the hotkey to stop/start the program.
import keyboard
import time
keyboard.wait('esc')
name = ("python")
while name == "python":
keyboard.press_and_release('5')
time.sleep(0.1)
#this is where i want to add the timer
name = ("notpython")
I want to add the timer there so that after a few seconds the name variable changes from python to notpython and making the while loop false.
I've tried the time sleep function but it keeps printing 5 and doesnt stop.
maybe you can try something like this:
import keyboard
import time
keyboard.wait('esc')
name = ("python")
timer = time.time()
while name == "python":
keyboard.press_and_release('5')
time.sleep(0.1)
timer2 = time.time()
if timer2 - timer >= 5: name = "notpython"
I am working on this project with a motion sensor in which I would like to have the monitor turned off when there is no motion after a certain amount of time has passed. But every time there is a motion I would like the timer to reset.
I have the code working for turning the monitor on and off with motion, but how do I add the timer?
Any help will be appreciated. My code:
from gpiozero import MotionSensor
import time
from subprocess import call
pir = MotionSensor(4)
while True:
pir.wait_for_motion()
print("Screen On")
call(["/usr/bin/vcgencmd", "display_power", "1"])
time.sleep(30)
pir.wait_for_no_motion()
print("Screen Off")
call(["/usr/bin/vcgencmd", "display_power", "0"])
time.sleep(1)
As well as wait_for_motion(), gpiozero also provides a variable, motion_detected. The code below sets a variable, startpoint to the current time in seconds since 1/1/1970. It then starts a loop which:
Checks if motion is detected - if so, sets the startpoint variable back to the current time (which, of course, will be different to what it was previously) and turns on the display.
Checks if the startpoint variable is more than 30 seconds before the current time. Because every motion detection resets this variable, we know that there must have been at least 30 seconds since the last motion detection. If so, turns off the display.
startpoint = time.time()
while True:
if pir.motion_detected:
startpoint = time.time()
call(["/usr/bin/vcgencmd", "display_power", "1"])
print("Display on")
elif time.time() > (startpoint+30):
call(["/usr/bin/vcgencmd", "display_power", "0"])
print("Display off")
You could also use threading for this.
from enum import Enum
from gpiozero import MotionSensor
from subprocess import call
from threading import Timer
from time import sleep
from typing import Optional
pir = MotionSensor(4)
timer: Optional[Timer] = None
class MonitorState(Enum):
ON = "1"
OFF = "0"
def set_monitor_state(state: str):
call(["/usr/bin/vcgencmd", "display_power", state.value])
def start_timer():
global timer
if timer:
timer.cancel()
timer = Timer(30, set_monitor_state, (MonitorState.OFF,))
timer.start()
start_timer()
while True:
pir.wait_for_motion()
start_timer()
set_monitor_state(MonitorState.ON)
I'm not sure if the Timer actually counts as being done when the callback returns or before that. In the first case you could run into troubles when the set_monitor_state(MonitorState.ON) get called while the timer runs it's callback on another thread. You might want to use locking in this case.
I have written some Python code in an attempt to show one video after the other where I want to in Pygame:
import pygame
import time
def playvid(vidfile, runtime, FPS):
print("playing" + vidfile)
playmovie = pygame.movie.Movie(vidfile)
movie_screen = pygame.Surface(playmovie.get_size()).convert()
playmovie.set_display(movie_screen)
playmovie.play()
thentime = time.time()
playing = True
while playing:
screen.blit(movie_screen,(0,0))
pygame.display.update()
clock.tick(FPS)
nowtime = time.time()
#Play the video for 5s
if nowtime - thentime > runtime + 1: playing = False
print("done this one")
#FPS = 29.97
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((0, 0),pygame.FULLSCREEN)
run = True
while run:
playvid('snowdone.mpg', 10, 29.97)
playvid('snowdone1.mpg', 10, 29.97)
run = False
pygame.quit()
Playing the first video is fine, however, playing the second gives the warning:
"Runtime Error!
Program: C:\Python27\pythonw.exe
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information."
Both videos play fine on their own so this is not an mpg problem. Any suggestions?
I do not know the specific reason as to why the change I am suggesting works, but apparently it works.
Using your code as is, my first movie would not run for more than 4-5 seconds no matter what (it always stopped at roughly the same point) and then the pygame window would crash.
Adding a simple piece of pygame.event.get code in the while playing loop, however, fixed everything
while playing:
for event in pygame.event.get():
pass
screen.blit(movie_screen,(0,0))
pygame.display.update()
clock.tick(FPS)
nowtime = time.time()
#Play the video for 5s
if nowtime - thentime > runtime + 1: playing = False
It seems strange that even doing nothing in the loop for events runs the code without an issue.
I'm making an game for ending my programming course and I sometimes have some longer scenes displayed and the loop ain't checking for if there is QUIT event and then stop everything and close the loop. So I thought multi-threading would help, but apparently it doesn't seem to get events, I have tried printing out - it gets events, but it simply aint getting QUIT event.
Here is the code:
from pygame import *
from threading import Thread
def closesearcher():
global running
while running:
for i in event.get():
if i.type == QUIT:
print ("QUIT event in closesearcher")
running = False
quit()
exit()
clock.tick(60)
def main():
init()
#True while game is running
global running
running = True
global window
window = display.set_mode([640, 480])
global clock
clock = time.Clock()
#Let's start closesearcher
searcherclose = Thread(target = closesearcher)
searcherclose.start()
scenegame = gamescene()
def gamescene():
#Render some scenery, wait for user to press button, etc in the following loop
while running:
for i in event.get():
if i.type == QUIT:
print ("QUIT event in game loop")
clock.tick(60)
if __name__ == "__main__":
main()
Any solutions that would be similiar to the structure of my current code?
Note: I use Python 3.2
How about actually exiting the loops with return instead of only printing that you got the quit event?
Currently, you only exit the loop in closesearcher but not in gamescene.
Also, I think event.get() will remove the event. So calling get in the other loop will not return anything. You need to set a global flag playerWantsToQuit and use that instead of the local running variables.
I'm running it through the livewires wrapper which is just training wheels for pygame and other python modules in general; but everytime I run it, it'll execute and when I try to exit, it will not respond and then crash.
Any input on how I could go about fixing this would be great. There isn't any input in my textbook and all google seems to yield are results to this problem using pygame itself.
Apparently pygame and Tkinter seem to conflict?
Thanks in advance!
Addendum - This is the code I was trying to run:
from livewires import games
screen_w = 640
screen_h = 480
my_screen = games.Screen (wid, heit)
my_screen.mainloop()
Similar question: Pygame screen freezes when I close it
There isn't any input in my textbook
and all google seems to yield are
results to this problem using pygame
itself.
Those results probably address the same problem you're having. This is the relevant part of the games.py file from livewires, and nowhere does it call pygame.quit():
def handle_events (self):
"""
If you override this method in a subclass of the Screen
class, you can specify how to handle different kinds of
events. However you must handle the quit condition!
"""
events = pygame.event.get ()
for event in events:
if event.type == QUIT:
self.quit ()
elif event.type == KEYDOWN:
self.keypress (event.key)
elif event.type == MOUSEBUTTONUP:
self.mouse_up (event.pos, event.button-1)
elif event.type == MOUSEBUTTONDOWN:
self.mouse_down (event.pos, event.button-1)
def quit (self):
"""
Calling this method will stop the main loop from running and
make the graphics window disappear.
"""
self._exit = 1
def mainloop (self, fps = 50):
"""
Run the pygame main loop. This will animate the objects on the
screen and call their tick methods every tick.
fps -- target frame rate
"""
self._exit = 0
while not self._exit:
self._wait_frame (fps)
for object in self._objects:
if not object._static:
object._erase ()
object._dirty = 1
# Take a copy of the _objects list as it may get changed in place.
for object in self._objects [:]:
if object._tickable: object._tick ()
self.tick ()
if Screen.got_statics:
for object in self._objects:
if not object._static:
for o in object.overlapping_objects ():
if o._static and not o._dirty:
o._erase ()
o._dirty = 1
for object in self._objects:
if object._dirty:
object._draw ()
object._dirty = 0
self._update_display()
self.handle_events()
# Throw away any pending events.
pygame.event.get()
The QUIT event just sets a flag which drops you out of the while loop in the mainloop function. I'm guessing that if you find this file in your Python directory and stick a pygame.quit() after the last line in mainloop, it will solve your problem.
I agree. you need to put the whole program (the part to be executed, not the definitions and such) into a while loop.The problem is that pygame is not told to close when exited in IDLE, but outside of IDLE the closure of the program overrides the need to close pygame.
here is the loop:
done = False
while done==False:
# ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
#Main program here
pygame.quit()