timeSelect.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import tkinter as tk
  2. from datetime import datetime, timedelta
  3. def enhanced_time_picker():
  4. """增强版时间选择弹窗 - 直接返回选择时间"""
  5. selected_time = [None] # 使用列表实现类似引用传递
  6. def on_button_click(minutes):
  7. """按钮点击处理函数"""
  8. target_time = datetime.now() - timedelta(minutes=minutes)
  9. selected_time[0] = int(target_time.timestamp()) * 1000
  10. root.destroy()
  11. # 创建主窗口
  12. root = tk.Tk()
  13. root.title("快速时间选择")
  14. root.geometry("250x300")
  15. root.resizable(False, False)
  16. # 居中显示
  17. root.eval('tk::PlaceWindow . center')
  18. # 当前时间显示
  19. current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  20. current_label = tk.Label(root, text=f"当前时间: {current_time}",
  21. font=("Arial", 9), pady=10)
  22. current_label.pack()
  23. # 按钮框架
  24. button_frame = tk.Frame(root)
  25. button_frame.pack(expand=True, pady=10)
  26. # 快速选择按钮
  27. time_options = [
  28. ("30分钟前", 30),
  29. ("1小时前", 60),
  30. ("2小时前", 120),
  31. ("3小时前", 180)
  32. ]
  33. for text, minutes in time_options:
  34. tk.Button(button_frame, text=text,
  35. command=lambda m=minutes: on_button_click(m),
  36. width=15, height=1).pack(pady=5)
  37. # 运行窗口
  38. root.mainloop()
  39. return selected_time[0]
  40. # 使用示例
  41. if __name__ == "__main__":
  42. result = enhanced_time_picker()
  43. if result:
  44. print(f"选择的时间: {result}")
  45. else:
  46. print("未选择时间")