wxpython gridtable如何更新

时间:2018-11-24 18:11:05

标签: wxpython

因此,此应用旨在打开一个包含DJI股票数据的csv文件。 csv文件如下所示:

enter image description here

到目前为止,该应用程序如下所示:

enter image description here

我的问题是如何通过用户输入来更新数据表: 例如:用户可以输入公司代码,然后按Enter键,以便对表进行过滤以仅显示与代码中的类型匹配的信息。

尝试了onEnter()。不起作用,返回:

free():invalid pointer
Aborted (Core dump)

代码如下:

import wx
import wx.grid as gridlib
import pandas as pd
import matplotlib.pyplot as plt
import os

class MyDataTable(gridlib.GridTableBase):
    def __init__(self, data=None):
        gridlib.GridTableBase.__init__(self)
        self.headerRows = 0
        if data is None:
            data = pd.DataFrame()

        self.data = data

    def GetNumberRows(self):
        return len(self.data)

    def GetNumberCols(self):
        return len(self.data.columns) + 1



class MyFrame(wx.Frame):
    """Frame class that display data"""
    def __init__(self, parent, title):
        super(MyFrame, self).__init__(parent, title=title, size = (1000,2000))

        self.InitUI()

    def InitUI(self):

        # PANEL
        self.panel = wx.Panel(self, -1) # set id = -1
        self.panel.SetBackgroundColour('White')

        menuBar = wx.MenuBar()
        menu1 = wx.Menu()

        openMenuItem = menu1.Append(wx.ID_OPEN, '&Open', 'Open a file in a dialog')
        menu1.AppendSeparator() # add a separator
        closeMenuItem = menu1.Append(wx.ID_EXIT, '&Quit', 'Close the dialog')
        menuBar.Append(menu1, '&File')
        self.SetMenuBar(menuBar)

        # input widgets
        label_text = wx.StaticText(self.panel, -1, 'Company Code:', pos=(0, 5))
        self.text_enter = wx.TextCtrl(self.panel, -1, "", pos=(110, 1), style=wx.TE_PROCESS_TAB | wx.TE_CENTER | wx.TE_PROCESS_ENTER)
        self.checkBox1 = wx.CheckBox(self.panel, -1, "Open Price", (0,50))
        self.checkBox2 = wx.CheckBox(self.panel, -1, "Highest Price", (100,50))
        self.checkBox3 = wx.CheckBox(self.panel, -1, "Lowest Price", (220,50))
        self.checkBox4 = wx.CheckBox(self.panel, -1, "Close Price", (340,50))
        self.checkBox5 = wx.CheckBox(self.panel, -1, "Volume", (450,50))
        plot_button = wx.Button(self.panel, -1, "Plot", pos=(550, 50))

        self.Bind(wx.EVT_MENU, self.onOpen, openMenuItem)
        self.Bind(wx.EVT_MENU, self.onClose, closeMenuItem)
        self.Bind(wx.EVT_TEXT_ENTER, self.onEnter, self.text_enter)


    def onOpen(self, event):
        filepath = None
        openFileDialog = wx.FileDialog(self, "Open CSV File", "", "", "csv files(*.csv) |*.csv", wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
        if openFileDialog.ShowModal() == wx.ID_CANCEL:
            return
        filepath = openFileDialog.GetPath() # path
        #make a grid table
        panel2 = wx.Panel(self.panel, id=2, pos=(0, 100), size = (800, 500))
        self.df = pd.read_csv(filepath, header=0)
        self.table = MyDataTable(self.df)
        grid = gridlib.Grid(self, 2, pos=(0, 100), size=(self.table.GetNumberRows(), self.table.GetNumberCols()))
        grid.SetRowLabelSize(0)
        grid.SetColLabelSize(30)
        grid.SetTable(self.table)
        grid.AutoSize()
        grid.AutoSizeColumns(True)

        openFileDialog.Destroy()


    def onClose(self, event):
        self.Destroy()

    def onEnter(self, event):
        df2 = self.df[self.df['Codes'] == self.text_enter]
        self.table = MyDataTable(df2)
        grid = gridlib.Grid(self, 2, pos=(0, 100), size=(self.table.GetNumberRows(), self.table.GetNumberCols()))
        grid.SetRowLabelSize(0)
        grid.SetColLabelSize(30)
        grid.SetTable(self.table)
        grid.AutoSize()
        grid.AutoSizeColumns(True)

0 个答案:

没有答案