控制台版本

思路

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()
效果

最新文章

  1. ABP入门系列(1)——学习Abp框架之实操演练
  2. Python 黑帽编程大纲(变化中)
  3. RAC 10.2.0.5,客户端登陆间断遭遇ORA-12545
  4. js对象学习
  5. Python中读取csv文件内容方法
  6. dropdownlist值改变时调用js
  7. 二维树状数组——SuperBrother打鼹鼠(Vijos1512)
  8. oracle 主键删除,联合主键的创建
  9. 腾讯2014在广州站实习生offer经验(TEG-开发背景)
  10. 你不知道的Eclipse用法:使用Allocation tracker跟踪Android应用内存分配
  11. unity 组件开发
  12. SLAVE为什么一直不动了
  13. C++ Primer 有感(多重继承与虚继承)
  14. 从零开始学习python:demo2.5
  15. Ubuntu 16.04下安装谷歌浏览器(转)
  16. oracle 查询非自增长分区的最大分区
  17. CPP相关的常见错误(更新ing)
  18. LeetCode: Recover Binary Search Tree 解题报告
  19. ALGO-147_蓝桥杯_算法训练_4-3水仙花数
  20. Hadoop 4 MapReduce

热门文章

  1. Alpha阶段第2周/共2周 Scrum立会报告+燃尽图 05
  2. Alpha冲刺 (4/10)
  3. 7-13 求链式线性表的倒数第K项(20 分)
  4. Vitya in the Countryside
  5. DB2经常使用命令
  6. RHEL6安装Oracle 11g R2
  7. .NET 4.5 HttpClient 中使用Cookie
  8. JAVA面向对象编程课程设计——项目部署
  9. Shell中单引号、双引号、反引号、反斜杠的区别
  10. 【BZOJ】1756: Vijos1083 小白逛公园(线段树)