RDSS/gui/widgets/status_bar.py

58 lines
1.6 KiB
Python

#!/usr/bin/env python3
"""
状态栏组件
"""
import customtkinter as ctk
from datetime import datetime
class StatusBar(ctk.CTkFrame):
def __init__(self, parent):
super().__init__(parent, height=30, fg_color="#2b2b2b")
# 配置网格布局
self.grid_columnconfigure(0, weight=1)
self.grid_columnconfigure(1, weight=0)
# 状态标签
self.status_var = ctk.StringVar(value="就绪")
self.status_label = ctk.CTkLabel(
self,
textvariable=self.status_var,
font=ctk.CTkFont(size=12)
)
self.status_label.grid(row=0, column=0, sticky="w", padx=10, pady=5)
# 时间标签
self.time_var = ctk.StringVar()
self.time_label = ctk.CTkLabel(
self,
textvariable=self.time_var,
font=ctk.CTkFont(size=12)
)
self.time_label.grid(row=0, column=1, sticky="e", padx=10, pady=5)
# 初始化变量
self.is_running = True
self.is_closing = False
# 更新时间
self.update_time()
def update_time(self):
"""更新时间显示"""
if self.is_running and not self.is_closing:
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.time_var.set(current_time)
# 每秒钟更新一次
self.after(1000, self.update_time)
def stop(self):
"""停止更新"""
self.is_running = False
self.is_closing = True
def set_status(self, message):
"""设置状态栏消息"""
self.status_var.set(message)