TIL Python Basics Day 28 - Dynamic Typing/ Pomodoro App

이다연·2021년 1월 11일
0

Udemy Python Course

목록 보기
28/64

Dynamic Typing

Python is dynamically typed. link

Python is unique.
-Python is strongly typed; it holds on to the data type of the variable. when you assign a value, it knows that it's a string, int or float. if you are trying to do something that's not meant for a type of value, it's gonna print 'type error'.

-Python is dynamically typed; it allows you to change the data type of variable, just by assigning it to different type of value(changing the content). Other languange doesn't allow it. (maintenance, test, reuse, planning time)

-Opposite is Static typed programming languages. (fast, strict, team)

a =3  -> int
a= "Hi"   ->changed to string

Dynamic typing allows the code below works, count_sec was int and it works inside string.

    if count_sec < 10:
        count_sec = f"0{count_sec}"

Project: Pomodoro

Purpose: Make a Tkinter program for focus time management. Pomodoro method runs 25 min work, 5 min short break for 4 times then give 25min long break to enhance the productivity.

Tkinter Canvas Widget Explained

Canvas

creating canvas object, enables to lay images on top of each other, overlaping.

canvas = Canvas(width=200, height=224, bg=YELLOW, highlightthickness=0) 
canvas.create_image(100, 112, image=tomato_img)

Text

create text. modify: canvas.itemconfig(variable_name, attribute)

timer_text = canvas.create_text(103, 138, text="00:00", fill="white", font=(FONT_NAME, 35, "bold"))
canvas.grid(column=1,row=1)
canvas.itemconfig(timer_text, text="00:00")
  • to remove the ugly white line wrapping around the button and label
start_button = Button(text="Start", highlightthickness=0, command=start_timer)
  • fg stands for foreground. to change the text colour
if reps % 8 == 0:
        count_down(long_break_sec)
        title.config(text="Break", fg=RED)
  • Layout guide

Countdown Mechanism

1

  • Event driven : GUI keeps checking on the events and when it detects the event(click etc), it's ready to react.'window.mainloop()' enables it. It loops through every millisecond to check a new event.
    -if we use another 'while loop' inside 'window.mainloop', it won't be able to reach the mainloop and it won't even launch the app.
window.mainloop()

2

  • .after()
    Built-in method. It executes a command after a time delay; it makes the timer function in pomodoro app.
    ms: in milleseconds. 1sec = 1000 milliseconds
    window.after(1000, count_down, count - 1)
    -> wait for 1000 milliseconds (1sec), call function count_down(function itself), passing in count(argument) -1

3

  • Function recursion
    to loop through a function without having to use while loop, we can use recursion of a function. Function calling itself inside the execution part. Basically looping through it. It has to have 'if statement' as a flag.
    e.g. count_down function inside .after( count_down )
def count_down(count):
    if count > 0:
        global timer
        timer = window.after(1000, count_down, count - 1)

timer is global variable holds None, to be used in another part.
if count > 0:
global timer
timer = window.after(1000, count_down, count - 1)

4

  • Format the time
    math module, floor returns largest whole number that is <= x
    e.g. 4.8 -> 4
    seconds: using modulo
count_min = math.floor(count / 60) 
    count_sec = count % 60
    if count_sec < 10:
        count_sec = f"0{count_sec}"
    canvas.itemconfig(timer_text, text=f"{count_min}:{count_sec}")

5

  • Count marks
    Every two reps, tick marks every even number reps (Work-Break) 2nd, 4th, 6th ... math.floor(reps/2)
def count_down(count):
    else:
        start_timer()
        mark = " "
        work_sessions = math.floor(reps/2)
        for i in range(work_sessions):
            mark += "✓"
        tick.config(text=mark)

Start Timer

is responsible for calling countdown function. We have three timers; short break, long break and work

def start_timer():
    global reps
    reps += 1
    work_sec = WORK_MIN * 60
    short_break_sec = SHORT_BREAK_MIN * 60
    long_break_sec = LONG_BREAK_MIN * 60
    if reps % 8 == 0:
        count_down(long_break_sec)
        title.config(text="Break", fg=RED)
    elif reps % 2 == 0:
        count_down(short_break_sec)
        title.config(text="Break", fg=PINK)
    else:
        count_down(work_sec)
        title.config(text="Work")
  • Reset Timer
    -after_cancel(): cancel waits, that we set previously.
    -We made variable 'timer' as Global to be able to use it in both function 'reset_timer()' and 'start_timer()' function, but we assigned None to it.
timer = None
timer = window.after(1000, count_down, count - 1)
def reset_timer():
    window.after_cancel(timer)

Angela's solution


from tkinter import *
import math
# ---------------------------- CONSTANTS ------------------------------- #
PINK = "#e2979c"
RED = "#e7305b"
GREEN = "#9bdeac"
YELLOW = "#f7f5dd"
FONT_NAME = "Courier"
WORK_MIN = 25
SHORT_BREAK_MIN = 5
LONG_BREAK_MIN = 20
reps = 0
timer = None

# ---------------------------- TIMER RESET ------------------------------- # 
def reset_timer():
    window.after_cancel(timer)
    canvas.itemconfig(timer_text, text="00:00")
    title.config(text="Timer")
    tick.config(text="")

# ---------------------------- TIMER MECHANISM ------------------------------- # 

def start_timer():
    global reps
    reps += 1
    work_sec = WORK_MIN * 60
    short_break_sec = SHORT_BREAK_MIN * 60
    long_break_sec = LONG_BREAK_MIN * 60

    if reps % 8 == 0:
        count_down(long_break_sec)
        title.config(text="Break", fg=RED)

    elif reps % 2 == 0:
        count_down(short_break_sec)
        title.config(text="Break", fg=PINK)
    else:
        count_down(work_sec)
        title.config(text="Work")


# ---------------------------- COUNTDOWN MECHANISM ------------------------------- #
def count_down(count):

    count_min = math.floor(count / 60) 
    count_sec = count % 60

    if count_sec < 10:
        count_sec = f"0{count_sec}"
    canvas.itemconfig(timer_text, text=f"{count_min}:{count_sec}")
    if count > 0:
        global timer
        timer = window.after(1000, count_down, count - 1)
    else:
        start_timer()
        mark = " "
        work_sessions = math.floor(reps/2)
        for i in range(work_sessions):
            mark += "✓"
        tick.config(text=mark)



# ---------------------------- UI SETUP ------------------------------- #

window = Tk()
window.title("Pomodoro")
window.config(padx=100, pady=50, bg=YELLOW)



title = Label(text="Timer", font=(FONT_NAME, 32, "bold"), fg=GREEN, bg=YELLOW)
title.grid(column=1,row=0)


canvas = Canvas(width=200, height=224, bg=YELLOW, highlightthickness=0) 
tomato_img = PhotoImage(file="tomato.png")
canvas.create_image(100, 112, image=tomato_img)
timer_text = canvas.create_text(103, 138, text="00:00", fill="white", font=(FONT_NAME, 35, "bold"))
canvas.grid(column=1,row=1)



start_button = Button(text="Start", highlightthickness=0, command=start_timer)
start_button.grid(column=0,row=2)

reset_button = Button(text="Reset", highlightthickness=0, command=reset_timer)
reset_button.grid(column=2,row=2)

tick = Label(font=(FONT_NAME, 12, "bold"), fg=GREEN, bg=YELLOW)
tick.grid(column=1,row=3)


window.mainloop()
profile
Dayeon Lee | Django & Python Web Developer

1개의 댓글

comment-user-thumbnail
2024년 3월 27일

That's great to hear that you learned about Python Basics, specifically Dynamic Typing and smash karts, on Day 28! Dynamic typing is a feature of Python where variables can hold values of different types, and their types can be changed during runtime.

답글 달기