# coding=utf-8
import logging
import sys
import time
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import messagebox
import threading
from aw.aw import Env
_format = "[%(asctime)s][%(levelname)s][%(lineno)d] %(message)s"
logging.basicConfig(level=logging.INFO, format=_format, datefmt="%H:%M:%S")
EntryBoxContent = "" # entrybox中的内容
EntryBoxResult = messagebox.NO # entrybox中按下ok键还是no键
class RedirectStdout(object):
def __init__(self, tk_text: tk.Text):
self.tk_text = tk_text
def write(self, chars):
self.tk_text.insert(index=tk.END, chars=chars)
self.tk_text.see(index=tk.END)
def flush(self):
pass
class Decorator(object):
@staticmethod
def thread(func):
def wraper(*args, **kwargs):
# print(f"Start exe {func.__name__}")
threading.Thread(target=func, args=args, kwargs=kwargs).start()
# func(*args, **kwargs)
# print(f"Finish exe {func.__name__}")
return wraper
class APP(tk.Tk):
def __init__(self):
super().__init__()
self.set_window()
self.set_frame()
self.set_widget()
sys.stdout = RedirectStdout(self.text)
self.env = Env()
def set_window(self):
self.title("环境变量")
self.geometry("960x540")
def set_frame(self):
self.frame_1 = ttk.LabelFrame(master=self, text="系统环境变量")
self.frame_1.grid(row=0, column=0,
sticky=tk.NSEW)
self.frame_2 = ttk.LabelFrame(master=self, text="用户环境变量")
self.frame_2.grid(row=1, column=0,
sticky=tk.NSEW)
self.frame_3 = ttk.Frame(master=self)
self.frame_3.grid(row=2, column=0,
sticky=tk.NSEW)
self.rowconfigure(index=2, weight=1)
self.columnconfigure(index=0, weight=1)
def set_widget(self):
self.set_widget_1()
self.set_widget_2()
self.set_widget_3()
def set_widget_1(self):
ttk.Button(master=self.frame_1,
text="获取",
command=self.get_system_env,
width=10).grid(row=0, column=0)
ttk.Button(master=self.frame_1,
text="设置",
command=self.set_system_env,
width=10).grid(row=0, column=1)
ttk.Button(master=self.frame_1,
text="新增",
command=self.add_system_env,
width=10).grid(row=0, column=2)
ttk.Button(master=self.frame_1,
text="删除",
command=self.del_system_env,
width=10).grid(row=0, column=3)
def set_widget_2(self):
ttk.Button(master=self.frame_2,
text="获取",
command=self.get_user_env,
width=10).grid(row=0, column=0)
ttk.Button(master=self.frame_2,
text="设置",
command=self.set_user_env,
width=10).grid(row=0, column=1)
ttk.Button(master=self.frame_2,
text="新增",
command=self.add_user_env,
width=10).grid(row=0, column=2)
ttk.Button(master=self.frame_2,
text="删除",
command=self.del_user_env,
width=10).grid(row=0, column=3)
def set_widget_3(self):
self.text = tk.Text(master=self.frame_3, spacing1=3, spacing3=3)
self.text.grid(row=0, column=0,
sticky=tk.NSEW)
self.frame_3.rowconfigure(index=0, weight=1)
self.frame_3.columnconfigure(index=0, weight=1)
def log2text(self, strings: str, timestamp: bool = False, del_last_line: bool = False) -> None:
"""
向text文本框中打印日志
:param timestamp: 是否添加时间戳
:param strings: 日志内容
:param del_last_line: 是否删除最后一行,用于进度条
:return: None
"""
# 删除
if del_last_line:
text_content = self.text.get("1.0", tk.END)
line_amount = len(text_content.splitlines())
self.text.delete(index1=float(line_amount - 1),
index2=tk.END)
if line_amount > 2: # 光标在第1或2行时,删除到end,光标在第1行,再换行后光标到第2行,导致第1行是空行
self.text.insert(index=tk.END,
chars="\n")
# 写入
if timestamp:
temp = f'[{time.strftime("%H:%M:%S")}] {strings}'
else:
temp = f'{strings}'
self.text.insert(index=tk.END,
chars=f"{temp}\n")
self.text.see(index=tk.END)
@Decorator.thread
def get_system_env(self):
"""
获取系统环境变量
:return:
"""
self.log2text("系统环境变量", True)
env = self.env.get_system_env()
for item in env.split(";"):
self.log2text(item)
# @Decorator.thread
def set_system_env(self):
global EntryBoxContent
global EntryBoxResult
entrybox = EntryBox()
entrybox.mainloop()
if EntryBoxResult == messagebox.NO:
return None
rst = messagebox.askyesno(title="信息确认",
message="确认要覆盖系统环境变量吗?\n",
default=messagebox.NO)
if rst is False:
return None
self.log2text(EntryBoxContent)
# 重置全局变量
EntryBoxContent = ""
EntryBoxResult = messagebox.NO
@Decorator.thread
def add_system_env(self):
pass
@Decorator.thread
def del_system_env(self):
pass
@Decorator.thread
def get_user_env(self):
"""
获取用户环境变量
:return:
"""
self.log2text("用户环境变量", True)
env = self.env.get_user_env()
for item in env.split(";"):
self.log2text(item)
@Decorator.thread
def set_user_env(self):
pass
@Decorator.thread
def add_user_env(self):
pass
@Decorator.thread
def del_user_env(self):
pass
class EntryBox(tk.Toplevel):
def __init__(self):
super().__init__()
self.set_window()
self.set_frame()
self.set_widget()
def set_window(self):
self.title("EntryBox")
self.geometry("480x270")
def set_frame(self):
self.frame_1 = ttk.Frame(master=self)
self.frame_2 = ttk.Frame(master=self)
self.frame_1.grid(row=0, column=0,
padx=5, pady=5,
sticky=tk.NSEW)
self.frame_2.grid(row=1, column=0,
padx=5, pady=5,
sticky=tk.NSEW)
self.rowconfigure(index=0, weight=1)
self.columnconfigure(index=0, weight=1)
def set_widget(self):
self.text = tk.Text(master=self.frame_1)
self.text.grid(row=0, column=0, sticky=tk.NSEW)
self.frame_1.rowconfigure(index=0, weight=1)
self.frame_1.columnconfigure(index=0, weight=1)
self.button_ok = ttk.Button(master=self.frame_2,
text="Ok",
command=self.click_ok,
width=10)
self.button_cancel = ttk.Button(master=self.frame_2,
text="Cancel",
command=self.click_cancel,
width=10)
self.button_ok.grid(row=0, column=0, padx=5, pady=5)
self.button_cancel.grid(row=0, column=1, padx=5, pady=5)
self.frame_2.columnconfigure(index="all", weight=1)
def click_ok(self):
global EntryBoxContent
global EntryBoxResult
EntryBoxContent = self.text.get("1.0", tk.END).strip()
EntryBoxResult = messagebox.OK
self.quit()
self.destroy()
def click_cancel(self):
self.quit()
self.destroy()
if __name__ == '__main__':
app = APP()
app.mainloop()
tkinter_demo_002
于 2024-04-05 23:19:10 首次发布