1.先创建tkinter窗体
#设置窗口
def set_window(self):
screenWidth = int(root.winfo_screenwidth()/8) # 获取显示区域的宽度
screenHeight = int(root.winfo_screenheight()/12) # 获取显示区域的高度
self.root.title("base64转换工具")
self.root.geometry('1068x681+'+str(screenWidth)+'+'+str(screenHeight)) #窗体大小
#self.root["bg"]='pink' #背景色
#self.root.attributes("-alpha",0.9) #虚化
#标签
self.in_label=Label(self.root,text="待处理数据")
self.in_label.grid(row=0,column=0)
self.out_label=Label(self.root,text="输出结果")
self.out_label.grid(row=0,column=12)
self.log_label=Label(self.root,text="日志")
self.log_label.grid(row=12,column=0)
#文本框
self.in_text=Text(self.root,width=67,height=35)
self.in_text.grid(row=1,column=0,rowspan=10,columnspan=10)
self.out_text=Text(self.root,width=70,height=49)
self.out_text.grid(row=1,column=12,rowspan=15,columnspan=10)
self.log_text=Text(self.root,width=66,height=9)
self.log_text.grid(row=13,column=0,columnspan=10)
#按钮
self.button1=Button(self.root,text="编码",bg="lightblue",width=10,command=self.str_base64)
self.button1.grid(row=1,column=11)
self.button2=Button(self.root,text="解码",bg="lightblue",width=10,command=self.base64_str)
self.button2.grid(row=3,column=11)
self.button3=Button(self.root,text="清空",bg="lightblue",width=10,command=self.clear)
self.button3.grid(row=5,column=11)
2.base64转字符编码函数
def str_base64(self): #编码
self.out_text.delete("1.0","end")
data=self.in_text.get(1.0,END).strip().replace("\n","")
b64_b=base64.b64encode(data.encode('utf-8'))
self.out_text.insert(1.0,b64_b)
self.write_log('成功编码')
3.字符转base64解码函数
def base64_str(self): #解码
self.out_text.delete("1.0","end")
try:
data=self.in_text.get(1.0,END).strip().replace("\n","").encode()
byt=base64.b64decode(data)
str=byt.decode('utf-8')
self.out_text.insert(1.0,str)
self.write_log('成功解码')
except Exception:
self.menu=tkinter.messagebox.showwarning('警告',"输入错误的base64码")
self.write_log('解码失败,输入有误')
4.清空函数
def clear(self):
self.in_text.delete("1.0","end")
self.out_text.delete("1.0","end")
self.log_text.delete("1.0","end")
5.输出日志函数
def write_log(self,logmsg):
c_time=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
logmsg_in = str(c_time) + " " + str(logmsg) + "\n"
#self.log_text.delete(1.0, 2.0)
self.log_text.insert(END, logmsg_in)
6.打包成exe
打开python控制台,输入pyinstaller -D -w str2base64.py
测试