Python Logo

Tipping at the Right Time with Python: Improved Timer


A couple of days ago I wrote a short tutorial on how to make a timer. The purpose of the timer was to remind us to open Publish0x, read and tip. Reactions and feedback led me to do a bit more development.

 

Timer Review: Assessing Functionalities

The timer was “only” a timer. It opened the Publish0x page after the time has passed. But that was it.

The previous article already hinted at some of its limitations like:

  1. can’t run multiple times: first tip, second tip, etc.;
  2. lacks interactivity: the page is opened, but it does not interact with the user;
  3. lacks sounds or similar advice when the time runs out: is non-beeping timer still a timer? ;)

Here we are using a quick-fix for 3: open a Youtube music video.

Dealing with 1 and 2 is more complex. Ideally we would want 1 to follow the times of the tipping. We need to pass our arguments to our timer, ideally those of the “tipping pauses”. * It would also be good to have some kind of pop-up that says: “Hey, did you read something”? We can have such 90s pop-up built with Tkinter. What’s better, we can use the pop-up to go through the list.

Say yes to “did you read and tip” and you will reset the timer to the following time element in the tipping list. Say no to that and you’ll quit the program. Ok, time to code.

 

Improved Timer

I’ve tried to reuse most of the previous timer. I’ve dried up some of the comments of the previous version.

(Some comments on the code will follow.)

[As you can see from the comments, Smoljanović was so kind  to report some visualization issues with dark mode on Iphone. You can't see the code below scroll down until you see a "Timer Code (for Dark Mode Issues)" in which we are experimenting solutions. You'll see the comments before the actual code then.]


# same imports as before import time import datetime as dt import webbrowser #new import for the message board import tkinter from tkinter import messagebox #list of intervals for the timer TIPPING_TIMES = [1, 6, 24, 60, 60*8] #open Tkinter for the message popup window = tkinter.Tk() #this will remove the Tkinter window from sight window.withdraw() #define what we want to happen once time is over def openyoutubeandpublish0x(): # this will open Azure's Redtail because it's good and I like it webbrowser.open_new_tab('https://www.youtube.com/watch?v=A_KPau0z5_U') webbrowser.open_new_tab('https://www.publish0x.com/') #old timer with new end-of-time operations #print statements removed def timecounter(tempo): now = dt.datetime.now() timer = dt.timedelta(0,tempo*60) #timedelta takes second so we multiply tempo by 60 future = now + timer while True: #this loop will go on forever if now > future: #this means timer is over, so we run our dosomething function openyoutubeandpublish0x() break else: #if the timer is not over it is still running. #we have to wait for some time to pass and then update our actual time time.sleep(60) #we are checking our time every minute. Feel free to change this now = dt.datetime.now() #we are updating the 'now' we check against future #main loop #initialize a counter to run during tipping times counter = 0 while counter < len(TIPPING_TIMES): timecounter(TIPPING_TIMES[counter]) #now we break the loop after the message appears chech_search_performance = messagebox.askyesno("Time passed!","Did you read&tip?") if chech_search_performance == True: #print(timer) counter = counter + 1 continue elif chech_search_performance == False: messagebox.showinfo("Hope you've read something cool") break

 

 

Code Comments

The logic of the program follows what we said above. Here a few comments:

  • we have a list of TIPPING_TIMES (feel free to change the times or add more elements);
  • the main loop is a while loop on the TIPPING_TIMES list that tracks where we are inside the item list using a counter. We run the timer with the corresponding items in the list. (That’s TIPPING_TIME[counter]);
  • we turn the list into the number of its items with the len() function;
  • Python lists are zero index (that’s why we initialized the counter with 0);
  • Tkinter will build our interface with minimal effort. We are relying on the two functions: messagebox.askyesno and messagebox.showinfo.
  • If you say yes the counter is updated, i.e. you run the timer with the next item in the TIPPING_TIME, otherwise you quit the program.
  • with window.withdraw() we remove the Tkinter window that would otherwise be popping up (we are using Tkinter only for the messageboxes).

 

Improving this Version of the Timer

  • What if you open a random part of Red Tail each time?
  • Red Tail is a particularly long song. It need to buffer. Are there other ways to have sounds and beeps as time is over?
  • Azure’s Red Tail is so long that after the first calls it will be still playing, unless you closed it.
  • You still need to run the program everytime.
  • The landing page is the same everytime, maybe you’d like to change it.

 

Ok, that was it. Let me know if it works and if there's something more I may add to it. 

 

Timer Code (for Dark Mode Issues)

# same imports as before
import time
import datetime as dt
import webbrowser

#new import for the message board
import tkinter
from tkinter import messagebox

#list of intervals for the timer
TIPPING_TIMES = [1, 6, 24, 60, 60*8]
#open Tkinter for the message popup
window = tkinter.Tk()
#this will remove the Tkinter window from sight
window.withdraw()

#define what we want to happen once time is over
def openyoutubeandpublish0x():
# this will open Azure's Redtail because it's good and I like it
    webbrowser.open_new_tab('https://www.youtube.com/watch?v=A_KPau0z5_U')
    webbrowser.open_new_tab('https://www.publish0x.com/')

#old timer with new end-of-time operations
#print statements removed
def timecounter(tempo):
    now = dt.datetime.now()
    timer = dt.timedelta(0,tempo*60) #timedelta takes second so we multiply tempo by 60
    future = now + timer

    while True: #this loop will go on forever
        if now > future:
            #this means timer is over, so we run our dosomething function
            openyoutubeandpublish0x()
            break
        else:
#if the timer is not over it is still running.
#we have to wait for some time to pass and then update our actual time
            time.sleep(60) #we are checking our time every minute. Feel free to change this
            now = dt.datetime.now() #we are updating the 'now' we check against future

#main loop
#initialize a counter to run during tipping times
counter = 0

while counter < len(TIPPING_TIMES):
    timecounter(TIPPING_TIMES[counter])
#now we break the loop after the message appears
    chech_search_performance = messagebox.askyesno("Time passed!","Did you read&tip?")
    if chech_search_performance == True:
#print(timer)
        counter = counter + 1
        continue
    elif chech_search_performance == False:
        messagebox.showinfo("Hope you've read something cool")
        break

 

 

 

 

 

 

How do you rate this article?

7



Programming, Blockchain and Stuff
Programming, Blockchain and Stuff

Writing about programming, Digital Humanities and the blockchain.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.