How to add additional seconds to a microwave timer? - python

I am at a very beginner level with Python and I have a simple project for myself. My goal is to build a simple timer with only two functions using tkinter and python. The first button starts the countdown timer at 30 seconds. And the second button stops it. However, I would like each additional press of the first button to add an additional 30 seconds to whatever time is currently left on the countdown clock. The first two elements have proven not too difficult using this prior thread, but adding additional time to the active timer is something I cannot reach.
Essentially, the goal is to recreate the functionality of the "+30 seconds" button on a microwave, with an updating display, where the initial press both adds 30 seconds and starts the timer, each subsequent press adds an additional 30 seconds to the countdown timer, and the second button stops the timer.
Here's what I've been using for the basic timer logic.
def countdown(t=30):
while t:
mins, secs = divmod(t, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
print(timer, end="\r")
time.sleep(1)
t -= 1
print('Done!')

The problem is a little bit more complex than it appears for the following reasons:
If you are not using any library for parallel events, then, the function countdown() will block the main loop and you will not be able to do anything (including click the button again to add more time) while the countdown is running
If you want to add time with the same button, you have to check if there's already a countdown print on screen, if you don't check it, for each click a new clock will appear.
I suggest to use asyncio lib as follow:
import asyncio
# Create a global variable for the time
secs = 0
#This function adds +30secs
def addTime():
global secs
secs+=30
#This is the func that gather the addTime function and the async clock
async def timerAction():
#This "while" is used to simulate multiple cliks on your button.
# If you implement this as a callback delete the "while"
while True:
j = 0 #Counter if a clock is already running
addTime() #Add time
for task in asyncio.all_tasks(): #This loop checks if there's a countdown already running
if task.get_name()=='countdown':
j+=1
if j == 0: #If no running countdown, j==0, and fires the countdown
taskCountdown = asyncio.create_task(countdown(), name='countdown')
await asyncio.sleep(10) #Wait 10secs between each call to the func (each call simulate a click)
async def countdown():
global secs
while secs>0:
mins, secs = divmod(secs, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
print(timer, end="\r")
await asyncio.sleep(1)
secs -= 1
print('Done!')
asyncio.run(timerAction())
Output:
On the fisrt call:
00:30
after 10secs:
00:50
after 10secs:
01:10
and so on...

Related

Let python script still receive input while in time.sleep

What I want to achieve here is that when I press * the program should wait for 17 seconds before doing anything else and if I don't press anything it can continue pressing the ] key every 11 seconds. The problem here is that if I were to press the * while we are in any time.sleep period, the press of the key will not go through.
import pyautogui
import time
import keyboard
while not keyboard.is_pressed('*'):
if keyboard.is_pressed('*'):
time.sleep(17)
else:
time.sleep(11)
pyautogui.press(']')
There are a lot of ways to accomplish this.
One of the simpler (but certainly not best) ways is to sleep in shorter increments (say 0.1 sec) to wake up periodically and poll the keyboard. You would track in a variable the system time at which point you are done "waiting" and break out of your loop at that point.
This works for me
import pyautogui
import time
import keyboard
last_star_press = 0 # initialize variable to keep track of time of last * press
while True:
if keyboard.is_pressed('*'):
last_star_press = time.time() # update time of last * press
time.sleep(17)
else:
if time.time() - last_star_press >= 11:
pyautogui.press(']')
last_star_press = time.time() # update time of last key press
time.sleep(1) # add a small delay to reduce CPU usage

To stop performing a function after this requirement is satisfied

This is my code
It performs keep_click_attendance_link() enter code here function until JustBefore time is reached
and then at EndTime it performs the leave_the_meeting() function
import pyautogui
import time
import pause
import datetime
import schedule
YEAR = 2020
MONTH = 11
DATE = 6
HOUR = 13
MINUTES = 3
SECONDS = 15
now = datetime.datetime.now()
EndTime = now.replace(hour=HOUR, minute=MINUTES, second=SECONDS, microsecond=0)
JustBefore= now.replace(hour=HOUR, minute=MINUTES-1, second=SECONDS, microsecond=0)
def leave_the_meeting():
pyautogui.click(1198, 1072)
time.sleep(3)
pyautogui.click(1443, 998)
time.sleep(1)
pyautogui.click(1398, 933)
def click_attendance_link():
pyautogui.click(1665, 674)
time.sleep(9)
def keep_click_attendance_link():
while datetime.datetime.now() < JustBefore:
click_attendance_link()
# Sleep for 60 secs before trying again
time.sleep(9)
keep_click_attendance_link()
while datetime.datetime.now() < EndTime:
# Sleep for 1 sec intervals:
time.sleep(1)
# eventually Leave the meeting at Endtime
leave_the_meeting()
So what I want is it to stop the function keep_click_attendance_link() when the attendance link is clicked. The teacher sends the link at any random time, so I had to program it to continuously click at that spot until 1 minute before the EndTime which is JustBefore. The meeting is on zoom client not on the web browser.
First of all, don't skip lesson.
Secondly, to do what you intend to do, for educational purpose only, you will need a mechanism to detect whether the button has appeared yet. Since you can't directly communicate with the zoom client, you will need to detect pixel by pixel in the screen and analyze whether the color of that pixel/area is the color for the attendance button.
To do that, you will need to find the color code for your button first. It can be simply done by screen capture and put that in any image processing software, even Paint. After you have your color, you can use Pillow module to analyze that particular spot, more on that here.
After the checking mechanism is done, you can integrate that into your code and stop the function execution once the button has been detected.
def check_attendance_link_presence():
# check it here
pass
def keep_click_attendance_link():
clicked = False
while datetime.datetime.now() < JustBefore and not clicked:
presence = check_attendance_link_presence()
if presence:
click_attendance_link()
clicked = True
# Sleep for 60 secs before trying again
time.sleep(60)
p.s. I intentionally not to include the code for the presence checking here for the sake of encouraging you to spend time learning it while you are "taking a break" from your lessons.
Maybe: Define a variable something like clicked = False, and in the attendance function set it to True. Then modify the While in the other function with an and clicked == False. So your while cycle only loops till the clicked flag is False, but after it clicked the attendance, it'll change to True, and the cycle stops.
I think it would be more elegant with callbacks tough.

Is it possible to have my program draw a shape with pygame, wait a second, and then draw another? But, I still want the rest of the program to run

I trying to make a rhythm game for my final project. I'm using pygame, and I want my program to draw a shape, wait a second, and then draw another, in accordance with the music I'm playing. Is it possible for my program to wait a second between drawing each shape, but have the rest of the program still runs? (not pygame.time.delay())
I've tried http://fredericiana.com/2014/11/14/settimeout-python-delay/ which either didn't work, or I failed to implement it correctly
def spawnShapesGameOne(gameInPlay, gameInPlayOne,drawShapesOne):
if gameInPlay == True:
if drawShapesOne == True:
pygame.draw.rect(surface, GREEN,(w*.23, h*.25, w*.05,w*.05))
#Wait one second
pygame.draw.rect(surface, GREEN,(w*.73, h*.25, w*.05,w*.05))
#Wait one second
pygame.draw.rect(surface, GREEN,(w*.73, h*.65, w*.05,w*.05))
#Wait one second
pygame.draw.rect(surface, GREEN,(w*.23, h*.65, w*.05,w*.05))
you can do this non-blocking delay / schedule in general by using time module or any timer:
Note: I moved reset timer to main loop
import time
timer1sec = 0
def draw_myshape():
# your drawing code here
# main pygame loop
while True:
if time.time() - timer1sec >= 1: # if one seconds passed
draw_myshape()
timer1sec = time.time() # reset our timer
# rest of pygame code here

Trouble with root.after

I am making a GUI application in tkinter and I need the code in a function to run every 60 seconds. I have tried using the root.after(root, function) pause, but using this, my GUI window froze and the code in the function continued to run as I was interrupting the root.mainloop(). Here is a bit of the code with which I am having problems:
def start():
global go
go=1
while go == 1:
getclass()
root.after(1000,func=None)
def stop():
global go
go=0
def getclass():
#my code here
print("Hello World")
I just want getclass() to run every 60 seconds when the start button is pushed. Then I want this 60 second loop to stop when the stop button is pushed.
Thank you in advance,
Sean
You are overthinking this: What is needed is very simple, maybe like this:
def start():
getclass()
root.after(60000, start) # <-- 60,000 milliseconds = 60 seconds
When start() gets called, it executes a call to getClass(), then sets a callback that will call itself again in 60 seconds.
Minimal app demonstrating the use:
import tkinter as tk
def getclass():
print('hello')
def start():
getclass()
root.after(60000, start) # <-- 60,000 milliseconds = 60 seconds
root = tk.Tk()
start()
root.mainloop()

Trying to make a countdown with a twist

I'm trying to make a countdown timer like little script. I want it to look like this:
0 years left until your event.
0 months left until your event.
10 days left until your event.
234 hours left until your event.
14020 minutes left until your event.
841166 seconds left until your event.
As you can see it's not a "normal" countdown. I want it to be like this, total amount of seconds, total amount of minutes left etc. Now I'm running into trouble trying to make it actually count-down. Just the seconds are working fine, I'm using this piece of code for that:
def secondsLeft(eventDate):
while secondsUntil:
toPrint = "%s seconds left until your event." %(secondsUntil)
print(toPrint, end='\r')
time.sleep(1)
secondsUntil -= 1
However, when trying to do the same for the minutes, replacing with time.sleep(60) it's not printing out the seconds anymore!
I think it has to do with the while loop. However, I don't know what to do about it... Any help in the right direction is appreciated!!
If you wait a minute the whole program will be stopped for a minute. It can't decrease the seconds. What you want to do is, keep waiting only a second per while interation and add this line mins, secs = divmod(secondsUntil, 60). You can print mins for the minutesUntil

Categories

Resources