# -*- coding: utf-8 -*-
"""
Created on Thu Sep 17 14:14:15 2020
@author: 19749
"""
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import nb_dl_0, nb_dl_02, nb_dl_03
import pandas as pd
df = pd.DataFrame({'': [],
'基金简称': [],
'基金代码': [],
'类别': []})
class pandasModel(QAbstractTableModel):
def __init__(self, data):
QAbstractTableModel.__init__(self)
self._data = data
def rowCount(self, parent=None):
return self._data.shape[0]
def columnCount(self, parnet=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]
return None
class data:
def data11(self):
a = pd.read_csv('指数股票型.csv', encoding = "gb2312").T.reset_index(drop=False)
a.insert(2, '类别', '指数股票型')
a.columns = ['基金代码', '基金简称', '类别']
return a
def data12(self):
a = pd.read_csv('主动股票开放型.csv', encoding = "gb2312").T.reset_index(drop=False)
a.insert(2, '类别', '主动股票开放型')
a.columns = ['基金代码', '基金简称', '类别']
return a
def data21(self):
a = pd.read_csv('主动混合封闭型.csv', encoding = "gb2312").T.reset_index(drop=False)
a.insert(2, '类别', '主动混合封闭型')
a.columns = ['基金代码', '基金简称', '类别']
return a
def data22(self):
a = pd.read_csv('主动混合开放型.csv', encoding = "gb2312").T.reset_index(drop=False)
a.insert(2, '类别', '主动混合开放型')
a.columns = ['基金代码', '基金简称', '类别']
return a
def data31(self):
a = pd.read_csv('指数债券型.csv', encoding = "gb2312").T.reset_index(drop=False)
a.insert(2, '类别', '指数债券型')
a.columns = ['基金代码', '基金简称', '类别']
return a
def data32(self):
a = pd.read_csv('主动债券封闭型.csv', encoding = "gb2312").T.reset_index(drop=False)
a.insert(2, '类别', '主动债券封闭型')
a.columns = ['基金代码', '基金简称', '类别']
return a
def data33(self):
a = pd.read_csv('主动债券开放型.csv', encoding = "gb18030").T.reset_index(drop=False)
a.insert(2, '类别', '主动债券开放型')
a.columns = ['基金代码', '基金简称', '类别']
return a
def data_all(self):
a = pd.DataFrame({'基金代码': [], '基金简称': [], '类别': []})
for i in range(1,4):
for j in range(1,4):
try:
exec('global b\nb = self.data' + str(i) + str(j) + '()')
a = pd.concat([a, b])
except:
pass
return a
#指标树
l_f_1 = ['单位净值', '收益率', '最大回撤']
l_f_2 = ['换手率', '持仓行业']
d_f = {'集1': l_f_1, '集2': l_f_2}
#列标签
l1 = ['', '基金简称', '基金代码', '类别']
l2 = ['', '基金简称', '基金代码', '类别']
l3 = ['', '', '', '']
class window2(QWidget):
def __init__(self):
super(window2,self).__init__()
self.setWindowTitle("同业分析")
self.resize(1700, 900)
self.vlt0 = QVBoxLayout(self)
self.top()
self.sub_top()
self.mainWindow()
self.set_contextMenu()
def top(self):
self.hlt0 = QHBoxLayout()
self.vlt0.addLayout(self.hlt0)
self.btn01 = QPushButton('宏观经济指标')
self.btn01.setStyleSheet("background-color: rgb(222,211,140)")
self.btn01.setMaximumWidth(140)
self.btn02 = QPushButton('同业分析')
self.btn02.setStyleSheet("background-color: rgb(137,190,178)")
self.btn02.setMaximumWidth(180)
self.btn03 = QPushButton('第三页面')
self.btn03.setStyleSheet("background-color: rgb(222,211,140)")
self.btn03.setMaximumWidth(100)
self.hlt0.addWidget(self.btn01)
self.hlt0.addWidget(self.btn02)
self.hlt0.addWidget(self.btn03)
self.hlt0.addStretch()
def sub_top(self):
self.hlt1 = QHBoxLayout()
self.vlt0.addLayout(self.hlt1)
self.btn11 = QPushButton('获取数据')
self.btn11.clicked.connect(self.get_data)
self.btn12 = QPushButton('隐藏指标参数')
self.btn12.clicked.connect(self.para_hide)
self.btn13 = QPushButton('修改指标参数')
self.btn13.clicked.connect(self.para_mod)
self.hlt1.addWidget(self.btn11)
self.hlt1.addWidget(self.btn12)
self.hlt1.addWidget(self.btn13)
self.hlt1.addStretch()
def mainWindow(self):
self.hlt_main = QHBoxLayout()
self.vlt0.addLayout(self.hlt_main)
self.leftBar()
self.tableView()
def leftBar(self):
self.spl_left = QSplitter(Qt.Vertical)
self.spl_left.setMaximumWidth(370)
self.spl_left.setMinimumWidth(300)
self.hlt_main.addWidget(self.spl_left)
self.featureBox()
self.fundBox()
def featureBox(self):
self.tree1 = QTreeWidget(self.spl_left)
self.tree1.setHeaderLabel('指标列表')
for i in d_f.keys():
root = QTreeWidgetItem(self.tree1)
root.setText(0, i)
for j in d_f[i]:
child = QTreeWidgetItem(root)
child.setText(0, j)
btn = QPushButton(j)
btn.setObjectName(j)
self.tree1.setItemWidget(child, 0, btn)
btn.clicked.connect(self.f)
self.tree1.expandAll()
def fundBox(self):
self.tree2 = QTreeWidget(self.spl_left)
self.tree2.setHeaderLabel('已选基金')
self.btn_fundBox = QPushButton('展开选择栏')
self.btn_fundBox.setMaximumHeight(40)
self.spl_left.addWidget(self.btn_fundBox)
def tableView(self):
self.model = pandasModel(df)
self.view = QTableView()
self.view.setModel(self.model)
self.view.setSelectionMode(QTableView.ExtendedSelection)
self.hlt_main.addWidget(self.view)
def set_contextMenu(self):
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.show_menu)
def show_menu(self, point):
menu = QMenu(self)
del_col = QAction("删除所选列", menu)
del_col.triggered.connect(self.del_col)
del_row = QAction("删除所选行", menu)
del_row.triggered.connect(self.del_row)
menu.addAction(del_col)
menu.addAction(del_row)
dest_point = self.mapToGlobal(point)
menu.exec_(dest_point)
def del_col(self):
global df, l1, l2, l3
index= self.view.selectionModel().selectedIndexes()
list1=[]
for i in index:
list1.append(i.column())
list1.sort(key=int, reverse=True)
print('list1: ', list1)
for i in list1:
if i not in [0,1,2,3]:
df = df.drop(columns = list(df)[i], axis = 1)
del l1[i], l2[i], l3[i]
df.reset_index(drop=True, inplace=True)
print(df)
self.model = pandasModel(df)
self.view.setModel(self.model)
self.view.updateEditorData()
print(l1,l2,l3)
def del_row(self):
global df
index= self.view.selectionModel().selectedIndexes()
list1=[]
for i in index:
list1.append(i.row())
list1.sort(key=int, reverse=True)
print('list1: ', list1)
for i in list1:
df = df.drop(index = i)
df[''] = range(0,len(df))
df.reset_index(drop=True, inplace=True)
print(df)
self.model = pandasModel(df)
self.view.setModel(self.model)
self.view.updateEditorData()
def f(self):
text = self.sender().objectName()
global dl
if text == '单位净值':
dl = nb_dl_02.dl_1(text)
elif text == '收益率' or '最大回撤':
dl = nb_dl_02.dl_2(text)
dl.show()
try:
dl.exec_()
except:
pass
dl.btn.clicked.connect(self.f_add)
def f_add(self):
global dl, l1, l2, l3
l1.append(dl.t1)
l2.append(dl.t2)
l3.append(dl.t3)
print(l1,l2,l3)
self.set_col()
def set_col(self):
global l1, l2, df
flag = False
if self.btn12.text() == '显示指标参数':
df.columns = l2[:df.shape[1]]
flag = True
for i in l2:
if i not in df.columns.tolist():
df[i] = None
for i in df.columns.tolist():
if i not in l2:
df = df.drop(columns = i, axis = 1)
#print('l2:', l2, '\ncolumn:', df.columns.tolist(), '\nl1:', l1)
if flag:
df.columns = l1
self.model = pandasModel(df)
self.view.setModel(self.model)
self.view.updateEditorData()
def get_data(self):
global l3
dict_temp = {}
'''for l in l2[4:]:
l0 = l.split('\n')
dict_temp[l] = l0'''
print(dict_temp)
def para_hide(self):
global l1, l2, df
if self.btn12.text() == '隐藏指标参数':
df.columns = l1
self.btn12.setText('显示指标参数')
self.model = pandasModel(df)
self.view.setModel(self.model)
self.view.updateEditorData()
else:
df.columns = l2
self.btn12.setText('隐藏指标参数')
self.model = pandasModel(df)
self.view.setModel(self.model)
self.view.updateEditorData()
def para_mod(self):
global l1, l2, l3, dl_para
dl_para = nb_dl_03.window(l1, l2, l3)
dl_para.show()
dl_para.btn3.clicked.connect(self.para_change)
dl_para.exec_()
def para_change(self):
global l1, l2, l3, dl_para
l1 = dl_para.l1
l2 = dl_para.l2
l3 = dl_para.l3
print(l1, l2, l3)
self.set_col()
def p(self):
global df
df0 = nb_dl_0.df0
df = pd.concat([df, df0])
df[''] = range(0,len(df))
df.reset_index(drop=True, inplace=True)
print(df)
self.model = pandasModel(df)
self.view.setModel(self.model)
self.view.updateEditorData()
self.plot_tree2()
def plot_tree2(self):
dict_sel = nb_dl_0.dict_sel
self.tree2.clear()
#类别基金
for i in dict_sel.keys():
self.root1 = QTreeWidgetItem(self.tree2)
self.root1.setText(0, i)
for j in dict_sel[i]:
self.child1 = QTreeWidgetItem(self.root1)
self.child1.setText(0, j)
#自定义类别
'''self.root2 = QTreeWidgetItem(self.tree2)
self.root2.setText(0, '自定义基金类别')
for i in nb_dl_0.list_add3:
self.child2 = QTreeWidgetItem(self.root2)
self.child2.setText(0, i)'''
self.root3 = QTreeWidgetItem(self.tree2)
self.root3.setText(0, '单只基金')
for i in nb_dl_0.list_add2:
self.child3 = QTreeWidgetItem(self.root3)
self.child3.setText(0, i)
self.tree2.expandAll()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w2 = window2()
w2.show()
dl_f = nb_dl_0.dl_fund()
w2.btn_fundBox.clicked.connect(dl_f.show)
dl_f.btn2.clicked.connect(w2.p)
sys.exit(app.exec_())
nb_0
最新推荐文章于 2024-03-01 02:03:51 发布