利用python的PyQt5设计编译桌面看盘小工具
本文最后更新于:2024年12月14日 下午
前言
因为平时有看盘的习惯,基于在某些场景下不适合安装打开看盘软件。比如,在公司上班的时候,之前也都是想看的时候打开网页看一下,但还是不方便。所以今儿有空就利用python的PyQt5编写了一个简易的桌面看盘小工具,因为只需要看自己想看的那几只票的行情,所以也没有什么花里胡哨的功能,主打的就是一个简约,实用。欢迎有需要的同学试用
代码
import sys
import requests
from PyQt5 import QtWidgets, QtCore, QtGui
import configparser
import keyboard
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Referer': 'https://xueqiu.com/',
}
class ReminderApp(QtWidgets.QMainWindow):
def __init__(self):
super(ReminderApp, self).__init__()
self.resize(position[2], position[3])
self.setWindowTitle("桌面看盘工具")
self.setWindowFlag(QtCore.Qt.SplashScreen, True)
self.setWindowFlag(QtCore.Qt.FramelessWindowHint, True)
self.setWindowFlag(QtCore.Qt.WindowStaysOnTopHint, True)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.setStyleSheet("background:" + bg_color)
self.label = QtWidgets.QLabel(self)
self.label.setGeometry(QtCore.QRect(position[0], position[1], position[2], position[3]))
self.label.setFont(QtGui.QFont(Font, Fontsize))
self.label.setTextFormat(QtCore.Qt.RichText)
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.update_stock_data)
self.timer.start(1000)
self.update_stock_data()
keyboard.add_hotkey(Quick_exit, self.exit_application)
def exit_application(self):
QtWidgets.QApplication.quit()
def update_stock_data(self):
stock_data = self.get_stock_data()
if stock_data:
stock_name_mapping = dict(zip(stock_symbols, name))
display_text = "<br>".join([
f"<font color={zhang_color}> {symbol} {stock_name_mapping.get(symbol, symbol)}: {data['current']} {data['percent']}%</font>" if data['percent'] >= 0 else
f"<font color={die_color}> {symbol} {stock_name_mapping.get(symbol, symbol)}: {data['current']} {data['percent']}%</font>"
for symbol, data in stock_data.items()
])
self.label.setText(display_text)
def get_stock_data(self):
stock_data = {}
for symbol in stock_symbols:
url = f"https://stock.xueqiu.com/v5/stock/realtime/quotec.json?symbol={symbol}"
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
data = response.json()
if isinstance(data["data"], list) and len(data["data"]) > 0:
quote_data = data["data"][0]
stock_data[symbol] = {
"current": quote_data.get("current", None),
"percent": quote_data.get("percent", None),
}
else:
print(f"No data found for {symbol}")
except requests.exceptions.RequestException as e:
print(f"Error fetching data for {symbol}: {e}")
return stock_data
def StartReminder():
app = QtWidgets.QApplication(sys.argv)
main_window = ReminderApp()
main_window.showFullScreen()
sys.exit(app.exec())
if __name__ == '__main__':
StartReminder()
配置说明
[ConfigSettings]
Font = "Courier New" #设置字体
Fontsize = 14 #设置字体大小
zhang_color = "#dd2200" #设置涨的显示颜色
die_color = "#009933" #设置跌的显示颜色
bg_color = "#00000000" #设置窗口背景颜色
position = 1500,0,400,150 #设置显示的位置,参数分别为:x坐标,y坐标,宽度,高度
Quick_exit = Ctrl+Alt+K #设置快捷退出的按键
List = SZ002288,SH600305,SH600773,SZ300512,SH688584 #股票代码
Names = 超华科技,恒顺醋业,西藏城投,中亚股份,上海合晶 #股票对应的中文名字
示例
下载
123盘:https://www.123pan.com/s/BZouVv-NvWF3.html
度盘:https://pan.baidu.com/s/1VbTztbnHsWoOe1UQJNnkNQ?pwd=f7m2
阿里盘:https://www.alipan.com/s/uyowW3uBMqM
微信支付
支付宝支付
“请博主喝咖啡☕️”