Day 28 - 番茄钟应用开发 - Python学习笔记
Udemy - 100 Days of Code: The Complete Python Pro Bootcamp
Day 28 - Intermediate - Building a Pomodoro App
目录
- 214. Canvas Widget
- 215. Complete the UI
- 216. Add a Count Down Mechanism
- 217. Strong and Dynamic Typing
- 218. Set Different Timer Sessions
- 219. Add Checkmarks and Reset Timer
- 220. Complete Pomodoro App Project

214. Canvas Widget
Canvas Widget
import tkinter as tkTWO_POINTS = (100, 100, 200, 200)
THREE_POINTS = (100, 100, 100, 200, 200, 200)
FONT = ("Courier", 15, "normal")window = tk.Tk()canvas = tk.Canvas(width=400, height=400, bg="white")
canvas.pack()# 创建对象
canvas.create_rectangle(TWO_POINTS, fill="red") # 创建矩形
canvas.create_oval(TWO_POINTS, fill="orange") # 创建椭圆形
canvas.create_arc(TWO_POINTS, fill="yellow") # 创建扇形line = canvas.create_line(THREE_POINTS, fill="green", width=5) # 创建线条
poly = canvas.create_polygon(THREE_POINTS, fill="blue") # 创建多边形img = tk.PhotoImage(file="logo.png")
canvas.create_image(150, 300, image=img) # 创建图像
canvas.create_text(150, 300, text="Hello", font=FONT) # 创建文字# 修改对象
canvas.itemconfig(poly, fill="purple") # 修改属性
canvas.coords(poly, 100, 100, 150, 200, 200, 150) # 修改坐标
canvas.move(poly, 100, 100) # 平移
canvas.scale(poly, 200, 200, 1.5, 1.5) # 缩放# 删除对象
canvas.delete(line)window.mainloop()
215. Complete the UI
from tkinter import *PINK = "#e2979c"
RED = "#e7305b"
GREEN = "#9bdeac"
YELLOW = "#f7f5dd"
TIMER_FONT = ("Courier", 35, "bold")
TITLE_FONT = ("Courier", 50)# UI SETUP
window = Tk()
window.title("Pomodoro")
window.config(padx=100, pady=50, bg=YELLOW)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(100, 130, text="00:00", fill="white", font=TIMER_FONT)
canvas.grid(column=1, row=1)# Labels
title_label = Label(text="Timer", fg=GREEN, bg=YELLOW, font=TITLE_FONT)
title_label.grid(column=1, row=0)
check_marks = Label(text="", fg=GREEN, bg=YELLOW)
check_marks.grid(column=1, row=3) # Buttons
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) window.mainloop()
216. Add a Count Down Mechanism
after
- 延迟执行:
id = after(ms, func=None, *args)
- 取消延迟任务:
after_cancel(id)
- 主事件循环空闲时执行:
id = after_idle(func, *args)
- 查询任务信息:
after_info(id=None)
def start_timer(): count_down(5 * 60) def count_down(count): count_min = count // 60 count_sec = count % 60 canvas.itemconfig(timer_text, text=f"{count_min}:{count_sec}") if count > 0: window.after(1000, count_down, count-1)
217. Strong and Dynamic Typing
Python is a strongly, dynamically typed language.
- Strong Typing 强类型:不同类型之间不会发生隐式类型转换
- Weak Typing 弱类型:不同类型之间会自动进行隐式类型转换
print(3 + int("4"))
- Static Typing 静态类型:变量类型在编译时就确定,之后不能改变
- Dynamic Typing 动态类型:变量类型在运行时才确定,可以随赋值改变
count_sec = count % 60 # int type
if count_sec < 10:count_sec = f"0{count_sec}" # str type
218. Set Different Timer Sessions
WORK_MIN = 25
SHORT_BREAK_MIN = 5
LONG_BREAK_MIN = 20
reps = 0
timer = Nonedef start_timer(): if timer is not None: return 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: # 8th rep count_down(long_break_sec) title_label.config(text="Break", fg=RED) elif reps % 2 == 0: # 2nd/4th/6th rep count_down(short_break_sec) title_label.config(text="Break", fg=PINK) else: # 1st/3rd/5th/7th rep count_down(work_sec) title_label.config(text="Work", fg=GREEN)def count_down(count): count_min = 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: timer = Nonestart_timer()
219. Add Checkmarks and Reset Timer
def reset_timer(): global reps, timer window.after_cancel(timer) canvas.itemconfig(timer_text, text="00:00") title_label.config(text="Timer", fg=GREEN) check_marks.config(text="") reps = 0 timer = Nonedef count_down(count): ... if count > 0: global timer timer = window.after(1000, count_down, count-1)else: timer = Nonestart_timer() # Add Checkmarks marks = ["✔" for _ in range(reps // 2)] check_marks.config(text="".join(marks))
220. Complete Pomodoro App Project
from tkinter import *PINK = "#e2979c"
RED = "#e7305b"
GREEN = "#9bdeac"
YELLOW = "#f7f5dd"
TIMER_FONT = ("Courier", 35, "bold")
TITLE_FONT = ("Courier", 50)
WORK_MIN = 25
SHORT_BREAK_MIN = 5
LONG_BREAK_MIN = 20
reps = 0
timer = Nonedef reset_timer():global reps, timerwindow.after_cancel(timer)canvas.itemconfig(timer_text, text="00:00")title_label.config(text="Timer", fg=GREEN)check_marks.config(text="")reps = 0timer = Nonedef start_timer():if timer is not None:returnglobal repsreps += 1work_sec = WORK_MIN * 60short_break_sec = SHORT_BREAK_MIN * 60long_break_sec = LONG_BREAK_MIN * 60if reps % 8 == 0: # 8th repcount_down(long_break_sec)title_label.config(text="Break", fg=RED)elif reps % 2 == 0: # 2nd/4th/6th repcount_down(short_break_sec)title_label.config(text="Break", fg=PINK)else: # 1st/3rd/5th/7th repcount_down(work_sec)title_label.config(text="Work", fg=GREEN)def count_down(count):count_min = count // 60count_sec = count % 60if count_sec < 10:count_sec = f"0{count_sec}"canvas.itemconfig(timer_text, text=f"{count_min}:{count_sec}")if count > 0:global timertimer = window.after(1000, count_down, count-1)else:timer = Nonestart_timer()# Add Checkmarksmarks = ["✔" for _ in range(reps // 2)]check_marks.config(text="".join(marks))# UI SETUP
window = Tk()
window.title("Pomodoro")
window.config(padx=100, pady=50, bg=YELLOW)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(100, 130, text="00:00", fill="white", font=TIMER_FONT)
canvas.grid(column=1, row=1)# Labels
title_label = Label(text="Timer", fg=GREEN, bg=YELLOW, font=TITLE_FONT)
title_label.grid(column=1, row=0)
check_marks = Label(text="", fg=GREEN, bg=YELLOW)
check_marks.grid(column=1, row=3)# Buttons
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)window.mainloop()