95 lines
4.4 KiB
Python
95 lines
4.4 KiB
Python
import tkinter as tk
|
|
from tkinter import ttk
|
|
import pyperclip
|
|
from tkinter import messagebox
|
|
from datetime import datetime
|
|
|
|
def create_ui(parent, run_callback):
|
|
"""创建时间戳转换前端界面:时间戳转文本和文本转时间戳"""
|
|
frame = ttk.Frame(parent)
|
|
frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
|
|
frame.columnconfigure(1, weight=1)
|
|
|
|
# 时区选项
|
|
timezones = ["Asia/Shanghai", "UTC", "America/New_York", "Europe/London", "Australia/Sydney"]
|
|
|
|
# 上半部分:时间戳转文本
|
|
timestamp_frame = ttk.LabelFrame(frame, text="时间戳转文本", padding=5)
|
|
timestamp_frame.grid(row=0, column=0, columnspan=2, sticky=(tk.W, tk.E), pady=5)
|
|
timestamp_frame.columnconfigure(1, weight=1)
|
|
|
|
ttk.Label(timestamp_frame, text="时间戳:").grid(row=0, column=0, sticky=tk.W, pady=2)
|
|
timestamp_entry = ttk.Entry(timestamp_frame)
|
|
timestamp_entry.insert(0, str(int(datetime.now().timestamp()))) # 默认当前时间戳
|
|
timestamp_entry.grid(row=0, column=1, sticky=(tk.W, tk.E), pady=2)
|
|
|
|
ttk.Label(timestamp_frame, text="时区:").grid(row=1, column=0, sticky=tk.W, pady=2)
|
|
timestamp_tz_var = tk.StringVar(value="Asia/Shanghai")
|
|
ttk.Combobox(timestamp_frame, textvariable=timestamp_tz_var, values=timezones, state="readonly").grid(row=1, column=1, sticky=tk.W, pady=2)
|
|
|
|
ttk.Label(timestamp_frame, text="结果:").grid(row=2, column=0, sticky=tk.W, pady=2)
|
|
timestamp_result = ttk.Entry(timestamp_frame, state="readonly")
|
|
timestamp_result.grid(row=2, column=1, sticky=(tk.W, tk.E), pady=2)
|
|
|
|
def copy_timestamp_result():
|
|
output = timestamp_result.get()
|
|
if output:
|
|
pyperclip.copy(output)
|
|
messagebox.showinfo("提示", "结果已复制到剪贴板")
|
|
|
|
def convert_timestamp():
|
|
timestamp = timestamp_entry.get().strip()
|
|
if not timestamp:
|
|
messagebox.showerror("错误", "请输入时间戳")
|
|
return
|
|
result = run_callback(["timestamp_to_text", timestamp, timestamp_tz_var.get()])
|
|
timestamp_result.configure(state="normal")
|
|
timestamp_result.delete(0, tk.END)
|
|
timestamp_result.insert(0, result[0] if result else "无结果")
|
|
timestamp_result.configure(state="readonly")
|
|
|
|
ttk.Button(timestamp_frame, text="转换", command=convert_timestamp).grid(row=3, column=0, columnspan=2, pady=5)
|
|
timestamp_result.bind("<Double-1>", lambda e: copy_timestamp_result()) # 双击复制
|
|
|
|
# 分割线
|
|
ttk.Separator(frame, orient=tk.HORIZONTAL).grid(row=1, column=0, columnspan=2, sticky=(tk.W, tk.E), pady=10)
|
|
|
|
# 下半部分:文本转时间戳
|
|
text_frame = ttk.LabelFrame(frame, text="文本转时间戳", padding=5)
|
|
text_frame.grid(row=2, column=0, columnspan=2, sticky=(tk.W, tk.E), pady=5)
|
|
text_frame.columnconfigure(1, weight=1)
|
|
|
|
ttk.Label(text_frame, text="日期时间:").grid(row=0, column=0, sticky=tk.W, pady=2)
|
|
text_entry = ttk.Entry(text_frame)
|
|
text_entry.insert(0, "2025-06-05 16:29:11") # 默认时间
|
|
text_entry.grid(row=0, column=1, sticky=(tk.W, tk.E), pady=2)
|
|
|
|
ttk.Label(text_frame, text="时区:").grid(row=1, column=0, sticky=tk.W, pady=2)
|
|
text_tz_var = tk.StringVar(value="Asia/Shanghai")
|
|
ttk.Combobox(text_frame, textvariable=text_tz_var, values=timezones, state="readonly").grid(row=1, column=1, sticky=tk.W, pady=2)
|
|
|
|
ttk.Label(text_frame, text="结果:").grid(row=2, column=0, sticky=tk.W, pady=2)
|
|
text_result = ttk.Entry(text_frame, state="readonly")
|
|
text_result.grid(row=2, column=1, sticky=(tk.W, tk.E), pady=2)
|
|
|
|
def copy_text_result():
|
|
output = text_result.get()
|
|
if output:
|
|
pyperclip.copy(output)
|
|
messagebox.showinfo("提示", "结果已复制到剪贴板")
|
|
|
|
def convert_text():
|
|
text = text_entry.get().strip()
|
|
if not text:
|
|
messagebox.showerror("错误", "请输入日期时间")
|
|
return
|
|
result = run_callback(["text_to_timestamp", text, text_tz_var.get()])
|
|
text_result.configure(state="normal")
|
|
text_result.delete(0, tk.END)
|
|
text_result.insert(0, result[0] if result else "无结果")
|
|
text_result.configure(state="readonly")
|
|
|
|
ttk.Button(text_frame, text="转换", command=convert_text).grid(row=3, column=0, columnspan=2, pady=5)
|
|
text_result.bind("<Double-1>", lambda e: copy_text_result()) # 双击复制
|
|
|
|
return frame |