1 #/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 ''' 4 @author="livermorium116" 5 为了绕开公司内网而开发的 6 股票实时显示小程序 7 (1)程序基于QT5,pyQt5以及tushare库 8 (2)程序实时地简易显示时间、股票代码、盈亏数额 9 (3)使用方法:在终端直接运行python filename10 '''11 12 13 import sys14 from PyQt5.QtWidgets import *15 from PyQt5.QtGui import *16 from PyQt5.QtCore import *17 import tushare as ts18 import numpy as np19 import time20 21 22 23 24 25 class Example(QWidget):26 def __init__(self):27 super(Example, self).__init__()28 29 30 self.initUI()31 self.str1=""32 self.Flag=033 self.cost=19.57###把它修改成你的股票买入价格34 35 def initUI(self):36 QToolTip.setFont(QFont('SansSerif', 10))37 38 self.setToolTip('This is a QWidget widget')39 self.label=QLabel(self)40 self.label.setText("Begin.....")41 self.label.setFont(QFont("SansSerif",20))42 43 self.timer = QTimer()44 self.timer.setInterval(1000)45 self.timer.start()46 self.timer.timeout.connect(self.onTimerOut)47 48 49 50 self.setGeometry(300, 300, 380, 28)51 self.setWindowTitle('My Stock Price Indicator')52 self.show()53 54 55 56 57 def onTimerOut(self):58 59 df = ts.get_realtime_quotes("600030")##把它修改成你要购买的股票价格60 x=df["time"].to_dict()61 self.str1=str(x[0])62 63 64 x=df["price"].to_dict()65 self.str1 = self.str1 + " " + (x[0])66 67 sP=float(x[0])68 x=(sP-self.cost)*40069 self.str1=self.str1+ " " + str(x)70 if x > 0 :71 pe = QPalette()72 pe.setColor(QPalette.WindowText, Qt.red) # 设置字体颜色,红色表示盈利73 self.label.setPalette(pe)74 75 76 77 self.label.setText(self.str1)78 self.label.setVisible(self.Flag)79 self.Flag=1-self.Flag80 ##time.sleep(3)81 82 83 84 85 if __name__ == '__main__':86 app = QApplication(sys.argv)87 ex = Example()88 sys.exit(app.exec_())