使用PyQt绘制精美的QTabWidget页签式股票代码一览表

使用PyQt绘制精美的QTabWidget页签式股票代码一览表_第1张图片

# -*- coding: utf-8 -*-

import os
import sys
from PyQt4.QtCore import Qt
from pyqtgraph.Qt import QtCore, QtGui

import tushare as ts
import pandas as pd
from PyQt4.QtGui import QApplication

reload(sys)
sys.setdefaultencoding('utf8')

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class StockTableWidget():
    def __init__(self, *arg):
        pass

    def createStockTableWidget(self):

        self.tabWidget = QtGui.QTabWidget()
        sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(1)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.tabWidget.sizePolicy().hasHeightForWidth())
        self.tabWidget.setSizePolicy(sizePolicy)
        self.tabWidget.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
        self.tabWidget.setMouseTracking(False)
        self.tabWidget.setAutoFillBackground(False)
        self.tabWidget.setObjectName(_fromUtf8("tabWidget"))
        self.tabWidget.setTabPosition(QtGui.QTabWidget.West)
        self.tab = QtGui.QWidget()
        self.tab.setObjectName(_fromUtf8("tab"))
        self.verticalLayout_2 = QtGui.QVBoxLayout(self.tab)
        self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
        self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.gridLayout = QtGui.QGridLayout()
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.verticalLayout_2.addLayout(self.gridLayout)
        self.tabWidget.addTab(self.tab, _fromUtf8(""))
        self.tab_2 = QtGui.QWidget()
        self.tab_2.setObjectName(_fromUtf8("tab_2"))
        self.tabWidget.addTab(self.tab_2, _fromUtf8(""))
        self.verticalLayout_3 = QtGui.QVBoxLayout(self.tab_2)
        self.verticalLayout_3.setObjectName(_fromUtf8("verticalLayout_3"))
        self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
        self.tab_3 = QtGui.QWidget()
        self.tab_3.setObjectName(_fromUtf8("tab_3"))
        self.tabWidget.addTab(self.tab_3, _fromUtf8(""))
        self.verticalLayout_4 = QtGui.QVBoxLayout(self.tab_3)
        self.verticalLayout_4.setObjectName(_fromUtf8("verticalLayout_4"))
        self.verticalLayout_4.setContentsMargins(0, 0, 0, 0)
        self.tab_4 = QtGui.QWidget()
        self.tab_4.setObjectName(_fromUtf8("tab_4"))
        self.tabWidget.addTab(self.tab_4, _fromUtf8(""))
        self.verticalLayout_5 = QtGui.QVBoxLayout(self.tab_4)
        self.verticalLayout_5.setObjectName(_fromUtf8("verticalLayout_5"))
        self.verticalLayout_5.setContentsMargins(0, 0, 0, 0)
        self.tab_5 = QtGui.QWidget()
        self.tab_5.setObjectName(_fromUtf8("tab_5"))
        self.tabWidget.addTab(self.tab_5, _fromUtf8(""))
        self.verticalLayout_6 = QtGui.QVBoxLayout(self.tab_5)
        self.verticalLayout_6.setObjectName(_fromUtf8("verticalLayout_6"))
        self.verticalLayout_6.setContentsMargins(0, 0, 0, 0)
        self.tab_6 = QtGui.QWidget()
        self.tab_6.setObjectName(_fromUtf8("tab_6"))
        self.tabWidget.addTab(self.tab_6, _fromUtf8(""))
        self.verticalLayout_7 = QtGui.QVBoxLayout(self.tab_6)
        self.verticalLayout_7.setObjectName(_fromUtf8("verticalLayout_7"))
        self.verticalLayout_7.setContentsMargins(0, 0, 0, 0)
        self.tab_7 = QtGui.QWidget()
        self.tab_7.setObjectName(_fromUtf8("tab_7"))
        self.tabWidget.addTab(self.tab_7, _fromUtf8(""))
        self.verticalLayout_8 = QtGui.QVBoxLayout(self.tab_7)
        self.verticalLayout_8.setObjectName(_fromUtf8("verticalLayout_8"))
        self.verticalLayout_8.setContentsMargins(0, 0, 0, 0)
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "沪深", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "港股", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_3), _translate("MainWindow", "美股", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_4), _translate("MainWindow", "期货", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_5), _translate("MainWindow", "基金", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_6), _translate("MainWindow", "全球", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_7), _translate("MainWindow", "自选", None))

        self.treeWidget = QtGui.QTreeWidget()
        self.treeWidget.setObjectName(_fromUtf8("treeWidget"))
        self.treeWidget.setHeaderItem(QtGui.QTreeWidgetItem(
            [u'代码', u'名称', u'所属行业', u'地区', u'市盈率', u'流通股本(亿)', u'总股本(亿)', u'总资产(万)', u'流动资产', u'固定资产', u'公积金', u'每股公积金',
             u'每股收益', u'每股净资', u'市净率', u'上市日期', u'未分利润', u'每股未分配', u'收入同比(%)', u'利润同比(%)', u'毛利率(%)', u'净利润率(%)',
             u'股东人数']))
        self.treeWidget.header().setClickable(True)
        self.treeWidget.setSortingEnabled(True)
        self.treeWidget.sortItems(0, Qt.AscendingOrder)
        self.generateParameterTreeNode()
        self.verticalLayout_2.addWidget(self.treeWidget)
        return self.tabWidget

    def generateParameterTreeNode(self):
        self.treeWidget.setColumnCount(23)

        if os.path.exists('index.csv'):
            self.index = pd.read_csv('index.csv', dtype={'code': str})
        else:
            self.index = ts.get_index()
            self.index.to_csv('index.csv')
            self.index = pd.read_csv('index.csv', dtype={'code': str})

        if os.path.exists('stock_basics.csv'):
            self.stock_basics = pd.read_csv('stock_basics.csv', dtype={'code': str})
        else:
            self.stock_basics = ts.get_stock_basics()
            self.stock_basics.to_csv('stock_basics.csv')
            self.stock_basics = pd.read_csv('stock_basics.csv', dtype={'code': str})
        for indexs in self.stock_basics.index:
            try:
                code, name, industry, area, pe, outstanding, totals, totalAssets, liquidAssets, fixedAssets, reserved, reservedPerShare, esp, bvps, pb, timeToMarket, undp, perundp, rev, profit, gpr, npr, holders = self.stock_basics.loc[indexs].values[0:23]
                '''code,name,industry,area,pe,outstanding,totals,totalAssets,liquidAssets,fixedAssets,reserved,reservedPerShare,esp,bvps,pb,timeToMarket,undp,perundp, rev,profit,gpr,npr,holders'''
                self.insertTreeWidget(0, [code, name.decode('utf8'), industry.decode('utf8'), area.decode('utf8'), str(pe), str(outstanding), str(totals), str(totalAssets), str(liquidAssets), str(fixedAssets), str(reserved), str(reservedPerShare), str(esp), str(bvps), str(pb), str(timeToMarket), str(undp), str(perundp), str(rev), str(profit), str(gpr), str(npr), str(holders)], topLevel=None)
            except Exception:
                print Exception
        return

    def insertTreeWidget(self, level, datas, topLevel):
        if level == 0:
            topLevelItem = QtGui.QTreeWidgetItem(datas)
            self.treeWidget.addTopLevelItem(topLevelItem)
            return topLevelItem

if __name__ == '__main__':
    app = QApplication(sys.argv)
    stockTable = StockTableWidget().createStockTableWidget()
    stockTable.setWindowTitle(u'沪深证券代码一览表')
    stockTable.setStyleSheet('''QWidget{font-size: 12px;}''')
    stockTable.show()
    sys.exit(app.exec_())
    app.exec_()

你可能感兴趣的:(Python,股票行情,GUI,PyQt)