import tkinter as tk from datetime import datetime, timedelta def enhanced_time_picker(): """增强版时间选择弹窗 - 直接返回选择时间""" selected_time = [None] # 使用列表实现类似引用传递 def on_button_click(minutes): """按钮点击处理函数""" target_time = datetime.now() - timedelta(minutes=minutes) selected_time[0] = int(target_time.timestamp()) * 1000 root.destroy() # 创建主窗口 root = tk.Tk() root.title("快速时间选择") root.geometry("250x300") root.resizable(False, False) # 居中显示 root.eval('tk::PlaceWindow . center') # 当前时间显示 current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") current_label = tk.Label(root, text=f"当前时间: {current_time}", font=("Arial", 9), pady=10) current_label.pack() # 按钮框架 button_frame = tk.Frame(root) button_frame.pack(expand=True, pady=10) # 快速选择按钮 time_options = [ ("30分钟前", 30), ("1小时前", 60), ("2小时前", 120), ("3小时前", 180) ] for text, minutes in time_options: tk.Button(button_frame, text=text, command=lambda m=minutes: on_button_click(m), width=15, height=1).pack(pady=5) # 运行窗口 root.mainloop() return selected_time[0] # 使用示例 if __name__ == "__main__": result = enhanced_time_picker() if result: print(f"选择的时间: {result}") else: print("未选择时间")