-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui_utils.py
More file actions
272 lines (224 loc) · 7.01 KB
/
ui_utils.py
File metadata and controls
272 lines (224 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"""
UI Utilities Module
Helper utilities for improved user experience including tooltips and notifications.
"""
import customtkinter as ctk
from typing import Optional
import tkinter as tk
class ToolTip:
"""
Create tooltip for a widget.
"""
def __init__(self, widget, text: str, delay: int = 500):
"""
Initialize tooltip.
Args:
widget: Widget to attach tooltip to
text: Tooltip text
delay: Delay in milliseconds before showing
"""
self.widget = widget
self.text = text
self.delay = delay
self.tooltip_window = None
self.id = None
# Bind events
self.widget.bind('<Enter>', self.on_enter)
self.widget.bind('<Leave>', self.on_leave)
self.widget.bind('<Button>', self.on_leave)
def on_enter(self, event=None):
"""Schedule tooltip display."""
self.schedule()
def on_leave(self, event=None):
"""Hide and unschedule tooltip."""
self.unschedule()
self.hide()
def schedule(self):
"""Schedule tooltip to appear."""
self.unschedule()
self.id = self.widget.after(self.delay, self.show)
def unschedule(self):
"""Cancel scheduled tooltip."""
if self.id:
self.widget.after_cancel(self.id)
self.id = None
def show(self):
"""Display the tooltip."""
if self.tooltip_window:
return
# Get widget position
x = self.widget.winfo_rootx() + 20
y = self.widget.winfo_rooty() + self.widget.winfo_height() + 5
# Create tooltip window
self.tooltip_window = tk.Toplevel(self.widget)
self.tooltip_window.wm_overrideredirect(True)
self.tooltip_window.wm_geometry(f"+{x}+{y}")
# Create label with text
label = tk.Label(
self.tooltip_window,
text=self.text,
background="#2b2b2b",
foreground="white",
relief="solid",
borderwidth=1,
font=("Arial", 9),
padx=8,
pady=4
)
label.pack()
def hide(self):
"""Hide the tooltip."""
if self.tooltip_window:
self.tooltip_window.destroy()
self.tooltip_window = None
class StatusBar(ctk.CTkFrame):
"""
Professional status bar for the application.
"""
def __init__(self, parent):
"""
Initialize status bar.
Args:
parent: Parent widget
"""
super().__init__(parent, height=30)
self.grid_columnconfigure(0, weight=1)
# Status message label
self.status_label = ctk.CTkLabel(
self,
text="Ready",
font=ctk.CTkFont(size=10),
anchor="w"
)
self.status_label.grid(row=0, column=0, padx=10, sticky="w")
# Connection indicator
self.connection_label = ctk.CTkLabel(
self,
text="⚫ Disconnected",
font=ctk.CTkFont(size=10),
text_color="gray"
)
self.connection_label.grid(row=0, column=1, padx=10)
# Activity indicator
self.activity_label = ctk.CTkLabel(
self,
text="",
font=ctk.CTkFont(size=10)
)
self.activity_label.grid(row=0, column=2, padx=10)
def set_status(self, message: str, type: str = "info"):
"""
Set status message.
Args:
message: Status message
type: Message type ('info', 'success', 'warning', 'error')
"""
colors = {
'info': 'white',
'success': 'green',
'warning': 'orange',
'error': 'red'
}
self.status_label.configure(
text=message,
text_color=colors.get(type, 'white')
)
def set_connection(self, connected: bool, mode: str = ""):
"""
Update connection status.
Args:
connected: Connection state
mode: Trading mode (PAPER/LIVE)
"""
if connected:
color = "orange" if mode == "PAPER" else "green"
symbol = "🟢" if mode == "LIVE" else "🟠"
self.connection_label.configure(
text=f"{symbol} Connected ({mode})",
text_color=color
)
else:
self.connection_label.configure(
text="⚫ Disconnected",
text_color="gray"
)
def show_activity(self, message: str = ""):
"""
Show activity indicator.
Args:
message: Activity message
"""
if message:
self.activity_label.configure(text=f"⏳ {message}")
else:
self.activity_label.configure(text="")
def create_labeled_entry(parent, label: str, row: int, tooltip: Optional[str] = None, **kwargs):
"""
Create labeled entry with optional tooltip.
Args:
parent: Parent widget
label: Label text
row: Grid row
tooltip: Optional tooltip text
**kwargs: Additional entry arguments
Returns:
Entry widget
"""
# Label
lbl = ctk.CTkLabel(parent, text=label + ":")
lbl.grid(row=row, column=0, padx=10, pady=5, sticky="e")
if tooltip:
ToolTip(lbl, tooltip)
# Entry
entry = ctk.CTkEntry(parent, **kwargs)
entry.grid(row=row, column=1, padx=10, pady=5, sticky="ew")
return entry
def show_confirmation(parent, title: str, message: str, on_confirm=None):
"""
Show confirmation dialog.
Args:
parent: Parent widget
title: Dialog title
message: Confirmation message
on_confirm: Callback function if confirmed
"""
dialog = ctk.CTkToplevel(parent)
dialog.title(title)
dialog.geometry("400x200")
dialog.transient(parent)
dialog.grab_set()
# Center dialog
dialog.update_idletasks()
x = parent.winfo_x() + (parent.winfo_width() // 2) - (dialog.winfo_width() // 2)
y = parent.winfo_y() + (parent.winfo_height() // 2) - (dialog.winfo_height() // 2)
dialog.geometry(f"+{x}+{y}")
# Message
ctk.CTkLabel(
dialog,
text=message,
font=ctk.CTkFont(size=12),
wraplength=350
).pack(pady=30, padx=20)
# Buttons
button_frame = ctk.CTkFrame(dialog, fg_color="transparent")
button_frame.pack(pady=10)
def on_yes():
dialog.destroy()
if on_confirm:
on_confirm()
def on_no():
dialog.destroy()
ctk.CTkButton(
button_frame,
text="Yes",
command=on_yes,
fg_color="green",
hover_color="darkgreen",
width=100
).pack(side="left", padx=10)
ctk.CTkButton(
button_frame,
text="No",
command=on_no,
width=100
).pack(side="left", padx=10)