How to access grid from a page in a notebook

Hi!, I wanted to access the values of a grid when the grid is on a page on a notebook but I’m doing something wrong, I don’t seem to get to the grid in itself:

import wx
import wx.grid as gridlib

Some classes to use for the notebook pages. Obviously you would

want to use something more meaningful for your application, these

are just for illustration.

class PageOne(wx.Panel):
def init(self, parent):
wx.Panel.init(self, parent)

    mainSizer = wx.BoxSizer(wx.VERTICAL)
    
    
    btnSizer2 = wx.BoxSizer(wx.HORIZONTAL)
    btn5 = wx.Button(self, label="Key bindings")
    btn6 = wx.Button(self, label="Timers")
    btn7 = wx.Button(self, label="Print")
    btn8 = wx.Button(self, label="Print_all")
    btnSizer2.Add(btn5)
    btnSizer2.Add(btn6)
    btnSizer2.Add((1,1), 1, wx.EXPAND)
    btnSizer2.Add(btn7)
    btnSizer2.Add(btn8)
    
    
    myGrid = gridlib.Grid(self)
    myGrid.SetRowLabelSize(0)
    myGrid.SetColLabelSize(0)

    myGrid.CreateGrid(10, 2)
    
    #myGrid.AutoSizeColumns(True)
    #myGrid.AutoSizeRows(True)
    #self.SetSizer(note_sizer)
    #frame_sizer = wx.BoxSizer(wx.HORIZONTAL)
    mainSizer.Add(myGrid, 10, wx.ALL | wx.EXPAND, 1)
    mainSizer.Add(btnSizer2, 0, wx.BOTTOM, 1)
    self.SetSizer(mainSizer)
    

    #frame_sizer.Fit(self)
    myGrid.SetCellValue(0,0,str("Vid0604a654dsd04 fa5sd0f 6asd f1"))
    myGrid.SetCellValue(0,1,str("RQWERTYUI0LKKGHJDFSDASFASVXCZVFSF"))
    myGrid.SetCellFont(0, 0, wx.Font(12, wx.FONTFAMILY_DECORATIVE, wx.NORMAL, wx.FONTWEIGHT_BOLD))
    myGrid.AutoSizeColumns(True)
    myGrid.AutoSizeRows(True)

class MainFrame(wx.Frame):
def init(self):
wx.Frame.init(self, None, title=“Simple Notebook Example”)

    # Here we create a panel and a notebook on the panel
    p = wx.Panel(self)
    nb = wx.Notebook(p)

    # create the page windows as children of the notebook
    page1 = PageOne(nb)

    #FIRST ATTEMPT --> AttributeError: PageOne object has no attribute 'myGrid'
    print page1.myGrid.GetCellValue(0,1)

    # add the pages to the notebook with the label to show on the tab
    nb.AddPage(page1, "Page 1")
   
   #SECOND ATTEMPT --> AttributeError: 'Notebook' object has no aattribute 'page1' 
    print nb.page1.myGrid.GetCellValue(0,1)

    # finally, put the notebook in a sizer for the panel to manage
    # the layout
    sizer = wx.BoxSizer()
    sizer.Add(nb, 1, wx.EXPAND)
    p.SetSizer(sizer)

if name == “main”:
app = wx.App()
MainFrame().Show()
app.MainLoop()

simple_notebook.py (2.42 KB)

That's not a wx, but a Python problem.

You try to access the grid these two ways:

page1.myGrid.GetCellValue(0,1)
nb.page1.myGrid.GetCellValue(0,1)

There are two bugs:

myGrid is created using:
   myGrid = gridlib.Grid(self)

This does not assign a property to the PageOne instance.
If you want to access the grid as page1.myGrid, you have to do something like this:
   self.myGrid = myGrid = gridlib.Grid(self)
  
Also, nb.AddPage(page1, "Page 1") does not assign a property. If you want this, you have to do it yourself:
  nb.page1 = page1

Just ask yourself with some Python knowledge:
How would calling nb.AddPage(page1) tell the Notebook nb.AddPage method that its argument somewhere else has a variable name 'page1'?

Regards,

Dietmar

···

On 06.09.2016 15:46, Marcos del Amo wrote:

Hi!, I wanted to access the values of a grid when the grid is on a page on a notebook but I'm doing something wrong, I don't seem to get to the grid in itself: