python利用PyQt5和QTDesginer开发GUI应用(二)、股票查询工具

python应用GUI开发系列

python利用PyQt5和QTDesginer开发GUI应用(一)、环境准备及demo

-------------------------------------------------------------------------------------------------------------------

这里使用tushare实时采集财经类信息。是个爬虫包,作者大大是我的同事Jimmy,Liu。

建议大家试用一下:

http://tushare.org/newsevent.html

https://tushare.pro/

--------------------------------------------------------------------------------------------------------------------

先看ui:源码在后面或查看附件。源码

功能:

查看股票信息:

实时交易:

历史大单:

历史交易:

大盘指数:

新股:

龙虎榜:

近五日上榜统计:

fund_search_show_ui.py

import sys
from PyQt5 import QtWidgets,QtCore
from PyQt5.QtCore import QDate,QDateTime,QTime,Qt
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from fund_search import Ui_Form
import tushare as ts
import pandas as pd
import datetime
#显示所有列
pd.set_option('display.max_rows',500)
pd.set_option('display.max_columns',500)
pd.set_option('display.width',1000)

class mywindow(QtWidgets.QWidget, Ui_Form):
    fund_id = '002400'
    plaintxt = ''
    vol='100'
    deal_date=datetime.datetime.now().strftime("%Y-%m-%d")
    head_cnt=20
    cm_basic={
        "code": "代码",
        "name": "名称",
        "industry": "细分行业",
        "area": "地区",
        "pe": "市盈率",
        "outstanding": "流通股本",
        "totals": "总股本(万)",
        "totalAssets": "总资产(万)",
        "liquidAssets": "流动资产",
        "fixedAssets": "固定资产",
        "reserved": "公积金",
        "reservedPerShare": "每股公积金",
        "eps": "每股收益",
        "bvps": "每股净资",
        "pb": "市净率",
        "timeToMarket": "上市日期"
    }
    cm_dapan={
        "code": "指数代码",
        "name": "指数名称",
        "change": "涨跌幅",
        "open": "开盘价",
        "preclose": "昨日收盘价",
        "close": "收盘价",
        "high": "最高价",
        "low": "最低价",
        "volume": "成交量(手)",
        "amount": "成交金额(亿元)"

    }
    cm_realtime={
        "open": "今日开盘价",
        "pre_close": "昨日收盘价",
        "price": "当前价格",
        "high": "今日最高价",
        "low": "今日最低价",
        "bid": "竞买价,即“买一”报价",
        "ask": "竞卖价,即“卖一”报价",
        "volumn": "成交量 maybe you need do volumn/100",
        "amount": "成交金额(元 CNY)",
        "b1_v": "委买一(笔数 bid volume)",
        "b1_p": "委买一(价格 bid price)",
        "a1_v": "委卖一(笔数 ask volume)",
        "a1_p": "委卖一(价格 ask price)",
        "date": "日期",
        "time": "时间"
    }
    cm_today={
        "time": "成交时间",
        "price": "成交价格",
        "pchange": "价格变动",
        "change": "价格变动%",
        "volume": "成交手",
        "amount": "成交金额(元)",
        "type": "买卖类型"
    }
    cm_dadan={
        "code": "股票代码",
        "name": "股票名称",
        "time": "交易时间",
        "price": "价格",
        "volume": "成交量",
        "preprice": "前一笔价格",
        "type": "类型(买、卖、中性盘)"

    }
    cm_xingu={
        "code": "股票代码",
        "name": "股票名称",
        "ipo_date": "上网发行日期",
        "issue_date": "上市日期",
        "amount": "发行数量(万股)",
        "markets": "上网发行数量(万股)",
        "price": "发行价格(元)",
        "pe": "发行市盈率",
        "limit": "个人申购上限(万股)",
        "funds": "募集资金(亿元)",
        "ballot": "网上中签率(%)"
    }
    cm_longhu_cnt={
        "code": "代码",
        "name": "名称",
        "count": "上榜次数",
        "bamount": "累积购买额(万)",
        "samount": "累积卖出额(万)",
        "net": "净额(万)",
        "bcount": "买入席位数",
        "scount": "卖出席位数"
    }
    cm_longhu={
        "code": "代码",
        "name": "名称",
        "pchange": "涨跌幅 ",
        "amount": "龙虎榜成交额(万)",
        "buy": "买入额(万)",
        "bratio": "占总成交比例",
        "sell": "卖出额(万)",
        "sratio": "占总成交比例",
        "reason": "上榜原因",
        "date": "日期"
    }
    def __init__(self):
        super(mywindow, self).__init__()
        self.setupUi(self)
        self.setWindowTitle('股票查询工具')
        self.comboBox.addItems(['查看信息','实时分笔','今日分笔'])
        self.dadan_vol_lineEdit.setText(self.vol)
        self.Inputtext.setText(self.fund_id)
        now = QDateTime.currentDateTime()
        self.deal_date = now.toString(Qt.ISODate)[:10]
        self.dateEdit.setCalendarPopup(True)
        self.dateEdit.setDisplayFormat("yyyy-MM-dd")
        self.df_stock = ts.get_stock_basics()
        self.df_stock.index.name='[代码]'
        # df[df.index == '688126']
        # self.beginButton.clicked.connect = self.click_search  # 点击按钮beginButton时调用searchFund函数
        # self.beginButton.mousePressEvent  = self.click_search  # 点击按钮beginButton时调用searchFund函数

    def check_fundid(self):
        self.fund_id = self.Inputtext.text()
        if self.fund_id == '':
            self.plaintxt = 'ERROR:请输入股票代码!'
            res=False
        else:
            res=True
        return res

    def click_search_fund(self ):  # 查询基金
        if self.check_fundid():
            self.plaintxt='基金:'+str(self.fund_id)+'\n'
            self.plaintxt+=str(ts.get_fund_info(self.fund_id))
        self.plainTextEdit.setPlainText(self.plaintxt)
    def xingu_click(self ):  # 新股查看
        self.plaintxt='新股列表:\n'
        self.plaintxt+=str(ts.new_stocks().head(50).rename(columns=self.cm_xingu))
        self.plainTextEdit.setPlainText(self.plaintxt)

    def longhu_click(self ):  # 龙虎榜查看
        front_date = self.dateEdit.date().toString(Qt.ISODate)[:10]
        if front_date != '':
            self.deal_date = front_date

        self.plaintxt='龙虎榜:\n'
        self.plaintxt+=str(ts.top_list(self.deal_date).head(50).rename(columns=self.cm_longhu))
        self.plainTextEdit.setPlainText(self.plaintxt)

    def longhu_cnt_click(self ):  # 上榜统计
        date_int=5
        self.plaintxt='近{}日上榜:\n'.format(date_int)
        self.plaintxt+=str(ts.cap_tops(days= date_int).head(50).rename(columns=self.cm_longhu_cnt))
        self.plainTextEdit.setPlainText(self.plaintxt)

    def click_search(self ):  # 查询股票
        if self.check_fundid():
            self.plaintxt='股票:'+str(self.fund_id)+'\n'
            self.plaintxt+=str(self.df_stock[self.df_stock.index == self.fund_id].rename(columns=self.cm_basic))
        self.plainTextEdit.setPlainText(self.plaintxt)

    def dapan_click(self ):  # 大盘指数
        try:
            plaintxt='大盘指数:'+'\n'
            plaintxt+=str(ts.get_index().rename(columns=self.cm_dapan))
            self.plainTextEdit.setPlainText(plaintxt)
        except Exception as e:
            self.plainTextEdit.setPlainText(e)

    def real_click(self ):  # 实时分笔
        if self.check_fundid():
            self.plaintxt='当前股票:'+self.fund_id+'\n'
            self.plaintxt+='实时交易:'+'\n'
            self.plaintxt+=str(ts.get_realtime_quotes(self.fund_id).rename(columns=self.cm_realtime))
        self.plainTextEdit.setPlainText(self.plaintxt)

    def today_click(self):  # 今日分笔
        if self.check_fundid():
            self.plaintxt = '当前股票:' + self.fund_id + '\n'
            self.plaintxt += '今日交易:' + '\n'
            self.plaintxt += str( ts.get_today_ticks(int(self.fund_id) ).head(5).rename(columns=self.cm_today))
        self.plainTextEdit.setPlainText(self.plaintxt)

    def history_click(self):  # 历史分笔
        try:
            if self.check_fundid():
                front_date = self.dateEdit.date().toString(Qt.ISODate)[:10]
                if front_date != '':
                    self.deal_date = front_date

                self.plaintxt = '当前股票:' + self.fund_id + '\n'
                self.plaintxt += '交易日期:' + self.deal_date + '\n'
                self.plaintxt += '历史交易:' + '\n'
                self.plaintxt += str( ts.get_tick_data(self.fund_id,date=self.deal_date,src='tt').head(50).rename(columns=self.cm_today))
            self.plainTextEdit.setPlainText(self.plaintxt)
            print(self.plaintxt)
        except Exception as e:
            self.plainTextEdit.setPlainText(self.e)

    def big_click(self):  # 大单交易
        try:
            if self.check_fundid():
                if self.dateEdit.text()!='':
                    self.deal_date=self.dateEdit.text()
                if self.dadan_vol_lineEdit.text()!='':
                    self.vol=self.dadan_vol_lineEdit.text()
                self.plaintxt = '当前股票:' + self.fund_id + '\n'
                self.plaintxt += '大单交易:' + '\n'
                self.plaintxt += str( ts.get_sina_dd(self.fund_id,date=self.deal_date,vol=int(self.vol)).rename(columns=self.cm_dadan))
            self.plainTextEdit.setPlainText(self.plaintxt)
        except Exception as e:
            self.plainTextEdit.setPlainText(self.e)

    def select_combobox(self):  # 下拉框
        # fund_id = self.Inputtext.text()
        current_txt = self.comboBox.currentText()
        if current_txt=='查看信息':
            self.click_search()
        elif  current_txt=='实时分笔':
            self.real_click()
        elif  current_txt=='今日分笔':
            self.today_click()
        else:
            self.plainTextEdit.setPlainText('请选择一项!')

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ui = mywindow()
    ui.show()
    sys.exit(app.exec_())

