好友
阅读权限10
听众
最后登录1970-1-1
|
本帖最后由 mgsoft 于 2023-3-1 13:56 编辑
python源码加了涨红色跌绿色,颜色可通过config.ini修改。
[Python] 纯文本查看 复制代码 # coding=utf-8
import time
from get_data_tencent import get_realtime_data
import tkinter as tk # 图形界面
import configparser # 加载config.ini
import random
import threading # 多线程模块
import keyboard # 全局热键
import os
import sys
####################################################################
# 更新数据,config.ini可修改更新时间
def data_update(stock_code_list):
while True:
DataColumn = conf.get( 'window', 'DataColumn' )
list_data3 = []
list_data4 = []
for i in stock_code_list:
tick = get_realtime_data(i)
try: # try excpt: 捕捉错误,然后提示
#list_data3.append(format(random.random(), '.2%').rjust(8,' ')) # 测试使用
list_data3.append(format(str(tick.price).center(10,' ')))
list_data4.append(format(float(tick.percent)/100,'.2%'))
except (AttributeError):
list_data3.append(' ')
list_data4.append(' ')
for i,item in enumerate (list_data3):
listbox3.delete(i)
listbox3.insert(i,item)
for i,item in enumerate (list_data4):
listbox4.delete(i)
listbox4.insert(i,item)
#根据涨跌改变每行文字颜色
for i,item in enumerate (list_data4):
if(float(list_data4[i].replace('%',''))<0):
if(DataColumn == '0' or DataColumn == '1'):
listbox1.itemconfig(i,fg=diecolor)
if(DataColumn == '0' or DataColumn == '2'):
listbox2.itemconfig(i,fg=diecolor)
listbox3.itemconfig(i,fg=diecolor)
listbox4.itemconfig(i,fg=diecolor)
if(float(list_data4[i].replace('%',''))>0):
if(DataColumn == '0' or DataColumn == '1'):
listbox1.itemconfig(i,fg=zhangcolor)
if(DataColumn == '0' or DataColumn == '2'):
listbox2.itemconfig(i,fg=zhangcolor)
listbox3.itemconfig(i,fg=zhangcolor)
listbox4.itemconfig(i,fg=zhangcolor)
time.sleep(times)
# 右键关闭窗口并退出程序运行
def window_off():
window.destroy() # window.quit() 绕过根window.mainloop()
exit() # python退出
# 右键重新加载,重新运行本程序,重新加载config.ini
def reload():
os.execvp(sys.executable, [sys.executable]+sys.argv)
####################################################################
if __name__ == "__main__":
# stock_code = '1123077'
# stock_code_list = ['1000002','1123077']
#读取配置文件获取代码列表 config.ini
conf = configparser.ConfigParser()
try:
conf.read(filenames="config.ini",encoding="utf-8-sig") # utf-8 读取记事本修改过的ini文件会出错
times = int( conf.get( 'window', 'time' ) ) # 数据刷新时间,单位:秒
#获取所有节点
# sections = conf.sections()
# print(sections)
code_list = conf.items(section = 'stock_code')
stock_code_list = []
for i in code_list:
# print(i[1])
stock_code_list.append(i[1])
# print(stock_code_list)
error_flag = None
except Exception as e:
error_flag = str(e)
print(error_flag)
stock_code_list = []
####################################################################
# 创建窗口
window = tk.Tk()
####################################################################
# 如果config.ini 的[stock_code]部分键名重复则提示出错
if error_flag != None:
text = tk.Text(window, width=50, height=30, undo=True, autoseparators=False)
text.pack()
text.insert(tk.INSERT,error_flag)
window.mainloop()
####################################################################
attributes = conf.get( 'window', 'attributes' )
if float(attributes) <= 0: #预防config.ini设置窗口透明度小于等于0
attributes = 1
# print(float(attributes))
window.attributes("-alpha",attributes) #窗口透明度
# window.config(background ="black") #窗口背景色
window.wm_attributes('-topmost',1) #置顶窗口
window.title('迷你信息') # 标题
fontsize=conf.get( 'window', 'fontsize' )#字体大小
colwidth=conf.get( 'window', 'colwidth' )#列宽度
bgcolor=conf.get( 'window', 'bgcolor' )#背景颜色
font=conf.get( 'window', 'font' )#字体
zhangcolor=conf.get( 'window', 'zhangcolor' )#涨颜色
diecolor=conf.get( 'window', 'diecolor' )#跌颜色
# 设置窗口大小变量
# 数据列数 DataColumn ,默认 0: 显示4列
# (可修改config.ini配置文件实现)
# 0: 代码 + 名称 + 价格 + 涨跌
# 1: 代码 + 价格 + 涨跌
# 2: 名称 + 价格 + 涨跌
# 3: 价格 + 涨跌
DataColumn = conf.get( 'window', 'DataColumn' )
if (DataColumn == '1' or DataColumn == '2'):
width = 3*int(colwidth)
elif DataColumn == '3':
width = 2*int(colwidth)
else:
width = 4*int(colwidth)
height = 19 * len(stock_code_list)
window.resizable(width=True, height=False) # 窗口宽度固定,高度固定
window.overrideredirect(True) #隐藏窗口
# 窗口居中,获取屏幕尺寸以计算布局参数,使窗口居屏幕右下角
screenwidth = window.winfo_screenwidth()
screenheight = window.winfo_screenheight()
size_geo = '%dx%d+%d+%d' % (width, height, (screenwidth-width), (screenheight-height)-40)
window.geometry(size_geo)
####################################################################
list_data1 = []
list_data2 = []
list_data3 = []
list_data4 = []
for i in stock_code_list:
tick = get_realtime_data(i)
try: # try excpt: 捕捉错误,然后提示
list_data1.append(tick.symbol)
list_data2.append(tick.name)
list_data3.append(str(tick.price).center(10,' '))
list_data4.append(format(float(tick.percent)/100,'.2%'))
except (AttributeError):
list_data1.append('代码错误')
list_data2.append(' ')
list_data3.append(' ')
list_data4.append(' ')
####################################################################
if (DataColumn == '1'):
#;1: 代码 + 价格 + 涨跌
listbox1 = tk.Listbox(window,width=int(colwidth),height=height,relief="ridge",background =bgcolor,font=(font,fontsize),border=0) # 底色 background =bgcolor
listbox1.place(x=0)
listbox3 = tk.Listbox(window,width=int(colwidth),height=height,relief="ridge",background =bgcolor,font=(font,fontsize),border=0) # 底色 background ="lightgreen"
listbox3.place(x=colwidth)
listbox4 = tk.Listbox(window,width=int(colwidth),height=height,relief="ridge",background =bgcolor,font=(font,fontsize),border=0) # 底色 background ="lightgreen"
listbox4.place(x=int(colwidth)*2)
for i,item in enumerate (list_data1):
listbox1.insert(i,item)
for i,item in enumerate (list_data3):
listbox3.insert(i,item)
for i,item in enumerate (list_data4):
listbox4.insert(i,item)
elif (DataColumn == '2'):
#;2: 名称 + 价格 + 涨跌
listbox2 = tk.Listbox(window,width=int(colwidth),height=height,relief="ridge",background =bgcolor,font=(font,fontsize),border=0) # 底色 background =bgcolor
listbox2.place(x=0)
listbox3 = tk.Listbox(window,width=int(colwidth),height=height,relief="ridge",background =bgcolor,font=(font,fontsize),border=0) # 底色 background ="lightgreen"
listbox3.place(x=colwidth)
listbox4 = tk.Listbox(window,width=int(colwidth),height=height,relief="ridge",background =bgcolor,font=(font,fontsize),border=0) # 底色 background ="lightgreen"
listbox4.place(x=int(colwidth)*2)
for i,item in enumerate (list_data2):
listbox2.insert(i,item)
for i,item in enumerate (list_data3):
listbox3.insert(i,item)
for i,item in enumerate (list_data4):
listbox4.insert(i,item)
elif (DataColumn == '0'):
# ;0: 代码 + 名称 + 价格 + 涨跌
listbox1 = tk.Listbox(window,width=int(colwidth),height=height,relief="ridge",background =bgcolor,font=(font,fontsize),border=0) # 底色 background =bgcolor
listbox1.place(x=0)
listbox2 = tk.Listbox(window,width=int(colwidth),height=height,relief="ridge",background =bgcolor,font=(font,fontsize),border=0) # 底色 background ="lightgreen"
listbox2.place(x=colwidth)
listbox3 = tk.Listbox(window,width=int(colwidth),height=height,relief="ridge",background =bgcolor,font=(font,fontsize),border=0) # 底色 background ="lightgreen"
listbox3.place(x=width*0.5)
listbox4 = tk.Listbox(window,width=int(colwidth),height=height,relief="ridge",background =bgcolor,font=(font,fontsize),border=0) # 底色 background ="lightgreen"
listbox4.place(x=width*0.75)
for i,item in enumerate (list_data1):
listbox1.insert(i,item)
for i,item in enumerate (list_data2):
listbox2.insert(i,item)
for i,item in enumerate (list_data3):
listbox3.insert(i,item)
for i,item in enumerate (list_data4):
listbox4.insert(i,item)
else:
# ;3: 价格 + 涨跌
listbox3 = tk.Listbox(window,width=int(colwidth),height=height,relief="ridge",background =bgcolor,font=(font,fontsize),border=0) # 底色 background ="lightgreen"
listbox3.place(x=0)
listbox4 = tk.Listbox(window,width=int(colwidth),height=height,relief="ridge",background =bgcolor,font=(font,fontsize),border=0) # 底色 background ="lightgreen"
listbox4.place(x=colwidth)
for i,item in enumerate (list_data3):
listbox3.insert(i,item)
for i,item in enumerate (list_data4):
listbox4.insert(i,item)
####################################################################
t1 = threading.Thread(target=data_update,args=(stock_code_list,)) #更新数据
t1.setDaemon(True)
t1.start()
####################################################################
# 创建右键退出菜单
menu = tk.Menu(window, tearoff=False)
menu.add_command(label="重载",command = reload )
menu.add_command(label="退出",command = window_off )
def command(event):
# 使用 post()在指定的位置显示弹出菜单
menu.post(event.x_root, event.y_root)
window.bind("<Button-3>", command)
#################################
# 隐藏主窗口,显示窗口
def window_hide(event):
window.withdraw()
def window_show(event):
window.deiconify()
# 全局热键 Alt+1
#window.bind("<Escape>",window_hide)
keyboard.add_hotkey('alt+1', window_hide, args=('From global keystroke',))
keyboard.add_hotkey('alt+2', window_show, args=('From global keystroke',))
#################################
def move_window(event):
window.geometry('+{0}+{1}'.format(window.winfo_pointerx(),window.winfo_pointery()))
# 鼠标拖动窗体
window.bind("<B1-Motion>",move_window)
####################################################################
window.mainloop()
config.ini内容
[C] 纯文本查看 复制代码 ; 配置文件 config.ini
; 需要导入模块import configparser
; 谨慎修改
;代码列表 (首位字符代表 sh:上交所 sz:深交所 hk:港股)
[stock_code]
; 键名 = 键值 (新增键名不要重复,否则报错)
stock_code5 = sz002733
; 窗体配置
[window]
; 透明度:0 - 1之间,
attributes = 0.5
;字体
font=宋体
;字体大小,默认10
fontsize=10
;列宽度,默认55
colwidth=55
;背景颜色 在线颜色工具https://www.sioe.cn/yingyong/yanse-rgb-16/
bgcolor=yellow
;涨显示颜色
zhangcolor=red
;跌显示颜色
diecolor=green
;数据刷新时间,单位:秒
time = 1
;数据列数DataColumn
;0: 代码 + 名称 + 价格 + 涨跌
;1: 代码 + 价格 + 涨跌
;2: 名称 + 价格 + 涨跌
;3: 价格 + 涨跌
DataColumn = 3
[/mw_shl_code] |
|