现在人们的生活压力越来越大,喜欢油炸,烧烤,各种重口味食物,平时又懒得运动,所以年轻患病或者猝死的也越来越多。那么如何解决这一问题呢?
今天我们用Python开发一个久坐提醒小工具,让每个人都能合理休息,定时起身走动走动,远离猝死危害!
其实可以开发一个GUI工具,允许程序员设置倒计时,然后到时见电脑自动锁屏,强迫大家起来走走,放松放松。
为了避免突如其来的锁屏,导致大家不适。所以会在锁屏前10秒给出弹窗提示。这样不至于太过突兀。可这个思路却引发了问题。
tkinter的message会阻塞进程,用户不点击取消掉消息窗口,就无法继续执行。为了能使弹出提示消息的同时,继续倒计时读秒,我们需要引入threading和Toplevel的模块,创建一个子窗口,并等待3秒后销毁它。
熟悉bat脚本的童鞋都知道,bat中有shutdown 的命令,可以用来定时重启、关机,但却没有锁屏。查了很久没有结果,最终无奈只能通过pythonctypes模块调用windll,完成锁屏操作了,具体代码如下:
def close_windows():
user32 = windll.LoadLibrary('user32.dll')
user32.LockWorkStation()
# -*- coding: utf-8 -*-
# @Date : 2019/11/28 23:23
# @Software : PyCharm
# @version :Python 3.7.3
# @File : CareForCoders.py
from tkinter import *
from tkinter.messagebox import showwarning, showinfo
import time
from ctypes import *
import threading
# tkinter GUI工具居中展示
def center_window(master, width, height):
screenwidth = master.winfo_screenwidth()
screenheight = master.winfo_screenheight()
size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2,
(screenheight - height) / 2)
master.geometry(size)
# 锁定屏幕
def close_windows():
user32 = windll.LoadLibrary('user32.dll')
user32.LockWorkStation()
class CareForCoders:
def user_setting(self):
note = LabelFrame(root, text="说明", padx=10, pady=10,
fg="red", font=("黑体", '11'))
note.grid(padx=10, pady=2, sticky=NSEW)
index = Label(note, text='程序猿/媛们,久坐伤身请务必定时休息!')
index.grid()
lb = LabelFrame(root, text="定时设置(支持小数)", padx=10,
pady=10, fg="red", font=("黑体", '11'))
lb.grid(padx=10, pady=2, sticky=NSEW)
self.time_entry = Entry(lb)
self.time_entry.grid(row=1, column=0)
unit = Label(lb, text="(单位:分)")
unit.grid(row=1, column=1, padx=5)
self.countdown_lb = Label(text="休息倒计时:", justify=LEFT,
font=("黑体", '11'))
self.countdown_lb.grid(row=2)
self.submit = Button(root, text="启动", width=8,
command=lambda: self.get_countdown(self.time_entry.get())
)
self.submit.grid(row=3, column=0, pady=10)
def get_countdown(self, countdown):
try:
_float_countdown = float(countdown)
if _float_countdown <= 0:
showwarning("提示:", message="倒计时必须为正数!")
else:
self.countdown_show(_float_countdown * 60)
except ValueError:
showwarning("提示:", message="请填写正确的倒计时!")
def countdown_show(self, countdown_sec):
self.time_entry.config(state=DISABLED)
self.submit.config(state=DISABLED)
time.sleep(1)
self.countdown_lb.config(text="休息倒计时: %02d:%02d" %
(countdown_sec // 60, countdown_sec % 60))
root.update()
# 为了避免突如其来的锁屏,倒计时30秒给出提示...
if countdown_sec == 10:
t = threading.Thread(target=self.notice)
t.start()
if countdown_sec < 1:
# 启动锁屏操作
close_windows()
self.time_entry.config(state=NORMAL)
self.submit.config(state=NORMAL)
self.countdown_lb.config(text="欢迎主人回来...")
root.update()
return
countdown_sec -= 1
self.countdown_lb.after(1000, self.countdown_show(countdown_sec))
@staticmethod
def notice():
# message = Toplevel(root)
# message.title('提示')
# Label(message, text='主人,工作这么久了,准备休息下吧!'
# , justify=CENTER, font=("黑体", '11')).grid()
# time.sleep(3)
# message.destroy()
showinfo("提示",message='主人,工作这么久了,准备休息下吧!')
if __name__ == '__main__':
root = Tk()
center_window(root, 260, 200)
root.resizable(width=False, height=False)
root.title('久坐提醒 by:清风Python')
Main = CareForCoders()
Main.user_setting()
进下面链接,学习更多python小技巧。