熊猫数据框导入到WxGlade GUI

时间:2019-04-09 09:03:45

标签: python python-3.x pandas pyscripter wxglade

我一直在尝试在GUI中使用DataFrames,但遇到了一些麻烦,这确实很烦人。我的代码可以运行,但是我看不到要在我的GUI中显示DataFrame的内容。我已经创建了网格和样式,但不会提取数据。

能帮我看看我在想什么。

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# generated by wxGlade 0.9.3 on Tue Apr  9 09:46:29 2019
#

import wx
import wx.grid
import numpy as np
import pandas as pd

#colors

EVEN_ROW_COLOUR = '#CCE6FF'
GRID_LINE_COLOUR = '#ccc'

# begin wxGlade: dependencies
# end wxGlade

# begin wxGlade: extracode
# end wxGlade


class MySD_Frame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MySD_Frame.__init__
        kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.SetSize((1289, 685))
        self.button_6 = wx.Button(self, wx.ID_ANY, "button_6")
        self.button_7 = wx.Button(self, wx.ID_ANY, "button_7")
        self.button_8 = wx.Button(self, wx.ID_ANY, "button_8")
        self.button_11 = wx.Button(self, wx.ID_ANY, "button_11")
        self.button_9 = wx.Button(self, wx.ID_ANY, "button_9")
        self.button_10 = wx.Button(self, wx.ID_ANY, "button_10")
        self.MyTable = wx.grid.Grid(self, wx.ID_ANY, size=(1, 1))

        self.__set_properties()
        self.__do_layout()
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MySD_Frame.__set_properties
        self.SetTitle("frame")
        self.SetBackgroundColour(wx.Colour(50, 58, 114))
       # self.GridTableBase.
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MySD_Frame.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_6 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_9 = wx.BoxSizer(wx.VERTICAL)
        sizer_10 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_3 = wx.BoxSizer(wx.VERTICAL)
        sizer_4 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_5 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_1 = wx.GridSizer(1, 9, 0, 0)
        sizer_15 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_14 = wx.BoxSizer(wx.HORIZONTAL)
        bitmap_1 = wx.StaticBitmap(self, wx.ID_ANY, wx.Bitmap('PNG PATH', wx.BITMAP_TYPE_ANY))
        sizer_3.Add(bitmap_1, 0, wx.ALIGN_CENTER, 0)
        sizer_14.Add((20, 0), 0, 0, 0)
        sizer_14.Add(self.button_6, 0, wx.ALIGN_CENTER_VERTICAL, 0)
        grid_sizer_1.Add(sizer_14, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(self.button_7, 0, wx.ALIGN_CENTER_VERTICAL, 0)
        grid_sizer_1.Add(self.button_8, 0, wx.ALIGN_CENTER_VERTICAL, 0)
        grid_sizer_1.Add(self.button_11, 0, wx.ALIGN_CENTER_VERTICAL, 0)
        grid_sizer_1.Add((0, 0), 0, 0, 0)
        grid_sizer_1.Add((0, 0), 0, 0, 0)
        grid_sizer_1.Add((0, 0), 0, 0, 0)
        grid_sizer_1.Add(self.button_9, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT, 0)
        sizer_15.Add((40, 20), 0, 0, 0)
        sizer_15.Add(self.button_10, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_RIGHT, 0)
        sizer_15.Add((20, 20), 0, 0, 0)
        grid_sizer_1.Add(sizer_15, 1, wx.EXPAND, 0)
        sizer_5.Add(grid_sizer_1, 1, wx.EXPAND, 0)
        sizer_4.Add(sizer_5, 1, wx.EXPAND, 0)
        sizer_3.Add(sizer_4, 1, wx.EXPAND, 0)
        sizer_1.Add(sizer_3, 1, wx.EXPAND, 0)
        sizer_10.Add((20, 20), 0, 0, 0)
        sizer_10.Add(self.MyTable, 1, wx.EXPAND, 0)
        sizer_10.Add((20, 20), 0, 0, 0)
        sizer_9.Add(sizer_10, 1, wx.EXPAND, 0)
        sizer_9.Add((20, 60), 0, 0, 0)
        sizer_6.Add(sizer_9, 1, wx.EXPAND, 0)
        sizer_2.Add(sizer_6, 1, wx.EXPAND, 0)
        sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        self.Layout()
        # end wxGlade

# end of class MySD_Frame



class DataTable(wx.grid.GridTableBase):
    def __init__(self, data=None):
        wx.grid.GridTableBase.__init__(self)
        self.headerRows = 1
        if data is None:
            data = pd.read_excel('EXCEL PATH')
        self.data = data

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

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


    def GetValue(self, row, col):
        if col == 0:
            return self.data.index[row]
        return self.data.iloc[row, col - 1]

    def SetValue(self, row, col, value):
        self.data.iloc[row, col - 1] = value

    def GetColLabelValue(self, col):
        if col == 0:
            if self.data.index.name is None:
                return 'Index'
            else:
                return self.data.index.name
        return str(self.data.columns[col - 1])

    def GetTypeName(self, row, col):
        return wx.grid.GRID_VALUE_STRING

    def GetAttr(self, row, col, prop):
        attr = wx.grid.GridCellAttr()
        if row % 2 == 1:
            attr.SetBackgroundColour(EVEN_ROW_COLOUR)
        return attr


    def _init_gui(self):
        df = pd.read_excel('EXCEL PAT')
        table = DataTable(df)

        self.table = DataTable(df)
        self.frame.MyTable.SetTable(self.table, takeOwnership=True)
        grid.AutoSizeColumns()

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(grid, 1, wx.EXPAND)
        self.SetSizer(sizer)

        self.Bind(wx.EVT_CLOSE, self.exit)



class MyApp(wx.App):
    def OnInit(self):
        self.frame = MySD_Frame(None, wx.ID_ANY, "")
        self.SetTopWindow(self.frame)
        self.frame.Show()
        return True

        wx.Frame.__init__(self, None, wx.ID_ANY, "Pandas")
        self._init_gui()
        self.Layout()
        self.Show()

# end of class MyApp

if __name__ == "__main__":
    app = MyApp(0)
    app.MainLoop()

我本来是用tkinter来构建它的,但是我感到非常沮丧,然后搬到了wxglade。现在,代码开始运行,并且gui显示以及数据表似乎丢失了数据。

失望...猜测您必须是C#程序员才能在这里获得任何帮助,因为python社区似乎是如此有限。对于任何想知道我已经阅读,观看和观看过该区域的视频的人,在尝试解决该问题时我都收到了一些错误报告,但是在逐步解决问题时似乎总是回到这个问题上。

  File "This code", line 66, in __do_layout
    sizer_7 = wx.grid.MyTable(self.ExcelSheet1)
TypeError: wx._grid.MyTable represents a C++ abstract class and cannot be instantiated

(我不再抛出此错误,但是网格仍然无法初始化)

再次...任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

使用“ GridTableBase”作为网格名称是一个非常奇怪的选择。

无论如何,在创建包括网格的框架后:

    self.table = DataTable(df)
    self.frame.GridTableBase.SetTable(self.table, takeOwnership=True)

wxPython的网格演示GridCustTable.py或GridHugeTable.py应该有足够的示例来创建虚拟网格。