自己写一个类似vnpy的界面框架

概述

通过之前3次对vnpy的界面代码的研究,我们去模仿做一个vn.py的大框架。巩固一下PyQt5的学习。

这部分的代码相对来说没有难度和深度,基本上就是把PyQt5的组件进行了使用。主要应用了QVBoxLayout布局在交易下单的窗体tradingWidget上使用,其他的窗体都大多数用了QTableWidget

关于QTableWidget的教程我找到了一篇:PyQt5高级界面控件之QTableWidget(四)

在vnpy中值得自己学习的是:

  1. vnpy抽象了所有的QTableWidget的对象,因为交易类的系统用到了大量的列表。所以建立了一个BaseMonitor的对象
  2. 不同的列显示的方式不太一样,有的部分可能是直接显示出来vaule,而有的列表显示的可能是多空,所以定义了Cell对象。
class BaseCell(QtWidgets.QTableWidgetItem):
"""
General cell used in tablewidgets.
""" def __init__(self, content: Any, data: Any):
""""""
super(BaseCell, self).__init__()
self.setTextAlignment(QtCore.Qt.AlignCenter)
self.set_content(content, data) def set_content(self, content: Any, data: Any):
"""
Set text content.
"""
self.setText(str(content))
self._data = data def get_data(self):
"""
Get data object.
"""
return self._data
class EnumCell(BaseCell):
"""
Cell used for showing enum data.
""" def __init__(self, content: str, data: Any):
""""""
super(EnumCell, self).__init__(content, data) def set_content(self, content: Any, data: Any):
"""
Set text using enum.constant.value.
"""
if content:
super(EnumCell, self).set_content(content.value, data)

根据方向不一样,显示不同的颜色

class DirectionCell(EnumCell):
"""
Cell used for showing direction data.
""" def __init__(self, content: str, data: Any):
""""""
super(DirectionCell, self).__init__(content, data) def set_content(self, content: Any, data: Any):
"""
Cell color is set according to direction.
"""
super(DirectionCell, self).set_content(content, data) if content is Direction.SHORT:
self.setForeground(COLOR_SHORT)
else:
self.setForeground(COLOR_LONG)
class TimeCell(BaseCell):
"""
Cell used for showing time string from datetime object.
""" def __init__(self, content: Any, data: Any):
""""""
super(TimeCell, self).__init__(content, data) def set_content(self, content: Any, data: Any):
"""
Time format is 12:12:12.5
"""
if content is None:
return timestamp = content.strftime("%H:%M:%S") millisecond = int(content.microsecond / 1000)
if millisecond:
timestamp = f"{timestamp}.{millisecond}" self.setText(timestamp)
self._data = data

界面部分大同小异。接下来研究下MainEngine部分。看看引擎是如何把所有的界面和数据接口穿插起来的。

vnpy源码阅读学习(4):自己写一个类似vnpy的UI框架的更多相关文章

  1. vnpy源码阅读学习(1):准备工作

    vnpy源码阅读学习 目标 通过阅读vnpy,学习量化交易系统的一些设计思路和理念. 通过阅读vnpy学习python项目开发的一些技巧和范式 通过vnpy的设计,可以用python复现一个小型简单的 ...

  2. vnpy源码阅读学习(5):关于MainEngine的代码阅读

    关于MainEngine的代码阅读 在入口文件中,我们看到了除了窗体界面的产生,还有关于MainEngine和EventEngin部分.今天来学习下MainEngine的代码. 首先在run代码中,我 ...

  3. vnpy源码阅读学习(8):关于app

    关于app 在入口程序中,我们看到了把 gateway,app, 各类的engine都添加到mainEngine中来.不难猜测gateway主要是处理跟外部的行情,接口各方面的代码,通过别人的文章也不 ...

  4. vnpy源码阅读学习(7):串在一起

    串在一起 我们已经分析了UI.MainEngine.EventEngine.然后他们几个是如何发挥作用的呢?我总结了一张图: 我们来具体的看看UI部分是如何跟EventEngine穿插起来的 \exa ...

  5. vnpy源码阅读学习(3):学习vnpy的界面的实现

    学习vnpy的界面的实现 通过简单的学习了PyQt5的一些代码以后,我们基本上可以理解PyQt的一些用法,下面让我们来先研究下vnpy的UI部分的代码. 首先回到上一节看到的run.py(/vnpy/ ...

  6. vnpy源码阅读学习(2):学习PyQt5

    PyQt5的学习 花费了一个下午把PyQt5大概的学习了下.找了一个教程 PyQt5教程 跟着挨着把上面的案例做了一遍,大概知道PyQt5是如何生成窗体,以及控件的.基本上做到如果有需求要实现,查查手 ...

  7. vnpy源码阅读学习(9)回到OptionMaster

    回到OptionMaster 根据我们对APP调用的代码阅读,我们基本上知道了一个APP是如何被调用,那么我们回到OptionMaster学习下这个APP的实现. 看看结构 class OptionM ...

  8. Spring源码阅读学习一

    昨天抽时间阅读Spring源码,先从spring 4.x的core包开始吧,除了core和util里,首当其冲的就是asm和cglib. 要实现两个类实例之间的字段的复制功能: 多年之前用C#,因为阅 ...

  9. 12.源码分析—如何为SOFARPC写一个序列化?

    SOFARPC源码解析系列: 1. 源码分析---SOFARPC可扩展的机制SPI 2. 源码分析---SOFARPC客户端服务引用 3. 源码分析---SOFARPC客户端服务调用 4. 源码分析- ...

随机推荐

  1. 2018-11-19-win10-uwp-使用-AppCenter-自动构建

    title author date CreateTime categories win10 uwp 使用 AppCenter 自动构建 lindexi 2018-11-19 15:29:34 +080 ...

  2. 2019-11-19-C#-高级面试题

    title author date CreateTime categories C# 高级面试题 lindexi 2019-11-19 08:40:50 +0800 2018-11-12 11:18: ...

  3. 【23.26%】【codeforces 747D】Winter Is Coming

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. SpringSecurity 自定义用户 角色 资源权限控制

    SpringSecurity 自定义用户 角色 资源权限控制 package com.joyen.learning.security; import java.sql.ResultSet; impor ...

  5. Linux 内核设备属性

    sysfs 中的设备入口可有属性. 相关的结构是: struct device_attribute { struct attribute attr; ssize_t (*show)(struct de ...

  6. 在eclipse动态网页项目中,编写web.xml时,servlet标签报错.

    cvc-complex-type.2.4.b: The content of element 'servlet' is not complete. One of '{"http:// jav ...

  7. Python14_中TK模块使用总结

    事件的绑定: https://www.cnblogs.com/jerryspace/p/9836142.html https://www.cnblogs.com/progor/p/8505599.ht ...

  8. Hibernate映射文件详解(News***.hbm.xml)二

    转自 http://blog.csdn.net/a9529lty/article/details/6454924 一.hibernate映射文件的作用: Hibernate映射文件是Hibernate ...

  9. UI自动化测试框架 ---TestCafe

    UI自动化测试框架 ---TestCafe 官网文档链接: https://devexpress.github.io/testcafe/ https://devexpress.github.io/te ...

  10. DynamoDB的基本操作(一)

    一.创建表 1 var AWS = require("aws-sdk"); 2 AWS.config.update({ 3 region: "us-west-2" ...