Python 入门学习(壹)上机时间提醒
2018-02-11 14:23
435 查看
- 基于 Udacity 的 Python 入门课程 Programming Foundations with Python
- 基于 Python 2.7
控制台版本
思路
Project 1 是一个时间提醒助手,从打开程序开始计时,每两个小时打开一个 Youtube 上的视频链接。我想也许能够做一个更完善的版本——以开机时间为原点,计算上机的时长,每两个小时弹出一个 MessageBox 提示休息。
代码
import time import os from datetime import datetime import psutil import sys from time import sleep import Tkinter as tk import tkMessageBox count = 0 while 1: os.system('cls') #清屏实现动态显示 bTime = datetime.fromtimestamp(psutil.boot_time()) #获取开机时间 print "Boot Time:", #不换行输出 print bTime cTime = datetime.now() #获取当前时间 print "Current Time:", print cTime dTime = (cTime - bTime) #计算用机时长 print "Running Seconds:", print dTime.seconds, print "s" time = int(dTime.seconds) if (time/7200 > count): tkMessageBox.showinfo(title = 'Notice', message = 'You Should Have a Rest') #弹出提示框 count+=1 sleep(1) if __name__=="__main__": main()
效果
GUI 版本
思路
既然都用上了 Tkinter,何不直接来一个 GUI 界面呢。不过如果是 Tkinter 的话,代码就需要完全重构了。
代码
#coding=utf-8 import os from datetime import datetime import psutil import sys from time import sleep from Tkinter import * import tkMessageBox import time def tick(): global time1 # 获取当前系统时间 time2 = time.strftime('%H:%M:%S') # 动态显示 if time2 != time1: time1 = time2 clock.config(text=time2) clock.after(200, tick) def timer(): global dTime1 bTime = datetime.fromtimestamp(psutil.boot_time()) cTime = datetime.now() dTime2 = (cTime - bTime).seconds if dTime2 != dTime1: dTime1 = dTime2 runn.config(text=str(dTime2)+' s') runn.after(800, timer) def msg(): global count if (dTime1/7200 > count): tkMessageBox.showinfo(title = 'Notice', message = 'You Should Have a Rest Now') count+=1 tips.config(text="累计提示次数:"+str(count)) tips.after(800, msg) root = Tk() #标题栏 root.title('Tik') #窗体尺寸 #root.geometry('200x100') #时钟 nclock = Label(root, text="当前时间", font=('DengXian', 15, 'bold'), relief=GROOVE) nclock.grid(row=0, column=0) time1 = '' clock = Label(root, font=('DengXian', 15, 'bold')) clock.grid(row=0, column=1) tick() #开机时间 nboot = Label(root, text="开机时间", font=('DengXian', 15, 'bold'), relief=GROOVE) nboot.grid(row=1, column=0) boottime = datetime.fromtimestamp(psutil.boot_time()) boottime = boottime.strftime('%H:%M:%S') boot = Label(root, text=boottime, font=('DengXian', 15, 'bold')) boot.grid(row=1, column=1) #运行时长 nrun = Label(root, text="运行时长", font=('DengXian', 15, 'bold'), relief=GROOVE) nrun.grid(row=2, column=0) dTime1 = '' runn = Label(root, font=('DengXian', 15, 'bold')) runn.grid(row=2, column=1) timer() #运行状态 status = Label(root, text=" [ Running ] ", fg = 'green', bg = 'black', font=('DengXian', 10, 'bold')) status.grid(row=10, column=0) count = 0 tips = Label(root, fg = 'green', bg = 'black', font=('DengXian', 10, 'bold')) tips.grid(row=10, column=1) msg() root.mainloop()
效果
相关文章推荐
- Python 学习入门(10)—— 时间
- python学习笔记--Django入门一 网页显示时间
- Python 学习入门(10)—— 时间
- python的GUI编程--wxpython学习(四)-->练习:一个自动测试app启动时间的小工具
- 零基础入门学习Python(18):对象(3)继承
- Python 学习入门(7)—— lambda
- Python学习入门基础教程(learning Python)--6.3 Python的list切片高级
- Python 学习入门(28)—— 服务器实例
- Python学习笔记 --- python求昨天的时间
- 零基础入门学习Python(27):模块和包
- Python学习笔记9:标准库之日期时间(time包,datetime包)
- Caffe深度学习入门——Ubuntu16.04 + CUDA9.1+cudnn7.5 + opencv3.3.1+python3.5+caffe安装参考博客和跳坑笔记
- Python装饰器学习(九步入门)
- Python学习入门(1)---MIT导论
- python爬虫学习整理——爬虫入门(1)
- 零基础入门学习Python(8):字符串格式化
- Python装饰器学习(九步入门)
- 【python】入门学习(一)
- python bottle学习(一)快速入门
- Python3.5入门学习记录-条件控制