测试工具开发(python+Tkinter+pyinstaller)实现时间戳转换、base64、MD5加密
内网办公环境导致很多线上工具的使用受限,所以产生出python写点平时使用的工具,应用于测试活动,下面是一个简单的例子,以作延伸。
简介:
- tkinter
tkinter(Tk interface)是Python的标准GUl库,支持跨平台的GUl程序开发。tkinter适合小型的GUl程序编写,也特别适合初学者学习GUl编程。不需要安装,直接用。
- pyinstaller
pyinstaller 用于打包成exe文件,方便使用,pyinstaller是一个python库,和其他库一样,可以用pip 进行导入。pip install pyinstaller
功能:
- 获取当前月密码
- 当前时间转换时间戳
- 时间戳转换时间
- 时间转换时间戳
- base64加解密
- Md5加密32位小写
代码:
import base64 import datetime import time from tkinter import * import hashlib from tkinter import messagebox class timeTransform(Frame): def __init__(self,master=None): super().__init__(master) self.master=master self.pack() self.createWidget() def createWidget(self): # 标签:当前月密码获取 self.curr_title1 = Label(self, text='2023当前月密码获取', bg="light gray", fg='black', anchor='w', width=80, font=('宋体', 10, 'bold')) self.curr_title1.grid(row=0, column=0, sticky="w", columnspan=4, padx=5, pady=5) self.btn_curr1 = Button(self, text=' 密码获取 ', bg="green", fg='white', command=self.getCurrentMonthPassword) self.btn_curr1.grid(row=1, column=0) self.curr_time1 = Text(self, width=30, height=2) self.curr_time1.grid(row=1, column=3) self.curr_time1.bind(self.getCurrentMonthPassword()) # 标签:当前时间转换时间戳 self.curr_title = Label(self, text='当前时间 → 时间戳',bg="light gray",fg='black',anchor='w',width=80,font=('宋体',10,'bold')) self.curr_title.grid(row=2, column=0, sticky="w",columnspan=4,padx=5,pady=5) # self.lable_curr = Label(self, text='当前时间') self.btn_curr = Button(self,text='当前时间刷新',bg="green",fg='white', command=self.getCurrentTime) self.btn_curr.grid(row=3, column=0) self.curr_now = Text(self, width=20, height=1) self.curr_now.grid(row=3, column=1,padx=5,pady=5) self.curr_time = Text(self, width=30, height=3) self.curr_time.grid(row=3, column=3) self.curr_time.bind(self.getCurrentTime()) #标签:时间戳转时间 self.lable_title=Label(self,text='时间戳 → 时间',bg="light gray",fg='black',anchor='w',width=80,font=('宋体',10,'bold')) self.lable_title.grid(row=4,column=0,sticky="w",columnspan=4,padx=5,pady=5) self.lable_stamp = Label(self, text='时间戳') self.lable_stamp.grid(row=5, column=0) # self.lable03 = Label(self, text='单位') # self.lable03.grid(row=1, column=1) self.lable_res = Label(self, text='时间') self.lable_res.grid(row=5, column=3) self.inputValue=StringVar() self.entry_res=Entry(self,textvariable=self.inputValue,width=20) self.entry_res.grid(row=6, column=0,padx=5,pady=5) self.btn01 = Button(self, text=' 转 换 ', bg='green',fg='white',command=self.changeToTime) self.btn01.grid(row=6, column=1) self.result_time=Text(self,width=30,height=2) self.result_time.grid(row=6, column=3,padx=5,pady=5) # 标签:时间转时间戳 self.time_title = Label(self, text='时间 → 时间戳',bg="light gray",fg='black',anchor='w',width=80,font=('宋体',10,'bold')) self.time_title.grid(row=7, column=0,sticky="w",columnspan=4,padx=5,pady=5) self.lable_time = Label(self, text='时间(格式:2023-02-09 10:20:20)') self.lable_time.grid(row=8, column=0) self.lable_time_stamp = Label(self, text='时间戳(毫秒)') self.lable_time_stamp.grid(row=8, column=3) self.inputValue1 = StringVar() self.entry_time_res = Entry(self, textvariable=self.inputValue1,width=20) self.entry_time_res.grid(row=9, column=0) self.btn02 = Button(self, text=' 转 换 ', bg='green',fg='white',command=self.changeToStamp) self.btn02.grid(row=9, column=1,padx=20,pady=5) self.curr_stamp = Text(self, width=30, height=2) self.curr_stamp.grid(row=9, column=3) # 标签:base64加解密 self.base64_title = Label(self, text='base64加解密', bg="light gray", fg='black', anchor='w', width=80, font=('宋体', 10, 'bold')) self.base64_title.grid(row=10, column=0, sticky="w", columnspan=4, padx=5, pady=5) self.inputValue3 = StringVar() self.base64_entry = Entry(self, textvariable=self.inputValue3, width=20) self.base64_entry.grid(row=11, column=0) self.base64_encode_btn = Button(self, text='base64加密', bg='green', fg='white', command=self.base64_encode) self.base64_encode_btn.grid(row=11, column=1, padx=20, pady=5) self.base64_decode_btn = Button(self, text='base64解密', bg='green', fg='white', command=self.base64_decode) self.base64_decode_btn.grid(row=11, column=3, padx=20, pady=5) # 标签:MD5加密32位小写 self.md5_title = Label(self, text='MD5加密32位小写', bg="light gray", fg='black', anchor='w', width=80, font=('宋体', 10, 'bold')) self.md5_title.grid(row=12, column=0, sticky="w", columnspan=4, padx=5, pady=5) self.md5_label = Label(self, text='文本') self.md5_label.grid(row=13, column=0) self.md5_decode_label = Label(self, text='MD5加密32位小写') self.md5_decode_label.grid(row=13, column=3) self.inputValue2 = StringVar() self.md5_text = Entry(self, textvariable=self.inputValue2, width=25) self.md5_text.grid(row=14, column=0, sticky="e") self.btn03 = Button(self, text=' 加 密 ', bg='green', fg='white', command=self.md5_encoding) self.btn03.grid(row=14, column=1, padx=20, pady=5) self.decode_txt = Text(self, width=30, height=3) self.decode_txt.grid(row=14, column=3) #时间戳转换时间 def changeToTime(self): try: te=self.inputValue.get() te = te.strip() if len(te) > 10 and len(te) <= 13: now = int(te) / 1000 elif len(te) == 10: now = int(te) elif len(te) == 16: now = int(te) / 1000000 else: messagebox.showerror(title='错误', message='请输入正确的时间戳!') except: messagebox.showerror(title='错误',message='请输入正确的时间戳!') timeArray = time.localtime(now) otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray) self.result_time.delete(1.0, END) self.result_time.insert(INSERT,otherStyleTime) #获取当前时间 def getCurrentTime(self): t = time.time() bt = time.strftime('%Y-%m-%d %H:%M:%S') # print('秒级时间戳:' + str(int(t))) # 秒级时间戳 # print('毫秒级时间戳:' + str(int(round(t * 1000)))) # 毫秒级时间戳 # print('微秒级时间戳:' + str(int(round(t * 1000000)))) # 微秒级时间戳 self.curr_time.delete(1.0, END) self.curr_time.insert(INSERT,'毫秒级时间戳:'+str(int(round(t * 1000)))+'\n'+'秒级时间戳:' + str(int(t))+'\n'+'微秒级时间戳:' + str(int(round(t * 1000000)))) self.curr_now.delete(1.0,END) self.curr_now.insert(INSERT,bt) #时间转换时间戳 def changeToStamp(self): try: tm=self.inputValue1.get() tm=tm.strip() # 转为时间数组 timeArray = time.strptime(tm, '%Y-%m-%d %H:%M:%S') except Exception as err_msg: messagebox.showerror(title='错误', message='请输入正确的时间格式!'+str(err_msg)) # 转为时间戳 timeStamp = int(time.mktime(timeArray) * 1000) self.curr_stamp.delete(1.0, END) self.curr_stamp.insert(INSERT,timeStamp) # 判断字段末尾是否有空格并删除空格 def isSpace(self,string): if string[-1].isspace==True: return string.strip(' ') #MD5加密32位小写 def md5_encoding(self): code=self.inputValue2.get() code = code.strip() if len(code)!=0: str_md5 = hashlib.md5(code.encode('utf-8')).hexdigest() self.decode_txt.delete(1.0, END) self.decode_txt.insert(INSERT, str_md5) else: messagebox.showerror(title='错误', message='请填写需要加密的内容!') #获取当前月登陆密码 def getCurrentMonthPassword(self): now_time = datetime.datetime.now().strftime('%m') mon = int(now_time) passList=self.setpasslist() pwd=passList[mon - 1] self.curr_time1.delete(1.0, END) self.curr_time1.insert(INSERT,str(mon)+'月登陆密码是:\n'+pwd) def setpasslist(self): passList=['2301','2302','2303','2304','2305','2306','2307','2308','2309','2310','2311','2312'] return passList #base64加密 def base64_encode(self): content=self.inputValue3.get() content=content.strip() if len(content) !=0: try: bs64=str(base64.b64encode(content.encode('utf-8'))) self.inputValue3.set(bs64[2:-1]) except : messagebox.showerror(title='错误', message='请填写正确的内容!') else: messagebox.showerror(title='错误', message='请填写要加密的内容!') #base64解密 def base64_decode(self): content = self.inputValue3.get() try: bs64=str(base64.b64decode(content.encode('utf-8'))) self.inputValue3.set(bs64[2:-1]) except : messagebox.showerror(title='错误', message='请填写正确的内容!') root=Tk() root.title('工具箱') root.geometry('680x500+500+200') app = timeTransform(master=root) root.mainloop()
最后通过打包命令:pyinstaller -F -w xxx.py
完成!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构