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.