表类数据建议QT中的tableview ,效率更高,以下是实现方法的代码
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import akshare as ak
import time
class PdTable(QAbstractTableModel):
def __init__(self, data):
QAbstractTableModel.__init__(self)
self._data = data
def rowCount(self, parent=None):
return self._data.shape[0]
def columnCount(self, parent=None):
return self._data.shape[1]
# 显示数据
def data(self, index, role=Qt.DisplayRole):
if index.isValid():
if role == Qt.DisplayRole:
return str(self._data.iloc[index.row(), index.column()])
return None
# 显示行和列头
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self._data.columns[col]
elif orientation == Qt.Vertical and role == Qt.DisplayRole:
return self._data.axes[0][col]
return None
class Stock_Index():
def Sh_Iindex(self):
'''返回上海证券实时涨跌幅数据,为防IP被封,每1分钟更新一次'''
#while True:
df = ak.stock_zh_index_spot()
#df=df[df['代码']=='sh000001']
#Value=df[df['代码']=='sh000001']['涨跌幅'].values[0]
return df # Value
class mywindow(QWidget):
def __init__(self):
super().__init__()
self.resize(500,400)
self.btn_close = QPushButton('close',self)#
self.txb=QTextBrowser(self)#
self.tableView = QTableView()
self.tableView.setGeometry( 0,50,500,350)
si=Stock_Index()
df=si.Sh_Iindex()
model=PdTable(df)
self.tableView.setModel(model)
dlgLayout = QVBoxLayout()
dlgLayout.addWidget(self.tableView)
self.setLayout(dlgLayout)
app=QApplication([])
main=mywindow()
main.show()
app.exec()
参考链接:
https://blog.csdn.net/islinyoubiao/article/details/105577081?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522165322394216781483798440%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=165322394216781483798440&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v1~rank_v31_ecpm-4-105577081-null-null.142v10control,157v4control&utm_term=pyqt+dataframe&spm=1018.2226.3001.4187