效果如下:
import webbrowser
from tkinter import *
import time
import difflib
LOG_LINE_NUM = 0
class MY_GUI_SET:
"""小工具"""
def __init__(self, init_window_name):
self.init_window_name = init_window_name
self.init_window_name.attributes("-alpha", 1) # 虚化 值越小虚化程度越高
self.init_window_name.title("小工具")
# 文本框
self.init_data_Text = Text(self.init_window_name, width=67, height=35) # 录入框一
self.init_data_Text2 = Text(self.init_window_name, width=70, height=49) # 录入框二
self.log_data_Text = Text(self.init_window_name, width=66, height=9) # 日志框
self.init_data_Text.grid(row=1, column=0, rowspan=10, columnspan=10)
self.init_data_Text2.grid(row=1, column=12, rowspan=15, columnspan=10)
self.log_data_Text.grid(row=13, column=0, columnspan=10)
# 标签
self.init_data_label = Label(self.init_window_name, text="数据一")
self.init_data_label2 = Label(self.init_window_name, text="数据二")
self.log_label = Label(self.init_window_name, text="日志")
self.init_data_label.grid(row=0, column=0)
self.init_data_label2.grid(row=0, column=12)
self.log_label.grid(row=12, column=0)
# diff按钮
self.str_trans_to_diff_button = Button(self.init_window_name, text="diff", bg="lightblue", width=10,
command=self.compare_file) # 调用内部方法 加()为直接调用
self.str_trans_to_diff_button.grid(row=1, column=11)
# 打开报告按钮
self.str_trans_to_diff_button = Button(self.init_window_name, text="打开报告", bg="lightblue", width=10,
command=self.open_report)
self.str_trans_to_diff_button.grid(row=2, column=11)
# 功能函数
def GetLines(self, file_name):
try:
with open(file_name, "r", encoding="utf-8") as f:
return f.readlines()
except IOError:
self.write_log_to_Text("ERROR: 没有找到文件:%s或读取文件失败!" % file_name)
def open_report(self):
try:
if report:
self.write_log_to_Text("INFO: 打开report文件为%s" % report)
webbrowser.open_new_tab(report)
except NameError:
self.write_log_to_Text("没有找到diff报告~~~")
def compare_file(self):
global report
# f1 = self.init_data_Text.get(1.0, END).strip().replace("\n", "").encode()
# f2 = self.init_data_Text2.get(1.0, END).strip().replace("\n", "").encode()
f1 = self.init_data_Text.get(1.0, END)
f2 = self.init_data_Text2.get(1.0, END)
if f1 and f2:
try:
# txt_line1 = self.GetLines(f1)
# txt_line2 = self.GetLines(f2)
txt_line1 = f1.splitlines(1)
txt_line2 = f2.splitlines(1)
if txt_line1 and txt_line2:
report = "result_" + self.get_current_time() + ".html"
d = difflib.HtmlDiff(wrapcolumn=45)
with open(report, "w", encoding="utf-8") as fid:
fid.write(d.make_file(txt_line1, txt_line2))
self.write_log_to_Text("INFO:Diff suceess, report文件为%s" % report)
except TypeError:
self.write_log_to_Text("ERROR:Diff failed,不是一个文件,现在只支持输入文件")
else:
self.write_log_to_Text("请先输入文件呀!!!")
@staticmethod
# 获取当前时间
def get_current_time():
current_time = time.strftime('%Y-%m-%d-%H_%M_%S', time.localtime(time.time()))
return current_time
# 日志动态打印
def write_log_to_Text(self, logmsg):
global LOG_LINE_NUM
current_time = self.get_current_time()
logmsg_in = str(current_time) + " " + str(logmsg) + "\n" # 换行
if LOG_LINE_NUM <= 7:
self.log_data_Text.insert(END, logmsg_in)
LOG_LINE_NUM = LOG_LINE_NUM + 1
else:
self.log_data_Text.delete(1.0, 2.0)
self.log_data_Text.insert(END, logmsg_in)
def gui_start():
init_window = Tk()
MY_GUI_SET(init_window)
init_window.mainloop()
gui_start()
windows用pyinstaller打包,mac用py2app打包。不打包直接运行也可以
最后,如果帮到了小伙伴,给我点个赞吧!嘻嘻