PyQt5-UI界面控件布局实战-界面表格布局(四)

界面效果
PyQt5-UI界面控件布局实战-界面表格布局(四)

import sys
import threading
import win32gui
import win32print

import win32con
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QWidget, QApplication, QHBoxLayout, QFrame, QAbstractItemView, QTableWidget, QTabWidget

from Main.Views.Easytrade.easytrademanage.qss import Qss


class RecordMsg(QWidget):
    tlock = threading.RLock()

    def __init__(self):
        super(RecordMsg, self).__init__()
        # 建立ws
        self.Recvmsg = "RecordMsg"
        self.setWindowTitle("记录")
        self.holding_table_data = []
        self.entrust_table_data = []
        self.deal_table_data = []
        self.startTreadingFlag = 1
        self.accountmsgComboFutures = None
        # self.brokerid = None
        self.initUi()

    def initUi(self):
        self.setStyleSheet("QWidget{background:#f0f0f0;}")
        # 获取屏幕的缩放比例
        hDC = win32gui.GetDC(0)
        self.dpi = win32print.GetDeviceCaps(hDC, win32con.LOGPIXELSX)
        dpi = win32print.GetDeviceCaps(hDC, win32con.LOGPIXELSX)
        # 获取显示器分辨率大小
        self.desktop = QApplication.desktop()
        self.screenRect = self.desktop.screenGeometry()
        self.height = self.screenRect.height()
        self.width = self.screenRect.width()
        self.font5 = QFont("微软雅黑")
        pointsize5 = self.font5.pointSize()
        self.font5.setPixelSize(pointsize5 * 90 / 72)
        self.abitragerecore = QTabWidget()
        self.abitrageposition = QWidget()
        self.abitrageentrust = QWidget()
        self.accountposition = QWidget()
        self.correlationentrust = QWidget()
        self.abitragerecore.addTab(self.abitrageposition, "持仓")
        self.abitragerecore.addTab(self.abitrageentrust, "委托")
        self.abitragerecore.addTab(self.accountposition, "成交")
        # self.abitragerecore.addTab(self.correlationentrust, "条件单")
        self.abitragerecore.setStyleSheet(Qss.abitragerecoreStyle)

        holdingColumnName = ['品种', '合约', '多空', '总仓', '可用', '今仓', '今可用', '开仓均价', '逐笔浮盈', '保证金', '持仓均价', '盯市浮盈', '投保',
                             '']
        self.holding_column_list = ['instrumentid', 'instrumentid', 'direct', 'sum_holding', 'sum_useful',
                                    'now_holding', 'now_useful',
                                    'opencost', 'closeprofitbytrade', 'usemargin', 'positioncost',
                                    'closeprofitbydate', 'hedge']
        holdingDictWidthList = {144: [88, 109, 70, 96, 96, 96, 96, 96, 96, 96, 96, 96, 96],
                                120: [88, 104, 60, 94, 94, 94, 94, 99, 86, 94, 94, 94, 95],
                                96: [87, 89, 55, 89, 89, 89, 89, 94, 96, 89, 89, 89, 89]}
        # 生成持仓表格
        self.holding_table = TableWidget(holdingColumnName, holdingDictWidthList)

        entrustColumnName = ['时间', '合约', '状态', '买卖', '开平', '委托价', '委托量', '可撤', '已成交', '已撤单', '投保', '报单编号', '']
        self.entrust_column_list = ['inserttime', 'instrumentid', 'orderstatus', 'side', 'direct',
                                    'price', 'amount', 'can_return', 'bizvolume', 'had_return',
                                    'hedge', 'orderid']
        entrustDictWidthList = {144: [153, 93, 93, 70, 70, 96, 96, 96, 96, 96, 96, 136],
                                120: [160, 93, 93, 60, 60, 96, 96, 94, 93, 93, 93, 123],
                                96: [150, 84, 80, 60, 60, 89, 89, 90, 94, 94, 94, 136]}
        # 生成委托表格
        self.entrust_table = TableWidget(entrustColumnName, entrustDictWidthList)

        dealColumnName = ['时间', '合约', '买卖', '开平', '成交价', '成交量', '报单编号', '']
        self.deal_column_list = ['tradetime', 'instrumentid', 'side', 'direct', 'price', 'volume', 'orderid']
        dealDictWidthList = {144: [153, 96, 70, 70, 96, 140, 140, 100],
                             120: [160, 96, 60, 60, 96, 140, 140, 100],
                             96: [150, 120, 120, 120, 160, 120, 160, 150]}
        # 生成成交表格
        self.deal_table = TableWidget(dealColumnName, dealDictWidthList)

        self.frame = QFrame()
        self.frame.setObjectName('frame')
        self.frame.setStyleSheet(Qss.record_frame_style)

        hbox1 = QHBoxLayout(self.abitrageposition)
        hbox1.addWidget(self.holding_table)
        hbox1.setContentsMargins(0, 0, 0, 0)

        hbox2 = QHBoxLayout(self.abitrageentrust)
        hbox2.addWidget(self.entrust_table)
        hbox2.setContentsMargins(0, 0, 0, 0)

        hbox3 = QHBoxLayout(self.accountposition)
        hbox3.addWidget(self.deal_table)
        hbox3.setContentsMargins(0, 0, 0, 0)

        hbox = QHBoxLayout()
        hbox.addWidget(self.abitragerecore)
        hbox.setContentsMargins(0, 0, 0, 0)
        self.frame.setLayout(hbox)

        hbox4 = QHBoxLayout()
        hbox4.addWidget(self.frame)
        hbox4.setContentsMargins(0, 0, 0, 0)

        self.setLayout(hbox4)
        self.setContentsMargins(2, 2, 0, 0)

        # self.abitragerecore.currentChanged.connect(self.getTableData)


class TableWidget(QTableWidget):
    def __init__(self, ColumnName=None, dictWidthList=None):
        super(TableWidget, self).__init__()
        self.ColumnName = ColumnName
        self.dictWidthList = dictWidthList
        self.initUi()

    def initUi(self):
        self.font5 = QFont("微软雅黑")
        pointsize5 = self.font5.pointSize()
        self.font5.setPixelSize(pointsize5 * 90 / 72)
        self.setAlternatingRowColors(True)  # 隔行换色
        self.setStyleSheet(Qss.table_style)  # 设置样式
        self.horizontalHeader().setStyleSheet(Qss.table_header_style)  # 设置表头背景色和字体颜色
        # self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.verticalScrollBar().setStyleSheet(Qss.table_vertical_style)
        self.horizontalScrollBar().setStyleSheet(Qss.table_horizontal_style)
        self.setFrameShape(QFrame.NoFrame)  # 无边框
        self.setShowGrid(1)  # 无网格
        self.setFont(self.font5)
        self.verticalHeader().setVisible(0)  # 隐藏表头
        # deal_table.horizontalHeader().setVisible(0)  # 隐藏表头
        self.setEditTriggers(QAbstractItemView.NoEditTriggers)  # 禁止对表格编辑
        # self.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.setSelectionBehavior(QAbstractItemView.SelectRows)  # 单行选择
        self.horizontalHeader().setDefaultAlignment(Qt.AlignRight | Qt.AlignVCenter)
        self.horizontalHeader().setMinimumHeight(35)
        self.setFocusPolicy(Qt.NoFocus)  # 去除选中虚线
        self.setSelectionMode(QAbstractItemView.SingleSelection)  # 单选
        self.horizontalHeader().setSectionsClickable(False)  # 水平方向表头不可点击
        self.horizontalHeader().setStretchLastSection(True)
        # table.setShowGrid(0)  # 去表格线

        column1 = self.setColumnCount(len(self.ColumnName))  # 设置列数
        self.setHorizontalHeaderLabels(self.ColumnName)  # 设置行名称
        # 滚动条以像素滚动
        self.setVerticalScrollMode(QAbstractItemView.ScrollPerPixel)
        self.setHorizontalScrollMode(QAbstractItemView.ScrollPerPixel)
        # 获取屏幕的缩放比例
        hDC = win32gui.GetDC(0)
        dpi = win32print.GetDeviceCaps(hDC, win32con.LOGPIXELSX)
        self.desktop = QApplication.desktop()
        self.screenRect = self.desktop.screenGeometry()
        self.height = self.screenRect.height()
        self.width = self.screenRect.width()

        # 设置列宽
        if dpi >= 144:
            widthList = self.dictWidthList[144]
            for i in range(len(widthList)):
                self.setColumnWidth(i, widthList[i])
        elif dpi >= 120:
            widthList = self.dictWidthList[120]
            for i in range(len(widthList)):
                self.setColumnWidth(i, widthList[i])
        elif dpi == 96:
            widthList = self.dictWidthList[96]
            for i in range(len(widthList)):
                self.setColumnWidth(i, widthList[i])


if __name__ == '__main__':
    # 创建应用程序和对象
    app = QApplication(sys.argv)
    ex = RecordMsg()
    ex.show()
    sys.exit(app.exec_())
上一篇:94-datetmie模块


下一篇:AC_94. 递归实现排列型枚举