new to the list - opening a text file in a grid using splitterwindows

Hello all,

I'm new to this list (and to wxPython). I'm going trough Manning's
book and whatever I can find online to learn it, and so far it's going
well. I managed to create a small app that reads a CSV file and
populates a grid with it. Now, what is I want to split the window, so
the grid would be in the left and something else in the right?

my code follows:

TIA

#!/usr/bin/env python

"""
Python script to display CSV file in a table using wxPython
the code originally came from
from http://www.velocityreviews.com/forums/t330788-how-to-update-window-after-wxgrid-is-updated.html

"""

import os, sys, csv
import wx, wx.grid

class MyFrame(wx.Frame):
  def __init__(self, parent, ID, title, size=(200,200)):
    wx.Frame.__init__(self, parent, ID, title,(-1,-1),size)
    self.CreateStatusBar()

    self.dirname=os.getcwd()

#create the file menu
    filemenu=wx.Menu()
    filemenu.Append(wx.ID_OPEN, '&Open', 'Open data file')
    filemenu.Append(wx.ID_EXIT, 'E&xit', 'Exit the program')

#create the menubar for the frame and add the menu to it
    menuBar=wx.MenuBar()
    menuBar.Append(filemenu, '&File')
    self.SetMenuBar(menuBar)

#set up menu events
    wx.EVT_MENU(self, wx.ID_OPEN, self.OnOpen)
    wx.EVT_MENU(self, wx.ID_EXIT, self.Exit)

#create the toolbar for the frame
    toolbar = self.CreateToolBar()
    toolbar.SetToolBitmapSize((16,16)) # sets icon size

    openTool = toolbar.AddSimpleTool(wx.ID_OPEN,
wx.Bitmap('images/silk/application.png'),"Open", "Open data file")

    toolbar.AddSeparator()
    toolbar.Realize()

#splits the window in two:

# splitter = wx.SplitterWindow(self, -1)
# panel1 = wx.Panel(splitter, -1)
# panel1.SetBackgroundColour(wx.LIGHT_GREY)
# panel2 = wx.Panel(splitter, -1)
# panel2.SetBackgroundColour(wx.WHITE)
# splitter.SplitVertically(panel1, panel2, 200)
# splitter.Initialize(panel1)
    
    self.Show(True)
    return

