大家好,本文将围绕python微信小程序自动打卡展开说明,python微信小程序打卡是一个很多人都想弄明白的事情,想搞清楚python 操作小程序需要先了解以下几个事情。
想找一个桌面待办事项提醒的软件,找了好久都没有找到合适的,现有软件的提醒时间都只是精确到上下午或晚上,没有能精确到分钟,便想着自己做一个,这样子还能顺带加强自己对python的学习。
功能目标:输入时间和待办事项内容后会在指定时间弹窗提示pythonturtle画递归树。主要使用Timer和tkinter这两个python自带的库,一是方便无需再下载,另一个是方便封装
因为是新手,所以代码过于冗余,等有空再简化修改吧
import time
from datetime import datetime
from threading import Timer
import threading
from tkinter import ttk
from tkinter import *
#创建显示窗口
window = Tk()
window.title("日程提醒")
#定义输入时间以及其位置
inputmessageyear = Label(window, text = "年份") #年份
inputmessageyear.grid(column = 0, row = 0)
inputyear = Spinbox(window, from_ = 2022, to= 3000)
inputyear.grid(column = 1,row = 0)
inputmessagemonth = Label(window, text = "月份") #月份
inputmessagemonth.grid(column = 0, row = 1)
inputmonth = Spinbox(window, from_ = 1, to = 12)
inputmonth.grid(column = 1, row = 1)
inputmessageday = Label(window, text = "日") #日
inputmessageday.grid(column = 0, row = 2)
inputday = Spinbox(window, from_ = 1, to = 31)
inputday.grid(column = 1, row = 2)
inputmessagehour = Label(window, text = "小时") #小时
inputmessagehour.grid(column = 0, row = 3)
inputhour = Spinbox(window, from_ = 0, to = 60)
inputhour.grid(column = 1, row = 3)
inputmessageminth = Label(window, text = "分钟") #分钟
inputmessageminth.grid(column = 0, row = 4, )
inputminth = Spinbox(window, from_ = 0, to = 60)
inputminth.grid(column = 1, row = 4)
#输入文本(备忘录事件)
inputmessagetext = Label(window, text = "日程提示")
inputmessagetext.grid(column = 0,row = 5)
inputtext = Entry(window, width = 26)
inputtext.grid(column = 0, row = 6, columnspan = 2, rowspan = 2)
#创建询问函数
def clicked():
useyear = str(inputyear.get())
#将输入时间转化为能与当地时间比较的标准格式,此处有些冗余,但懒得再改了
usemon = str(inputmonth.get())
useday = str(inputday.get())
usehour = str(inputhour.get())
usemin = str(inputminth.get())
usedatetime = "{}{}{}{}{}{}{}{}{}".format(useyear, "-", usemon, "-", useday, "-", usehour, "-", usemin)
usedate_time = datetime.strptime(usedatetime, '%Y-%m-%d-%H-%M')
#此处print用于检测
"""print(usedatetime,"clicked函数年内usedatetime输出正常")"""
def tishi():
global t
benditime = time.localtime(time.time())
benditime1 = "{}{}{}{}{}{}{}{}{}".format(benditime.tm_year, "-", benditime.tm_mon, "-", benditime.tm_mday, "-", benditime.tm_hour, "-", benditime.tm_min, "-", benditime.tm_sec)
local_time = datetime.strptime(benditime1, '%Y-%m-%d-%H-%M')
print("输出benditime") #测试用
if local_time != usedate_time:
print("输出tishi函数内的if输出正常")
t = Timer(10, tishi)
t.start()
elif local_time == usedate_time:
print("tishi函数内的elif输出正常")
messageshow = Tk()
messageshow.title("日程提醒")
messageshow.geometry('300x300')
messageshowtext = Label(messageshow, text = inputtext.get()) #获取输入文本信息并显示到新弹窗上
messageshowtext.grid(column = 0, row = 1)
messageshow.wm_attributes('-topmost', 1) #将tkinter置顶于最前
messageshow.mainloop()
t = threading.Timer(10, tishi)
t.start()
#添加确定按钮
confirmbutton = Button(window, text = "确定", command = clicked)
confirmbutton.grid(column = 1, row = 8)
window.mainloop()
在编写时主要遇到两个难题,一是如何将获取到的本地时间转化为与用户输入时间等同的字符串。为解决这一问题我采用了较为繁琐的方式:
benditime = time.localtime(time.time())
benditime1 = "{}{}{}{}{}{}{}{}{}".format(benditime.tm_year, "-", benditime.tm_mon, "-", benditime.tm_mday, "-", benditime.tm_hour, "-", benditime.tm_min, "-", benditime.tm_sec)
local_time = datetime.strptime(benditime1, '%Y-%m-%d-%H-%M')
应该还存在更为简约的表达方式,但能力有限只能留待以后摸索了
二是tkinter的Button始终无反应,我在程序内各处分别添加了几个print进行测试,最后发现居然是" command = clicked"被写成了" command = clicked()":
#添加确定按钮
confirmbutton = Button(window, text = "确定", command = clicked)
confirmbutton.grid(column = 1, row = 8)
多出来的一个"()"足足浪费了我两个小时,失之毫厘谬以千里。
之后采用puinstaller进行封装,成功运行。
纪念一下我人生中第一个成功封装运行的小程序。