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:"))