#Open the file and populate the table

  def OnOpen(self, event):
    dlg=wx.FileDialog(self, 'Choose a file',
    self.dirname, '','TXT files (*.txt)|*.txt|CSV files
(*.csv)|*.csv|All files(*.*)|*.*',wx.OPEN)
    if dlg.ShowModal() == wx.ID_OK:
      self.dirname=dlg.GetDirectory()
      self.filename=os.path.join(self.dirname,dlg.GetFilename())
      self.file=file(self.filename, 'r')

    #check for file format with sniffer
      dialect = csv.Sniffer().sniff(self.file.read(1024))
      self.file.seek(0)
      csvfile=csv.reader(self.file,dialect)

    #grab a sample and see if there is a header
      sample=self.file.read(2048)
      self.file.seek(0)
      if csv.Sniffer().has_header(sample):
        colnames=csvfile.next()
      else:
        row=csvfile.next()
        colnames=[]
        for i in range(len(row)):
          colnames.append('col%d' % i)
        self.file.seek(0)

      if getattr(self, 'grid', 0): self.grid.Destroy()
      self.grid=wx.grid.Grid(self, -1)
      self.grid.CreateGrid(0, len(colnames))

    #fill in headings
      for i in range(len(colnames)):
        self.grid.SetColLabelValue(i, colnames[i])
    
    #fill in rows
    r=0
    for row in csvfile:
# print row
      self.grid.AppendRows(1)
      for i in range(len(row)):
        try:
          self.grid.SetCellValue(r, i, row[i])
        except:
          self.grid.AppendCols(1, True)
# print r, i, range(len(row)), row[i]
      r += 1
    self.file.close()
    self.grid.AutoSizeColumns(True)
# self.Refresh(True, self.grid.GetRect())
    self.twiddle()

  def twiddle(self): #
    x,y = self.GetSize()
    self.SetSize((x, y+1))
    self.SetSize((x,y))

  def Exit(self, event):
    if getattr(self, 'file',0):
      self.file.close()
      self.Close(True)

class csv_view(wx.App):
  def OnInit(self):
    self.frame=MyFrame(None, -1, 'PyStereo', size=(900,600))
    self.SetTopWindow(self.frame)
    return True

if __name__ == '__main__':
  app=csv_view()
  app.MainLoop()

···

--
Carlos Henrique Grohmann - Geologist D.Sc.
a.k.a. Guano - Linux User #89721
ResearcherID: A-9030-2008
carlos dot grohmann at gmail dot com
http://www.igc.usp.br/pessoais/guano/
_________________
"Good morning, doctors. I have taken the liberty of removing Windows
95 from my hard drive."
--The winning entry in a "What were HAL's first words" contest judged
by 2001: A SPACE ODYSSEY creator Arthur C. Clarke

Can’t stop the signal.

Carlos Guâno Grohmann wrote:

Hello all,

I'm new to this list (and to wxPython). I'm going trough Manning's
book and whatever I can find online to learn it, and so far it's going
well. I managed to create a small app that reads a CSV file and
populates a grid with it. Now, what is I want to split the window, so
the grid would be in the left and something else in the right?

my code follows:

TIA

#!/usr/bin/env python

"""
Python script to display CSV file in a table using wxPython
the code originally came from
from http://www.velocityreviews.com/forums/t330788-how-to-update-window-after-wxgrid-is-updated.html

"""

import os, sys, csv
import wx, wx.grid

class MyFrame(wx.Frame):
  def __init__(self, parent, ID, title, size=(200,200)):
    wx.Frame.__init__(self, parent, ID, title,(-1,-1),size)
    self.CreateStatusBar()

    self.dirname=os.getcwd()

#create the file menu
    filemenu=wx.Menu()
    filemenu.Append(wx.ID_OPEN, '&Open', 'Open data file')
    filemenu.Append(wx.ID_EXIT, 'E&xit', 'Exit the program')

#create the menubar for the frame and add the menu to it
    menuBar=wx.MenuBar()
    menuBar.Append(filemenu, '&File')
    self.SetMenuBar(menuBar)

#set up menu events
    wx.EVT_MENU(self, wx.ID_OPEN, self.OnOpen)
    wx.EVT_MENU(self, wx.ID_EXIT, self.Exit)

#create the toolbar for the frame
    toolbar = self.CreateToolBar()
    toolbar.SetToolBitmapSize((16,16)) # sets icon size

    openTool = toolbar.AddSimpleTool(wx.ID_OPEN,
wx.Bitmap('images/silk/application.png'),"Open", "Open data file")

    toolbar.AddSeparator()
    toolbar.Realize()

#splits the window in two:

# splitter = wx.SplitterWindow(self, -1)
# panel1 = wx.Panel(splitter, -1)
# panel1.SetBackgroundColour(wx.LIGHT_GREY)
# panel2 = wx.Panel(splitter, -1)
# panel2.SetBackgroundColour(wx.WHITE)
# splitter.SplitVertically(panel1, panel2, 200)
# splitter.Initialize(panel1)
    
    self.Show(True)
    return

#Open the file and populate the table

  def OnOpen(self, event):
    dlg=wx.FileDialog(self, 'Choose a file',
    self.dirname, '','TXT files (*.txt)|*.txt|CSV files
(*.csv)|*.csv|All files(*.*)|*.*',wx.OPEN)
    if dlg.ShowModal() == wx.ID_OK:
      self.dirname=dlg.GetDirectory()
      self.filename=os.path.join(self.dirname,dlg.GetFilename())
      self.file=file(self.filename, 'r')

    #check for file format with sniffer
      dialect = csv.Sniffer().sniff(self.file.read(1024))
      self.file.seek(0)
      csvfile=csv.reader(self.file,dialect)

    #grab a sample and see if there is a header
      sample=self.file.read(2048)
      self.file.seek(0)
      if csv.Sniffer().has_header(sample):
        colnames=csvfile.next()
      else:
        row=csvfile.next()
        colnames=
        for i in range(len(row)):
          colnames.append('col%d' % i)
        self.file.seek(0)

      if getattr(self, 'grid', 0): self.grid.Destroy()
      self.grid=wx.grid.Grid(self, -1)
      self.grid.CreateGrid(0, len(colnames))

    #fill in headings
      for i in range(len(colnames)):
        self.grid.SetColLabelValue(i, colnames[i])
    
    #fill in rows
    r=0
    for row in csvfile:
# print row
      self.grid.AppendRows(1)
      for i in range(len(row)):
        try:
          self.grid.SetCellValue(r, i, row[i])
        except:
          self.grid.AppendCols(1, True)
# print r, i, range(len(row)), row[i]
      r += 1
    self.file.close()
    self.grid.AutoSizeColumns(True)
# self.Refresh(True, self.grid.GetRect())
    self.twiddle()

  def twiddle(self): #
    x,y = self.GetSize()
    self.SetSize((x, y+1))
    self.SetSize((x,y))

  def Exit(self, event):
    if getattr(self, 'file',0):
      self.file.close()
      self.Close(True)

class csv_view(wx.App):
  def OnInit(self):
    self.frame=MyFrame(None, -1, 'PyStereo', size=(900,600))
    self.SetTopWindow(self.frame)
    return True

if __name__ == '__main__':
  app=csv_view()
  app.MainLoop()

I'm not sure why, but the problem appears to lie with this line:

splitter.Initialize(panel1)

If you comment it out, the program splits as it should. If you don't, the left panel (i.e. panel1) expands to take up all the space in the window.

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

Oops.. the splitter.Initialize shouldn't be there. It was commented in
my code, as it was not working. Without the splitter.Initialize, I get
two panels, but the grid is not populated with any data. This is the
kind of file I'm trying to open:

145 23

136 40

140 47

155 15

160 63

150 65

148 48

160 55

172 34

180 10

160 15

160 50

170 15

160 35

thanks

···

I'm not sure why, but the problem appears to lie with this line:

splitter.Initialize(panel1)

If you comment it out, the program splits as it should. If you don't, the
left panel (i.e. panel1) expands to take up all the space in the window.

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

--
Carlos Henrique Grohmann - Geologist D.Sc.
a.k.a. Guano - Linux User #89721
ResearcherID: A-9030-2008
carlos dot grohmann at gmail dot com
http://www.igc.usp.br/pessoais/guano/
_________________
"Good morning, doctors. I have taken the liberty of removing Windows
95 from my hard drive."
--The winning entry in a "What were HAL's first words" contest judged
by 2001: A SPACE ODYSSEY creator Arthur C. Clarke

Can’t stop the signal.

Carlos Guâno Grohmann wrote:

Oops.. the splitter.Initialize shouldn't be there. It was commented in
my code, as it was not working. Without the splitter.Initialize, I get
two panels, but the grid is not populated with any data. This is the
kind of file I'm trying to open:

145 23

136 40

140 47

155 15

160 63

150 65

148 48

160 55

172 34

180 10

160 15

160 50

170 15

160 35

thanks

You are correct...the grid is just a crunched up ugly widget. I haven't had need of the csv module yet, so I don't know if the problem is it or not. The grid code looks ok from just a cursory look, however I've never used the Append methods that you're using. I would sprinkle my loops with print statements to see if they are actually executing and spitting out the data you expect them to. If not, fix them.

- Mike

···

  

I'm not sure why, but the problem appears to lie with this line:

splitter.Initialize(panel1)

If you comment it out, the program splits as it should. If you don't, the
left panel (i.e. panel1) expands to take up all the space in the window.

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Thanks for the quick response, Mike.

The grid code works if I comment all lines about splitting the frame.

Carlos

···

On Mon, Apr 13, 2009 at 14:42, Mike Driscoll <mike@pythonlibrary.org> wrote:

Carlos Guâno Grohmann wrote:

Oops.. the splitter.Initialize shouldn't be there. It was commented in
my code, as it was not working. Without the splitter.Initialize, I get
two panels, but the grid is not populated with any data. This is the
kind of file I'm trying to open:

145 23

136 40

140 47

155 15

160 63

150 65

148 48

160 55

172 34

180 10

160 15

160 50

170 15

160 35

thanks

You are correct...the grid is just a crunched up ugly widget. I haven't had
need of the csv module yet, so I don't know if the problem is it or not. The
grid code looks ok from just a cursory look, however I've never used the
Append methods that you're using. I would sprinkle my loops with print
statements to see if they are actually executing and spitting out the data
you expect them to. If not, fix them.

- Mike

I'm not sure why, but the problem appears to lie with this line:

splitter.Initialize(panel1)

If you comment it out, the program splits as it should. If you don't, the
left panel (i.e. panel1) expands to take up all the space in the window.

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

--
Carlos Henrique Grohmann - Geologist D.Sc.
a.k.a. Guano - Linux User #89721
ResearcherID: A-9030-2008
carlos dot grohmann at gmail dot com
http://www.igc.usp.br/pessoais/guano/
_________________
"Good morning, doctors. I have taken the liberty of removing Windows
95 from my hard drive."
--The winning entry in a "What were HAL's first words" contest judged
by 2001: A SPACE ODYSSEY creator Arthur C. Clarke

Can’t stop the signal.

Carlos Guâno Grohmann wrote:

Hello all,

I'm new to this list (and to wxPython). I'm going trough Manning's
book and whatever I can find online to learn it, and so far it's going
well. I managed to create a small app that reads a CSV file and
populates a grid with it. Now, what is I want to split the window, so
the grid would be in the left and something else in the right?

For starters the grid needs to be a child of the splitter instead of the frame.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Mike Driscoll wrote:

I'm not sure why, but the problem appears to lie with this line:

splitter.Initialize(panel1)

If you comment it out, the program splits as it should. If you don't, the left panel (i.e. panel1) expands to take up all the space in the window.

Look at the docs for the Initialize method. That is what it is supposed to do.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

Mike Driscoll wrote:

I'm not sure why, but the problem appears to lie with this line:

splitter.Initialize(panel1)

If you comment it out, the program splits as it should. If you don't, the left panel (i.e. panel1) expands to take up all the space in the window.

Look at the docs for the Initialize method. That is what it is supposed to do.

I kind of figured as much.

- Mike