fund_search.py

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(966, 546)
        self.plainTextEdit = QtWidgets.QPlainTextEdit(Form)
        self.plainTextEdit.setEnabled(True)
        self.plainTextEdit.setGeometry(QtCore.QRect(30, 190, 911, 341))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.plainTextEdit.setFont(font)
        self.plainTextEdit.setReadOnly(True)
        self.plainTextEdit.setObjectName("plainTextEdit")
        self.comboBox = QtWidgets.QComboBox(Form)
        self.comboBox.setGeometry(QtCore.QRect(280, 10, 111, 30))
        font = QtGui.QFont()
        font.setPointSize(15)
        self.comboBox.setFont(font)
        self.comboBox.setObjectName("comboBox")
        self.dapanButton = QtWidgets.QPushButton(Form)
        self.dapanButton.setGeometry(QtCore.QRect(420, 10, 128, 32))
        self.dapanButton.setObjectName("dapanButton")
        self.dateEdit = QtWidgets.QDateEdit(Form)
        self.dateEdit.setGeometry(QtCore.QRect(140, 100, 121, 31))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.dateEdit.setFont(font)
        self.dateEdit.setDate(QtCore.QDate(2020, 4, 20))
        self.dateEdit.setObjectName("dateEdit")
        self.history_Button = QtWidgets.QPushButton(Form)
        self.history_Button.setGeometry(QtCore.QRect(280, 100, 111, 31))
        self.history_Button.setObjectName("history_Button")
        self.dadan_vol_lineEdit = QtWidgets.QLineEdit(Form)
        self.dadan_vol_lineEdit.setGeometry(QtCore.QRect(140, 50, 101, 31))
        self.dadan_vol_lineEdit.setText("")
        self.dadan_vol_lineEdit.setObjectName("dadan_vol_lineEdit")
        self.dadan_Button = QtWidgets.QPushButton(Form)
        self.dadan_Button.setGeometry(QtCore.QRect(280, 50, 111, 31))
        font = QtGui.QFont()
        font.setPointSize(18)
        self.dadan_Button.setFont(font)
        self.dadan_Button.setObjectName("dadan_Button")
        self.label_2 = QtWidgets.QLabel(Form)
        self.label_2.setGeometry(QtCore.QRect(21, 56, 120, 24))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(Form)
        self.label_3.setGeometry(QtCore.QRect(21, 101, 120, 24))
        self.label_3.setObjectName("label_3")
        self.xinguButton = QtWidgets.QPushButton(Form)
        self.xinguButton.setGeometry(QtCore.QRect(420, 50, 128, 32))
        self.xinguButton.setObjectName("xinguButton")
        self.longhu_Button = QtWidgets.QPushButton(Form)
        self.longhu_Button.setGeometry(QtCore.QRect(420, 100, 128, 32))
        self.longhu_Button.setObjectName("longhu_Button")
        self.longhu_cnt_Button = QtWidgets.QPushButton(Form)
        self.longhu_cnt_Button.setGeometry(QtCore.QRect(279, 140, 271, 32))
        self.longhu_cnt_Button.setObjectName("longhu_cnt_Button")
        self.Inputtext = QtWidgets.QLineEdit(Form)
        self.Inputtext.setGeometry(QtCore.QRect(140, 10, 101, 31))
        self.Inputtext.setText("")
        self.Inputtext.setClearButtonEnabled(True)
        self.Inputtext.setObjectName("Inputtext")
        self.label = QtWidgets.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(21, 11, 96, 24))
        self.label.setObjectName("label")

        self.retranslateUi(Form)
        self.comboBox.activated['QString'].connect(Form.select_combobox)
        self.dadan_Button.clicked.connect(Form.big_click)
        self.history_Button.clicked.connect(Form.history_click)
        self.dapanButton.clicked.connect(Form.dapan_click)
        self.xinguButton.clicked.connect(Form.xingu_click)
        self.longhu_Button.clicked.connect(Form.longhu_click)
        self.longhu_cnt_Button.clicked.connect(Form.longhu_cnt_click)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.dapanButton.setText(_translate("Form", "大盘指数"))
        self.dateEdit.setDisplayFormat(_translate("Form", "yyyy/MM/dd"))
        self.history_Button.setText(_translate("Form", "历史分笔"))
        self.dadan_Button.setText(_translate("Form", "大单交易"))
        self.label_2.setText(_translate("Form", "最低笔数:"))
        self.label_3.setText(_translate("Form", "交易日期:"))
        self.xinguButton.setText(_translate("Form", "新股"))
        self.longhu_Button.setText(_translate("Form", "龙虎榜"))
        self.longhu_cnt_Button.setText(_translate("Form", "近五日上榜统计"))
        self.label.setText(_translate("Form", "股票ID:"))

猜你喜欢

转载自blog.csdn.net/wsdc0521/article/details/105864817