本帖最后由 hrh123 于 2023-7-25 18:24 编辑
帮楼主改了下gui,不知楼主中不中意呢?
import contextlib
import hashlib
import sys
import tkinter as tk
copyright_notice = "by 大白baymax,hrh123 from www.52pojie.cn"
def convert_to_md5(input_str):
md5_hash = hashlib.md5(input_str.encode()).hexdigest()
sys.stdout.write("\n{md5_hash}".format(md5_hash=md5_hash))
class GUI(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.master.title("字符串转换md5小工具")
self.pack()
self.create_widgets()
def create_widgets(self):
self.sub_frame = tk.Frame(self)
self.text = tk.Text(self.sub_frame)
self.text.insert(tk.INSERT, copyright_notice)
self.scroll = tk.Scrollbar(self.sub_frame)
self.text.config(yscrollcommand=self.scroll.set)
self.scroll.config(command=self.text.yview)
self.text.grid(row=0, column=0, sticky="nsew")
self.scroll.grid(row=0, column=1, sticky="ns")
self.sub_frame.grid(row=0, column=0, columnspan=2, sticky="nsew")
self.label = tk.Label(self, text="Type in the STRING,press <Enter> to start")
self.label.grid(row=1)
self.str_entry = tk.Entry(self)
self.str_entry.grid(row=2)
self.str_entry.bind(
"<Return>", lambda event: convert_to_md5(self.str_entry.get())
)
class PrintToText:
def __init__(self, text):
self.text = text
def write(self, s):
self.text.insert(tk.END, s)
self.text.see(tk.END)
self.text.update()
def start_gui():
root = tk.Tk()
gui = GUI(master=root)
ptt = PrintToText(gui.text)
with contextlib.redirect_stdout(ptt), contextlib.redirect_stderr(ptt):
gui.master.mainloop()
start_gui()
|