Pandas 数据框导入到 WxGlade GUI
Pandas dataframe import to WxGlade GUI
我一直在尝试在 GUI 中使用 DataFrames,但我遇到了一些非常烦人的问题。我的代码运行了,但我只是看不到要让我的 DataFrame 在我的 GUI 中显示我缺少什么。我已经创建了网格和样式,但它不会提取数据。
你能帮我看看我错过了什么吗?
#!/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 显示数据 table 似乎缺少数据。
失望...我想您必须是 C# 程序员才能在这里获得任何帮助,因为 python 社区似乎非常有限。对于任何想知道我已经阅读、查看和观看该区域的视频的人,我在尝试解决此问题时收到了多个错误报告,但在逐步解决 class 问题时似乎总是回到这个问题.
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
(我不再抛出这个错误,但网格仍然没有初始化)
再次...任何帮助将不胜感激
使用 'GridTableBase' 作为网格的名称是一个非常奇怪的选择。
无论如何,在创建包含网格的框架后:
self.table = DataTable(df)
self.frame.GridTableBase.SetTable(self.table, takeOwnership=True)
wxPython 的网格演示 GridCustTable.py 或 GridHugeTable.py 应该有足够的示例如何创建虚拟网格。
我一直在尝试在 GUI 中使用 DataFrames,但我遇到了一些非常烦人的问题。我的代码运行了,但我只是看不到要让我的 DataFrame 在我的 GUI 中显示我缺少什么。我已经创建了网格和样式,但它不会提取数据。
你能帮我看看我错过了什么吗?
#!/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 显示数据 table 似乎缺少数据。
失望...我想您必须是 C# 程序员才能在这里获得任何帮助,因为 python 社区似乎非常有限。对于任何想知道我已经阅读、查看和观看该区域的视频的人,我在尝试解决此问题时收到了多个错误报告,但在逐步解决 class 问题时似乎总是回到这个问题.
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
(我不再抛出这个错误,但网格仍然没有初始化)
再次...任何帮助将不胜感激
使用 'GridTableBase' 作为网格的名称是一个非常奇怪的选择。
无论如何,在创建包含网格的框架后:
self.table = DataTable(df)
self.frame.GridTableBase.SetTable(self.table, takeOwnership=True)
wxPython 的网格演示 GridCustTable.py 或 GridHugeTable.py 应该有足够的示例如何创建虚拟网格。