Reputation: 379
I've been viewing the wx demo for hours and just can't wrap my head around this.
I need to pull info from my database and store it's columns and values in a grid for viewing only (as of now)
Could someone make me a example script of how to do it just so I can then implement it with my own data. The grid I need is fixed positioned and will fit inside a panel (a page of a wx.notebook) I know how to do that part but how to get the grid inside the panel and populate it confuses me.
Additional info: my database holds customer info (name,phone,email)
Upvotes: 5
Views: 7082
Reputation: 769
I found an old minimal example from this page:
I updated it here as PyGridTableBase
and PySimpleApp()
are deprecated, and added a couple self.
so it stops complaining.
import wx
import wx.grid
class GenericTable(wx.grid.GridTableBase):
def __init__(self, data, rowLabels=None, colLabels=None):
wx.grid.GridTableBase.__init__(self)
self.data = data
self.rowLabels = rowLabels
self.colLabels = colLabels
def GetNumberRows(self):
return len(self.data)
def GetNumberCols(self):
return len(self.data[0])
def GetColLabelValue(self, col):
if self.colLabels:
return self.colLabels[col]
def GetRowLabelValue(self, row):
if self.rowLabels:
return self.rowLabels[row]
def IsEmptyCell(self, row, col):
return False
def GetValue(self, row, col):
return self.data[row][col]
def SetValue(self, row, col, value):
pass
data = (("A", "B"), ("C", "D"), ("E", "F"), ("G", "G"), ("F", "F"), ("Q", "Q"))
colLabels = ("Last", "First")
rowLabels = ("1", "2", "3", "4", "5", "6", "7", "8", "9")
class SimpleGrid(wx.grid.Grid):
def __init__(self, parent):
wx.grid.Grid.__init__(self, parent, -1)
self.tableBase = GenericTable(data, rowLabels, colLabels)
self.SetTable(self.tableBase)
class TestFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "A Grid", size=(275, 275))
self.grid = SimpleGrid(self)
app = wx.App()
frame = TestFrame(None)
frame.Show(True)
app.MainLoop()
Tested and running on:
Python 3.9.12
wx.version: 4.2.1a1
wxWidgets 3.2.1
Just a note to the WxPython team in charge of maintaining the demo.
Might be profitable to newcomers to have a for dummies
2nd simpler demo repository with all the current samples but as minimal examples
such as the one above so we could quickly spot the minimal requirements for the examples/tweak them easily as a first approach (without having to first filter through all the bells and whistles of the advanced demo examples).
Then we could jump and see all that's possible if extended to it's limit with the full demo.
Upvotes: 0
Reputation: 2263
import itertools as it
import pandas as pd
df = pd.DataFrame({'A': [1.1, 2.7, 5.3], 'B': [2, 10, 9], 'C': [3.3, 5.4, 1.5], 'D': [4, 7, 15]},
index = ['a1', 'a2', 'a3'])
for row, col in it.product(range(len(df)), range(len(df.columns))):
print ("mygrid.SetCellValue({0}, {1}, {2})".format(row, col, df.iat[row, col])) # **
# Results :
# mygrid.SetCellValue(0, 0, 1.1)
# mygrid.SetCellValue(0, 1, 2)
# ...
# mygrid.SetCellValue(2, 3, 15)
** For practical reasons I use print(...) in the code above
In real code instead of print(...) Use the following :
mygrid.SetCellValue(row, col, str(df.iat[row, col]))
Upvotes: 1
Reputation: 85603
If you are looking at the grid demo using wx.grid.PyGridTableBase and related, you're right: it seems a really cryptic code. However, the key method is:
def GetValue(self, row, col):
return 'something'
the idea is that if you have your data in
self.data = [[1,2,3,4],
[5,6,7,8],
........
]
then this will put your data in the corresponding cells:
def GetValue(self, row, col):
return str( self.data[row][column] )
(self.data
represents your database)
For other more simple examples using wx.grid.Grid with static data or with data fom a MySQL database you can check this and this
The method used to fill a cell with wx.grid.Grid is:
mygrid.SetCellValue(row, col, databasevalue4rowcol)
Upvotes: 6