forked from khscience/OSkhQuant
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGUIScheduler.py
More file actions
1860 lines (1596 loc) · 70.1 KB
/
GUIScheduler.py
File metadata and controls
1860 lines (1596 loc) · 70.1 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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import os
import logging
import multiprocessing
from datetime import datetime, time
from queue import Empty
import time as time_module
from PyQt5.QtWidgets import (QApplication, QMainWindow, QHBoxLayout, QVBoxLayout,
QWidget, QGroupBox, QCheckBox, QGridLayout, QTextEdit,
QPushButton, QStatusBar, QProgressBar, QLabel, QTimeEdit,
QMessageBox, QDesktopWidget, QSplitter, QComboBox, QFileDialog,
QSizePolicy)
from PyQt5.QtCore import Qt, QSettings, QThread, pyqtSignal, QTime, QTimer, QMutex
from PyQt5.QtGui import QIcon, QFont, QColor
import schedule
from khQTTools import KhQuTools
def supplement_data_worker(params, progress_queue, result_queue, stop_event):
"""
数据补充工作进程函数
在独立进程中运行,避免GIL限制
"""
# 多进程保护 - 防止在子进程中启动GUI
if __name__ != '__main__':
# 在子进程中,确保不会执行主程序代码
import multiprocessing
multiprocessing.current_process().name = 'SchedulerSupplementWorker'
try:
# 在子进程中导入需要的模块
import sys
import os
# 延迟导入并捕获任何GUI相关错误
try:
from khQTTools import supplement_history_data
except Exception as import_error:
# 如果导入失败,尝试直接从当前目录导入
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, current_dir)
try:
from khQTTools import supplement_history_data
except:
result_queue.put(('error', f"无法导入数据补充模块: {str(import_error)}"))
return
import logging
import time
# 配置子进程的日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# 进度和状态更新的时间控制
last_progress_time = 0
last_status_time = 0
update_interval = 0.5 # 500毫秒
def progress_callback(percent):
nonlocal last_progress_time
current_time = time.time()
# 降低进度更新频率限制,确保重要进度更新不被过滤
if current_time - last_progress_time >= 0.3 or percent >= 100 or percent % 10 == 0: # 每10%或最终完成时一定发送
try:
progress_queue.put(('progress', percent), timeout=1)
last_progress_time = current_time
print(f"[定时补充进程] 发送进度: {percent}%")
except Exception as e:
print(f"[定时补充进程] 发送进度失败: {e}")
def log_callback(message):
nonlocal last_status_time
current_time = time.time()
# 检查是否是补充数据的重要消息(成功、失败、错误等)
is_important = any(keyword in str(message) for keyword in [
'开始', '完成', '失败', '错误', '中断',
'补充', '数据成功', '数据时出错', '数据为空'
])
if is_important or current_time - last_status_time >= update_interval:
try:
progress_queue.put(('status', str(message)), timeout=1)
last_status_time = current_time
print(f"[定时补充进程] 发送状态: {message}")
except Exception as e:
print(f"[定时补充进程] 发送状态失败: {e}")
def check_interrupt():
# 检查停止事件
return stop_event.is_set()
# 执行数据补充
supplement_history_data(
stock_files=params['stock_files'],
field_list=["open", "high", "low", "close", "volume", "amount"],
period_type=params['period_type'],
start_date=params['start_date'],
end_date=params['end_date'],
time_range='all',
dividend_type='none',
progress_callback=progress_callback,
log_callback=log_callback,
check_interrupt=check_interrupt
)
print(f"[定时补充进程] 数据补充函数执行完成")
# 发送完成信号
result_queue.put(('success', '定时数据补充完成!'))
except Exception as e:
error_msg = f"定时补充数据过程中发生错误: {str(e)}"
result_queue.put(('error', error_msg))
logging.error(error_msg, exc_info=True)
class ScheduledSupplementThread(QThread):
"""定时数据补充线程(使用多进程后端)"""
progress = pyqtSignal(int)
finished = pyqtSignal(bool, str)
error = pyqtSignal(str)
status_update = pyqtSignal(str)
def __init__(self, params, parent=None):
super().__init__(parent)
self.params = params
self.running = True
self.mutex = QMutex()
# 在主线程中创建进程间通信队列
self.progress_queue = multiprocessing.Queue(maxsize=100)
self.result_queue = multiprocessing.Queue()
self.stop_event = multiprocessing.Event()
self.process = None
def run(self):
try:
if not self.isRunning():
return
# 参数验证
if not self.params.get('stock_files'):
raise ValueError("股票代码列表为空")
# 创建并启动子进程
self.process = multiprocessing.Process(
target=supplement_data_worker,
args=(self.params, self.progress_queue, self.result_queue, self.stop_event)
)
self.process.start()
# 在线程中直接监控进程间通信
while self.isRunning() and (self.process and self.process.is_alive()):
try:
# 检查进度消息
while True:
try:
msg_type, data = self.progress_queue.get_nowait()
if msg_type == 'progress':
self.progress.emit(data)
elif msg_type == 'status':
self.status_update.emit(data)
except Empty:
break
# 检查结果
try:
result_type, message = self.result_queue.get_nowait()
# 任务完成,设置运行状态为 False
self.mutex.lock()
self.running = False
self.mutex.unlock()
if result_type == 'success':
self.finished.emit(True, message)
else:
self.error.emit(message)
return # 完成后退出
except Empty:
pass
# 短暂休眠
self.msleep(100)
except Exception as e:
logging.error(f"监控进程时出错: {str(e)}")
break
# 检查进程是否异常退出
if self.process and not self.process.is_alive():
exit_code = self.process.exitcode
if exit_code != 0 and self.isRunning():
# 进程异常退出,设置运行状态为 False
self.mutex.lock()
self.running = False
self.mutex.unlock()
self.error.emit(f"定时补充进程异常退出,退出码: {exit_code}")
except Exception as e:
error_msg = f"启动定时补充进程时发生错误: {str(e)}"
logging.error(error_msg, exc_info=True)
if self.isRunning():
# 发生异常,设置运行状态为 False
self.mutex.lock()
self.running = False
self.mutex.unlock()
self.error.emit(error_msg)
self.finished.emit(False, error_msg)
def stop(self):
"""停止数据补充"""
self.mutex.lock()
self.running = False
# 停止多进程
try:
if self.stop_event:
self.stop_event.set()
if self.process and self.process.is_alive():
# 等待进程结束
self.process.join(timeout=5)
# 如果进程还没结束,强制终止
if self.process.is_alive():
self.process.terminate()
self.process.join(timeout=2)
if self.process.is_alive():
self.process.kill()
except Exception as e:
logging.error(f"停止定时补充进程时出错: {str(e)}")
self.mutex.unlock()
def isRunning(self):
self.mutex.lock()
result = self.running
self.mutex.unlock()
return result
class GUIScheduler(QMainWindow):
"""定时数据调度器"""
def __init__(self):
super().__init__()
# 检测屏幕分辨率并设置字体缩放
self.font_scale = self.detect_screen_resolution()
# 初始化状态变量
self.is_scheduled_running = False
self.supplement_thread = None
self.schedule_timer = QTimer()
self.schedule_timer.timeout.connect(self.check_schedule)
# 初始化xtdata工具和股票名称缓存
from khQTTools import KhQuTools
self.tools = KhQuTools()
self.stock_names_cache = {}
# 初始化自定义文件列表
self.custom_files = []
self.initUI()
self.load_stock_names()
# 在界面完全构建后,清理局部字体并统一应用缩放样式
QTimer.singleShot(0, self.apply_scaled_styles)
def detect_screen_resolution(self):
"""检测屏幕分辨率并返回字体缩放比例"""
screen = QDesktopWidget().screenGeometry()
width = screen.width()
height = screen.height()
# 根据屏幕宽度确定字体缩放比例(与主界面保持一致)
if width >= 3840: # 4K及以上分辨率
return 1.8
elif width >= 2560: # 2K分辨率
return 1.4
elif width >= 1920: # 1080P分辨率
return 1.0
else: # 低分辨率
return 0.8
def get_scaled_stylesheet(self):
"""获取根据分辨率缩放的样式表"""
# 基础字体大小
base_sizes = {
'small': 12,
'normal': 14,
'large': 16,
'xl': 18,
'xxl': 24,
'xxxl': 30
}
# 计算缩放后的字体大小
scaled_sizes = {k: int(v * self.font_scale) for k, v in base_sizes.items()}
return f"""
/* 主窗口和基础样式 */
QMainWindow {{
background-color: #2b2b2b;
color: #e8e8e8;
font-size: {scaled_sizes['normal']}px;
}}
QWidget {{
background-color: #2b2b2b;
color: #e8e8e8;
font-size: {scaled_sizes['normal']}px;
}}
/* 分组框样式 */
QGroupBox {{
background-color: #333333;
border: 1px solid #404040;
border-radius: 6px;
margin-top: 1em;
padding-top: 1em;
color: #e8e8e8;
font-size: {scaled_sizes['normal']}px;
}}
QGroupBox::title {{
subcontrol-origin: margin;
left: 10px;
padding: 0 5px;
color: #e8e8e8;
font-weight: bold;
background-color: #333333;
font-size: {scaled_sizes['normal']}px;
}}
/* 标签样式 */
QLabel {{
color: #e8e8e8;
background-color: transparent;
font-size: {scaled_sizes['normal']}px;
}}
/* 输入框样式 */
QLineEdit {{
background-color: #404040;
border: 1px solid #4d4d4d;
border-radius: 4px;
padding: 5px;
color: #e8e8e8;
selection-background-color: #666666;
font-size: {scaled_sizes['normal']}px;
}}
/* 按钮样式 */
QPushButton {{
background-color: #505050;
border: none;
border-radius: 4px;
padding: 8px 16px;
color: #e8e8e8;
min-width: 80px;
font-weight: bold;
font-size: {scaled_sizes['normal']}px;
}}
QPushButton:hover {{
background-color: #606060;
}}
QPushButton:pressed {{
background-color: #454545;
}}
QPushButton:disabled {{
background-color: #404040;
color: #808080;
}}
/* 下拉框样式 */
QComboBox {{
background-color: #404040;
border: 1px solid #4d4d4d;
border-radius: 4px;
padding: 5px;
color: #e8e8e8;
min-width: 100px;
font-size: {scaled_sizes['normal']}px;
}}
/* 复选框样式 */
QCheckBox {{
color: #e8e8e8;
spacing: 5px;
font-size: {scaled_sizes['normal']}px;
background-color: transparent;
}}
/* 时间编辑器样式 */
QTimeEdit {{
background-color: #404040;
border: 1px solid #4d4d4d;
border-radius: 4px;
padding: 5px;
color: #e8e8e8;
font-size: {scaled_sizes['normal']}px;
}}
/* 文本编辑器样式 */
QTextEdit {{
background-color: #2b2b2b;
color: #e8e8e8;
border: 1px solid #404040;
font-size: {scaled_sizes['normal']}px;
}}
/* 进度条样式 */
QProgressBar {{
background-color: #404040;
border: 1px solid #4d4d4d;
border-radius: 5px;
text-align: center;
font-size: {scaled_sizes['normal']}px;
}}
/* 状态栏样式 */
QStatusBar {{
background-color: #333333;
color: #e8e8e8;
border-top: 1px solid #4d4d4d;
font-size: {scaled_sizes['normal']}px;
}}
"""
def get_complete_scaled_stylesheet(self):
"""获取包含强制覆盖的完整缩放样式表"""
# 计算缩放后的字体大小和控件尺寸
font_14 = int(14 * self.font_scale)
font_16 = int(16 * self.font_scale)
font_12 = int(12 * self.font_scale)
# 计算控件高度(基于字体大小)
button_height = max(30, int(30 * self.font_scale))
input_height = max(25, int(25 * self.font_scale))
combo_height = max(25, int(25 * self.font_scale))
# 基础样式表
base_style = self.get_scaled_stylesheet()
# 强制覆盖样式,包含高度调整
override_style = f"""
/* 强制字体大小覆盖 */
* {{ font-size: {font_14}px !important; }}
QLabel {{ font-size: {font_14}px !important; min-height: {input_height}px; }}
QPushButton {{
font-size: {font_14}px !important;
min-height: {button_height}px;
padding: {max(4, int(4 * self.font_scale))}px {max(8, int(8 * self.font_scale))}px;
}}
/* 复选框:同步主界面的完整样式,确保间距和尺寸正确缩放 */
QCheckBox {{
spacing: {max(5, int(5 * self.font_scale))}px;
padding: {max(5, int(5 * self.font_scale))}px 0;
}}
QCheckBox::indicator {{
width: {max(20, int(20 * self.font_scale))}px;
height: {max(20, int(20 * self.font_scale))}px;
}}
QComboBox {{
font-size: {font_14}px !important;
min-height: {combo_height}px;
padding: {max(3, int(3 * self.font_scale))}px;
}}
QLineEdit {{
font-size: {font_14}px !important;
min-height: {input_height}px;
padding: {max(3, int(3 * self.font_scale))}px;
}}
QGroupBox {{
font-size: {font_14}px !important;
padding-top: {max(15, int(15 * self.font_scale))}px;
}}
QGroupBox::title {{
font-size: {font_14}px !important;
padding: {max(3, int(3 * self.font_scale))}px {max(5, int(5 * self.font_scale))}px;
background-color: transparent; /* 与主背景一致,消除标题底色块 */
subcontrol-origin: margin;
subcontrol-position: top left;
}}
QTimeEdit {{
font-size: {font_14}px !important;
min-height: {combo_height}px;
padding: {max(3, int(3 * self.font_scale))}px;
}}
QTextEdit {{ font-size: {font_14}px !important; }}
QProgressBar {{
font-size: {font_14}px !important;
min-height: {max(16, int(16 * self.font_scale))}px;
}}
QStatusBar {{ font-size: {font_14}px !important; }}
QCheckBox {{
font-size: {font_14}px !important;
min-height: {max(16, int(16 * self.font_scale))}px;
}}
QSpinBox, QDoubleSpinBox {{
font-size: {font_14}px !important;
min-height: {input_height}px;
padding: {max(3, int(3 * self.font_scale))}px;
}}
"""
return base_style + override_style
def apply_scaled_styles(self):
"""在界面构建完成后,统一清理并应用缩放样式,避免被局部样式覆盖"""
# 1) 递归移除所有子控件样式中的 font-size
try:
from PyQt5.QtWidgets import QWidget
import re
for child in self.findChildren(QWidget):
if hasattr(child, 'styleSheet'):
ss = child.styleSheet() or ''
if 'font-size' in ss:
ss = re.sub(r"font-size\s*:\s*\d+px;?", "", ss)
child.setStyleSheet(ss)
except Exception:
pass
# 2) 重新应用完整缩放样式表
self.setStyleSheet(self.get_complete_scaled_stylesheet())
def get_icon_path(self, icon_name):
"""获取图标文件的正确路径"""
if getattr(sys, 'frozen', False):
# 打包环境 - 使用sys._MEIPASS
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, 'icons', icon_name)
else:
# 备用路径
return os.path.join(os.path.dirname(sys.executable), 'icons', icon_name)
else:
# 开发环境
return os.path.join(os.path.dirname(__file__), 'icons', icon_name)
def center_main_window(self):
"""将主窗口居中显示在主屏幕上"""
desktop = QDesktopWidget()
# 使用主屏幕而不是跟随鼠标位置
primary_screen = desktop.primaryScreen()
screen = desktop.screenGeometry(primary_screen)
x = (screen.width() - self.width()) // 2
y = (screen.height() - self.height()) // 2
self.move(x, y)
def initUI(self):
"""初始化用户界面"""
# 设置窗口标题栏颜色(仅适用于Windows)
if sys.platform == 'win32':
try:
from ctypes import windll, c_int, byref, sizeof
from ctypes.wintypes import DWORD
# 定义必要的Windows API常量
DWMWA_USE_IMMERSIVE_DARK_MODE = 20
DWMWA_CAPTION_COLOR = 35 # 标题栏颜色
# 启用深色模式
windll.dwmapi.DwmSetWindowAttribute(
int(self.winId()),
DWMWA_USE_IMMERSIVE_DARK_MODE,
byref(c_int(2)), # 2 means true
sizeof(c_int)
)
# 设置标题栏颜色
caption_color = DWORD(0x2b2b2b) # 使用与主界面相同的颜色
windll.dwmapi.DwmSetWindowAttribute(
int(self.winId()),
DWMWA_CAPTION_COLOR,
byref(caption_color),
sizeof(caption_color)
)
except Exception as e:
logging.warning(f"设置标题栏深色模式失败: {str(e)}")
self.setWindowTitle("定时数据补充工具 - 看海量化交易系统")
self.setGeometry(100, 100, 1200, 700)
# 将窗口居中显示
self.center_main_window()
# 设置窗口图标
icon_file = self.get_icon_path('stock_icon.ico')
if os.path.exists(icon_file):
self.setWindowIcon(QIcon(icon_file))
else:
# 尝试png格式
icon_file_png = self.get_icon_path('stock_icon.png')
if os.path.exists(icon_file_png):
self.setWindowIcon(QIcon(icon_file_png))
else:
logging.warning(f"图标文件不存在: {icon_file} 和 {icon_file_png}")
# 在界面创建前就设置完整的样式表,避免闪烁
self.setStyleSheet(self.get_complete_scaled_stylesheet())
# 创建中心部件
central_widget = QWidget()
central_widget.setObjectName("centralWidget")
self.setCentralWidget(central_widget)
# 创建主布局
main_layout = QHBoxLayout(central_widget)
# 创建分割器
splitter = QSplitter(Qt.Horizontal)
splitter.setStyleSheet("background-color: #2b2b2b;")
# 创建左侧配置区域
config_widget = self.create_config_widget()
config_widget.setFixedWidth(480)
# 创建右侧日志和控制区域
log_widget = self.create_log_widget()
# 添加到分割器
splitter.addWidget(config_widget)
splitter.addWidget(log_widget)
splitter.setStretchFactor(0, 0) # 左侧配置区固定宽度
splitter.setStretchFactor(1, 1) # 右侧日志区可伸缩
main_layout.addWidget(splitter)
# 创建状态栏
self.status_bar = QStatusBar()
self.setStatusBar(self.status_bar)
# 创建进度条
self.progress_bar = QProgressBar()
self.progress_bar.setVisible(False)
self.status_bar.addPermanentWidget(self.progress_bar)
# 重新应用完整缩放样式表(确保不被覆盖)
self.setStyleSheet(self.get_complete_scaled_stylesheet())
# 原有样式注释掉,已整合到完整样式表中
"""
# 注释掉原有的硬编码样式,避免覆盖缩放样式表
self.setStyleSheet('''
/* 主窗口和基础样式 */
QMainWindow {
background-color: #2b2b2b;
color: #e8e8e8;
}
QWidget {
background-color: #2b2b2b;
color: #e8e8e8;
}
/* 确保所有子组件背景色 */
QWidget#centralWidget {
background-color: #2b2b2b;
}
/* 状态栏样式 */
QStatusBar {
background-color: #3a3a3a;
color: #e8e8e8;
border-top: 1px solid #4d4d4d;
padding: 3px;
font-size: 14px;
}
/* 进度条样式 */
QProgressBar {
background-color: #404040;
border: 1px solid #4d4d4d;
border-radius: 5px;
text-align: center;
font-size: 14px;
color: #e8e8e8;
}
QProgressBar::chunk {
background-color: #007acc;
border-radius: 4px;
}
/* 滚动条样式 */
QScrollBar:vertical {
background-color: #3a3a3a;
width: 15px;
border: none;
}
QScrollBar::handle:vertical {
background-color: #5a5a5a;
border-radius: 7px;
min-height: 20px;
}
QScrollBar::handle:vertical:hover {
background-color: #6a6a6a;
}
QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {
border: none;
background: none;
}
QScrollBar:horizontal {
background-color: #3a3a3a;
height: 15px;
border: none;
}
QScrollBar::handle:horizontal {
background-color: #5a5a5a;
border-radius: 7px;
min-width: 20px;
}
QScrollBar::handle:horizontal:hover {
background-color: #6a6a6a;
}
QScrollBar::add-line:horizontal, QScrollBar::sub-line:horizontal {
border: none;
background: none;
}
/* 分割器样式 */
QSplitter::handle {
background-color: #404040;
}
QSplitter::handle:horizontal {
width: 2px;
}
QSplitter::handle:vertical {
height: 2px;
}
/* 工具提示样式 */
QToolTip {
background-color: #555555;
color: #e8e8e8;
border: 1px solid #666666;
padding: 4px;
border-radius: 3px;
font-size: 14px;
}
''')
"""
def create_config_widget(self):
"""创建配置区域"""
config_widget = QWidget()
config_widget.setStyleSheet("background-color: #2b2b2b;")
# 主布局
main_layout = QVBoxLayout(config_widget)
main_layout.setSpacing(15)
main_layout.setContentsMargins(20, 20, 20, 20)
# 标题
title_label = QLabel("定时数据补充配置")
title_font = QFont()
title_font.setPointSize(16)
title_font.setBold(True)
title_label.setFont(title_font)
title_label.setStyleSheet("color: #e8e8e8; margin-bottom: 10px;")
main_layout.addWidget(title_label)
# 添加各个配置组
self.add_stock_pool_group(main_layout)
self.add_period_group(main_layout)
self.add_schedule_group(main_layout)
self.add_control_group(main_layout)
# 添加弹性空间
main_layout.addStretch()
return config_widget
def add_stock_pool_group(self, layout):
"""添加股票池选择组"""
stock_group = QGroupBox("股票池选择")
stock_group.setStyleSheet("""
QGroupBox {
color: #e8e8e8;
font-weight: bold;
font-size: 14px;
border: 1px solid #4d4d4d;
border-radius: 5px;
margin-top: 10px;
padding-top: 10px;
}
QGroupBox::title {
subcontrol-origin: margin;
subcontrol-position: top left;
padding: 0 5px;
}
""")
stock_layout = QVBoxLayout()
# 股票池复选框
self.stock_pool_checkboxes = {}
stock_pools = {
'hs_a': '沪深A股',
'gem': '创业板',
'sci': '科创板',
'zz500': '中证500成分股',
'hs300': '沪深300成分股',
'sz50': '上证50成分股',
'indices': '常用指数',
'convertible_bonds': '沪深转债',
'custom': '自定义股票池'
}
# 创建网格布局
grid_layout = QGridLayout()
grid_layout.setHorizontalSpacing(24)
grid_layout.setVerticalSpacing(10)
row = 0
col = 0
for pool_type, display_name in stock_pools.items():
if pool_type == 'custom':
# 为自定义股票池创建特殊的标签和复选框布局
custom_widget = QWidget()
custom_layout = QHBoxLayout(custom_widget)
custom_layout.setContentsMargins(0, 0, 0, 0)
custom_layout.setSpacing(8)
# 复选框
checkbox = QCheckBox()
checkbox.setStyleSheet("color: #e8e8e8; font-size: 14px; padding: 5px;")
checkbox.stateChanged.connect(self.on_stock_pool_changed)
self.stock_pool_checkboxes[pool_type] = checkbox
custom_layout.addWidget(checkbox)
# 创建可点击的标签
custom_label = QLabel(display_name)
custom_label.setStyleSheet("""
QLabel {
color: #e8e8e8;
text-decoration: underline;
font-size: 14px;
padding: 5px;
}
""")
custom_label.setCursor(Qt.PointingHandCursor)
custom_label.mousePressEvent = self.open_custom_pool
custom_layout.addWidget(custom_label)
custom_layout.addStretch()
# 将自定义股票池放在新的一行
if col != 0:
row += 1
col = 0
grid_layout.addWidget(custom_widget, row, col, 1, 2) # 跨越2列
row += 1
col = 0
else:
checkbox = QCheckBox(display_name)
checkbox.setStyleSheet("color: #e8e8e8; padding: 5px;")
# 给复选框一些最小宽度,避免长文本换行或挤压
checkbox.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed)
checkbox.stateChanged.connect(self.on_stock_pool_changed)
self.stock_pool_checkboxes[pool_type] = checkbox
grid_layout.addWidget(checkbox, row, col)
col += 1
if col >= 1: # 改为1列布局,彻底解决挤压问题
col = 0
row += 1
stock_layout.addLayout(grid_layout)
# 添加自定义文件管理按钮
button_layout = QHBoxLayout()
button_layout.setSpacing(8)
add_custom_button = QPushButton("添加自定义列表")
add_custom_button.setMinimumHeight(35)
add_custom_button.setStyleSheet("""
QPushButton {
background-color: #404040;
color: #e8e8e8;
border: 1px solid #4d4d4d;
border-radius: 3px;
padding: 8px;
font-size: 14px;
}
QPushButton:hover {
background-color: #454545;
}
QPushButton:pressed {
background-color: #505050;
}
""")
add_custom_button.clicked.connect(self.add_custom_stock_file)
button_layout.addWidget(add_custom_button)
clear_button = QPushButton("清空选择")
clear_button.setMinimumHeight(35)
clear_button.setStyleSheet("""
QPushButton {
background-color: #404040;
color: #e8e8e8;
border: 1px solid #4d4d4d;
border-radius: 3px;
padding: 8px;
font-size: 14px;
}
QPushButton:hover {
background-color: #454545;
}
QPushButton:pressed {
background-color: #505050;
}
""")
clear_button.clicked.connect(self.clear_stock_pools)
button_layout.addWidget(clear_button)
stock_layout.addLayout(button_layout)
# 添加预览区域
self.stock_files_preview = QTextEdit()
self.stock_files_preview.setMaximumHeight(80)
self.stock_files_preview.setMinimumHeight(60)
self.stock_files_preview.setReadOnly(True)
self.stock_files_preview.setText("未选择任何股票池")
self.stock_files_preview.setStyleSheet("""
QTextEdit {
background-color: #2b2b2b;
color: #e8e8e8;
border: 1px solid #4d4d4d;
border-radius: 3px;
padding: 8px;
font-size: 12px;
}
""")
stock_layout.addWidget(self.stock_files_preview)
# 股票池统计标签
self.stock_pool_label = QLabel("未选择任何股票池")
self.stock_pool_label.setStyleSheet("color: #b0b0b0; font-size: 13px; padding: 5px;")
stock_layout.addWidget(self.stock_pool_label)
stock_group.setLayout(stock_layout)
layout.addWidget(stock_group)
def add_period_group(self, layout):
"""添加周期类型组"""
period_group = QGroupBox("周期类型选择")
period_group.setStyleSheet("""
QGroupBox {
color: #e8e8e8;
font-weight: bold;
font-size: 14px;
border: 1px solid #4d4d4d;
border-radius: 5px;
margin-top: 10px;
padding-top: 10px;
}
QGroupBox::title {
subcontrol-origin: margin;
subcontrol-position: top left;
padding: 0 5px;
}
""")
period_layout = QVBoxLayout()
# 周期类型复选框
self.period_checkboxes = {}
periods = {
'tick': 'Tick数据',
'1m': '1分钟线',
'5m': '5分钟线',
'1d': '日线'
}
# 将水平布局改为2x2的网格布局
grid_layout = QGridLayout()
grid_layout.setHorizontalSpacing(int(24 * self.font_scale))
grid_layout.setVerticalSpacing(int(10 * self.font_scale))
checkboxes = []
for period_type, display_name in periods.items():
checkbox = QCheckBox(display_name)
checkbox.stateChanged.connect(self.on_period_changed)
self.period_checkboxes[period_type] = checkbox
checkboxes.append(checkbox)
# 手动将复选框添加到网格中
grid_layout.addWidget(checkboxes[0], 0, 0) # Tick
grid_layout.addWidget(checkboxes[1], 0, 1) # 1分钟
grid_layout.addWidget(checkboxes[2], 1, 0) # 5分钟
grid_layout.addWidget(checkboxes[3], 1, 1) # 日线
period_layout.addLayout(grid_layout)
period_group.setLayout(period_layout)
layout.addWidget(period_group)
def add_schedule_group(self, layout):
"""添加定时设置组"""