-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui_window.py
346 lines (269 loc) · 10.2 KB
/
ui_window.py
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import threading
import tkinter
import time
import handle_date
import get_foundation
import get_stock_data_from_sina
global main_ui, window_update_thread, update_event
# default_char_width = 11
# default_char_height = 21
default_title = 'Stock & Fund List'
default_up_color = 'red'
default_down_color = 'green'
default_unknow_color = 'grey'
default_split_color = 'yellow'
default_uptime = 600 #00 #60 #600 # unit: second
default_fast_uptime = 6
extra_data_len = 30
max_window_height_line = 30
class MainWindow(object):
"""docstring for MainWindow"""
def __init__(self, tk, codes):
global window_update_thread, main_ui
# self.init = 0
self.firstupdate = 0
# super(MainWindow, self).__init__()
# self.win_width = width
# self.win_height = height
# Basic tkinker setting
self.win = tk #tkinter.Toplevel(tk)
self.win.title(default_title)
self.win.bind('<FocusIn>', self._focus)
self.win.bind('<FocusOut>', self._unfocus)
self.win.protocol("WM_DELETE_WINDOW", self._quit)
self.win.grid_rowconfigure(0, weight=1)
self.win.columnconfigure(0, weight=1)
# self.win.overrideredirect(True)
self.win.wm_attributes('-topmost',1)
# Stock and fund
self.stock = get_stock_data_from_sina.SinaStockData()
self.fund = get_foundation.FoundationData()
# self.fund.get_fund_by_code_num("110013")
# Set window position
sw = self.win.winfo_screenwidth()
sh = self.win.winfo_screenheight()
# self.win_x = (sw - width) / 2
# self.win_y = (sh - height) / 2
# self.win.geometry("%dx%d+%d+%d" %(self.win_width, self.win_height, self.win_x, self.win_y))
# Basic frame
self.basic_frame = tkinter.Frame(self.win)
self.basic_frame.grid(row=2, column=0, pady=(5, 0), sticky='nw')
self.basic_frame.grid_rowconfigure(0, weight=1)
self.basic_frame.grid_columnconfigure(0, weight=1)
# Set grid_propagate to False to allow 5-by-5 buttons resizing later
self.basic_frame.grid_propagate(False)
# Canvas
canvas = tkinter.Canvas(self.basic_frame)
canvas.grid(row=0, column=0, sticky="news")
# Link a scrollbar to the canvas
self.scrollbar = tkinter.Scrollbar(self.basic_frame, orient="vertical", command=canvas.yview)
self.scrollbar.grid(row=0, column=1, sticky='ns')
canvas.configure(yscrollcommand=self.scrollbar.set)
# Create a frame to show the real elements
self.show_frame = tkinter.Frame(canvas, bg="black")
canvas.create_window((0, 0), window=self.show_frame, anchor='nw')
# Showed labels
self.labs = [tkinter.Label() for i in range(len(codes))]
self.codes = codes
i = 0
for code in codes:
code_type = code.split(',')[0]
if 'split' == code_type:
split_data = ''
for j in range(int(20)):
split_data += '-'
self.labs[i] = tkinter.Label(self.show_frame, fg = default_split_color, bg = 'black', text = split_data)
else:
self.labs[i] = tkinter.Label(self.show_frame, fg ='red', bg = 'black', text = 'null' + ' : ' + 'null' + ' (' + 'null' + ' %)' + '-' + 'null')
self.labs[i].bind('<Button-1>', self._handlerAdaptor(self._label_click, para=code))
# print(code.split(',')[1])
self.labs[i].grid(row=i, column=0, sticky='news')
i = i + 1
# Update buttons frames idle tasks to let tkinter calculate buttons sizes
self.show_frame.update_idletasks()
self.labs_max_width = 0
self.lab_max_height = 0
for lab in self.labs:
if self.labs_max_width < lab.winfo_width():
self.labs_max_width = lab.winfo_width()
if len(self.labs) > max_window_height_line:
self.lab_max_height = max_window_height_line * self.labs[0].winfo_height()
else:
self.lab_max_height = len(self.labs) * self.labs[0].winfo_height()
# Resize the canvas frame to show
self.basic_frame.config(width=self.labs_max_width + self.scrollbar.winfo_width(),
height=self.lab_max_height)
# Set the canvas scrolling region
canvas.config(scrollregion=canvas.bbox("all"))
self.canvas = canvas
# canvas.bind_all('<Button-4>', self._on_mousewheel_up)
self.canvas.bind_all('<Button-4>', self._on_mousewheel_up)
self.canvas.bind_all('<Button-5>', self._on_mousewheel_down)
def _on_mousewheel_up(self, event):
# self.canvas.yview_scroll(-1*(event.delta/120), "units")
# print(event.delta)
self.canvas.yview_scroll(-120, 'units')
def _on_mousewheel_down(self, event):
# self.canvas.yview_scroll(-1*(event.delta/120), "units")
# print(event.delta)
self.canvas.yview_scroll(120, 'units')
def _handlerAdaptor(self, fun,**kwds):
return lambda event,fun=fun,kwds=kwds:fun(event,**kwds)
def _label_click(self, event, para):
print(para)
def _focus(self, event):
self.win.attributes('-alpha', 1)
def _unfocus(self, event):
self.win.attributes('-alpha', 1)
def _quit(self):
global window_update_thread
self.win.destroy()
window_update_thread.exit()
def update_labels(self):
# print("Update")
if 1 == self.firstupdate:
datedata = handle_date.DateTimeNow()
if datedata.get_weekday() > 5:
return
if datedata.get_passed_second() < datedata.get_special_passed_second(9,20,0) or \
(datedata.get_passed_second() > datedata.get_special_passed_second(11,40,0) and datedata.get_passed_second() < datedata.get_special_passed_second(13,20,0)) or \
datedata.get_passed_second() > datedata.get_special_passed_second(15,10,0):
return
self.firstupdate = 1
index = 0
for code in self.codes:
code_type = code.split(',')[0]
real_code = code.split(',')[1]
if 'split' != code_type:
# Stock
if 'stock' == code_type:
self.stock.get_stock_by_code(real_code)
if 'null' != self.stock.current_percent:
percent = float(self.stock.current_percent)
if percent < 0.0:
color = default_down_color
else:
color = default_up_color
else:
color = default_unknow_color
if 'null' == self.stock.stock_name:
self.labs[index].config(fg = color, text = self.stock.stock_code + ' : ' + self.stock.current_price +
' (' + self.stock.current_percent + ' %)' + '-' + self.stock.date)
else:
self.labs[index].config(fg = color, text = self.stock.stock_name + ' : ' + self.stock.current_price +
' (' + self.stock.current_percent + ' %)' + '-' + self.stock.date)
# Fund
elif 'fund' == code_type:
self.fund.get_fund_by_code_num(real_code)
if 'null' != self.fund.fund_estimated_percent:
percent = float(self.fund.fund_estimated_percent)
if percent < 0.0:
color = default_down_color
else:
color = default_up_color
else:
color = default_unknow_color
if 'null' == self.fund.fund_name:
self.labs[index].config(fg = color, text = self.fund.fund_code + ' : ' + self.fund.fund_estimated_value +
' (' + self.fund.fund_estimated_percent + ' %)' + '-' + self.fund.fund_estimated_date)
else:
self.labs[index].config(fg = color, text = self.fund.fund_name + ' : ' + self.fund.fund_estimated_value +
' (' + self.fund.fund_estimated_percent + ' %)' + '-' + self.fund.fund_estimated_date)
# else:
# split_data = ''
# for j in range(int(self.labs_max_width / 4)):
# split_data += '-'
# self.labs[index].config(text = split_data)
index += 1
# Update buttons frames idle tasks to let tkinter calculate buttons sizes
self.show_frame.update_idletasks()
# self.labs_max_width = 0
# self.lab_max_height = 0
for lab in self.labs:
if self.labs_max_width < lab.winfo_width():
self.labs_max_width = lab.winfo_width()
# print(lab.winfo_width())
# if len(self.labs) > max_window_height_line:
# self.lab_max_height = max_window_height_line * self.labs[0].winfo_height()
# else:
# self.lab_max_height = len(self.labs) * self.labs[0].winfo_height()
# Resize the canvas frame to show
self.basic_frame.config(width=self.labs_max_width + self.scrollbar.winfo_width())
def run(self):
self.win.mainloop()
def update_data():
global main_ui, update_event
update_event = threading.Event()
while True:
main_ui.update_labels()
# time.sleep(60)
datedata = handle_date.DateTimeNow()
if datedata.get_passed_second() > datedata.get_special_passed_second(14,30,0):
update_event.wait(timeout=default_fast_uptime)
else:
update_event.wait(timeout=default_uptime)
if update_event.isSet():
break
# print("1")
def run_ui_window(*window_paras):
global main_ui, window_update_thread
print("run_ui_window")
win = tkinter.Tk()
main_ui = MainWindow(win, window_paras[0])
window_update_thread = uithreadControl(1, 'update_window')
window_update_thread.start()
# main_ui
main_ui.run()
class uithreadControl(threading.Thread):
def __init__(self, threadID, name, *args):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.args = args
def run(self):
print ("开始线程:" + self.name)
if self.name == 'ui_window':
run_ui_window(self.args[0])
elif self.name == 'update_window':
update_data()
def exit(self):
global update_event
update_event.set()
# def Get_Window_Width_Height(code_list):
# print("Test")
# fund = get_foundation.FoundationData()
# stock = get_stock_data_from_sina.SinaStockData()
# max_width = 0
# max_height = default_char_height
# # if len(code_list) > max_window_height_line:
# # max_height *= max_window_height_line
# # else:
# # max_height *= len(code_list)
# for code in code_list:
# code_type = code.split(',')[0]
# real_code = code.split(',')[1]
# if 'stock' == code_type:
# stock.get_stock_by_code(real_code)
# if max_width < (len(stock.stock_name) + extra_data_len) * default_char_width:
# max_width = (len(stock.stock_name) + extra_data_len) * default_char_width
# elif 'fund' == code_type:
# fund.get_fund_by_code_num(real_code)
# if max_width < (len(fund.fund_name) + extra_data_len) * default_char_width:
# max_width = (len(fund.fund_name) + extra_data_len) * default_char_width
# # print(fund.fund_name)
# # print(max_width)
# return max_width, max_height * len(code_list)
if __name__ == '__main__':
print("Testing this module...")
# ui_window_main()
code_list = ['fund,000001', 'split,split', 'fund,000003', 'fund,000004', 'fund,000005', 'fund,000006', 'fund,000007', 'fund,000008']
thread2 = uithreadControl(2, 'ui_window', code_list)
# win.mainloop()
# test.update_labels()
# test.run()
thread2.start()
#
thread2.join()
# thread1.join()