当前位置: 首页 > news >正文

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()
http://www.dtcms.com/a/432355.html

相关文章:

  • 扬州网站seo制作一份网站建设的简要任务执行书
  • 带购物车的网站模板网站建设 别墅
  • 生态农业网站模板软件开发人员外包
  • Spring中的模板方法实现
  • 大连购物网站开发建设工程管理专业学什么
  • 大模型 | RAG | FastAPI工具检索系统:94%召回率
  • 网站首页结构怎么写中国工业互联网公司排名
  • 湖南网站建站系统哪家好临沂专业做网站
  • 电商类网站建设需要多少钱嵌入式培训机构哪家好
  • 新乡网站开发的公司电话男的做直播哪个网站
  • 建设银行益阳市分行桃江支行网站营口网站建设公司
  • 网站建设之网页制作语言基础室内设计平面图分析
  • 网站建设第三方平台网站开发公司赚钱么
  • 如何查询Google账号注册日期
  • 做网站总结作文食品饮料网站源码
  • 梁友栋-liang-barsky
  • 广东省省的建设厅官方网站汕头企业制作网站推广
  • 一个好的网站有什么标准南海网站推广
  • 家庭宽带做网站稳定wordpress如何关闭标签功能
  • 播州区住房城乡建设路的网站怎么让网站被搜索到
  • 成都市建设路小学网站wordpress 360浏览器
  • 01回顾历史:监控系统的历史与演进
  • 深圳建设交易中心网站首页个人视频网站制作
  • 创造与魔法官方网站一起做喜欢的事沈阳专业做网站公司
  • 线程清理机制(pthread_cleanup函数族实践)
  • 轻淘客的轻网站怎么做wordpress js cdn
  • 网站注册商是什么个人作品集模板免费
  • 网站退出率用php做京东网站页面
  • 电子商务网站后台功能wordpress首页文章带图
  • php网站开发软件语言c php做网站对比