Hi.
I'm starting to learn wxPython and for an exercise I'm writing a
simple CSV file viewer. I just read in the CSV file and create a
wx.Grid with the data. I'm using Python 2.3.2 with wxPython 2.4.2.4.
Everything runs fine under linux, but when I try the same code on a
Win XP machine, I have a window, but the frame the grid is in isn't
seen until I do a minimize/maximize on the window. Besides that, it
seems to run fine. I tried to do a self.Refresh(Tr ue,
self.grid.GetRe ct()), but that didn't help.
Here is the code:
#!/bin/env python
"""
Python script to display CSV file in a table using wxPython
"""
import os, sys, csv
import wx, wx.grid
class MyFrame(wx.Fram e):
def __init__(self, parent, ID, title, size=(200,200)) :
wx.Frame.__init __(self, parent, ID, title,
(-1,-1),size)
self.CreateStat usBar()
self.dirname=os .getcwd()
#create the file menu
filemenu=wx.Men u()
filemenu.Append (wx.ID_OPEN, '&Open', 'Open CSV 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.Menu Bar()
menuBar.Append( filemenu, '&File')
self.SetMenuBar (menuBar)
#set up menu events
wx.EVT_MENU(sel f, wx.ID_OPEN, self.OnOpen)
wx.EVT_MENU(sel f, wx.ID_EXIT, self.Exit)
self.Show(True)
return
def OnOpen(self, event):
dlg=wx.FileDial og(self, 'Choose a file',
self.dirname, '',
'CSV files (*.csv)|*.csv|A ll files
(*.*)|*.*',
wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.dirname=dl g.GetDirectory( )
self.filename=o s.path.join(sel f.dirname,dlg.G etFilename())
self.file=file( self.filename, 'r')
csvfile=csv.rea der(self.file)
#grab a sample and see if there is a header
sample=self.fil e.read(8192)
self.file.seek( 0)
if csv.Sniffer().h as_header(sampl e):
colnames=csvfil e.next()
else:
row=csvfile.nex t()
colnames=[]
for i in len(row):
colnames.append ('col%d' % i)
self.file.seek( 0)
if getattr(self, 'grid', 0): self.grid.Destr oy()
self.grid=wx.gr id.Grid(self, -1)
self.grid.Creat eGrid(0, len(colnames))
#fill in headings
for i in range(len(colna mes)):
self.grid.SetCo lLabelValue(i, colnames[i])
#fill in rows
r=0
for row in csvfile:
self.grid.Appen dRows(1)
for i in range(len(row)) :
try:
self.grid.SetCe llValue(r, i, row[i])
except:
self.grid.Appen dCols(1, True)
print r, i, len(row), row[i]
r += 1
self.file.close ()
self.grid.AutoS izeColumns(True )
self.Refresh(Tr ue, self.grid.GetRe ct())
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=MyFr ame(None, -1, 'CSV viewer', size=(800,500))
self.SetTopWind ow(self.frame)
return True
if __name__ == '__main__':
app=csv_view()
app.MainLoop()
############### ###########
Thanks for any help.
I'm starting to learn wxPython and for an exercise I'm writing a
simple CSV file viewer. I just read in the CSV file and create a
wx.Grid with the data. I'm using Python 2.3.2 with wxPython 2.4.2.4.
Everything runs fine under linux, but when I try the same code on a
Win XP machine, I have a window, but the frame the grid is in isn't
seen until I do a minimize/maximize on the window. Besides that, it
seems to run fine. I tried to do a self.Refresh(Tr ue,
self.grid.GetRe ct()), but that didn't help.
Here is the code:
#!/bin/env python
"""
Python script to display CSV file in a table using wxPython
"""
import os, sys, csv
import wx, wx.grid
class MyFrame(wx.Fram e):
def __init__(self, parent, ID, title, size=(200,200)) :
wx.Frame.__init __(self, parent, ID, title,
(-1,-1),size)
self.CreateStat usBar()
self.dirname=os .getcwd()
#create the file menu
filemenu=wx.Men u()
filemenu.Append (wx.ID_OPEN, '&Open', 'Open CSV 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.Menu Bar()
menuBar.Append( filemenu, '&File')
self.SetMenuBar (menuBar)
#set up menu events
wx.EVT_MENU(sel f, wx.ID_OPEN, self.OnOpen)
wx.EVT_MENU(sel f, wx.ID_EXIT, self.Exit)
self.Show(True)
return
def OnOpen(self, event):
dlg=wx.FileDial og(self, 'Choose a file',
self.dirname, '',
'CSV files (*.csv)|*.csv|A ll files
(*.*)|*.*',
wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.dirname=dl g.GetDirectory( )
self.filename=o s.path.join(sel f.dirname,dlg.G etFilename())
self.file=file( self.filename, 'r')
csvfile=csv.rea der(self.file)
#grab a sample and see if there is a header
sample=self.fil e.read(8192)
self.file.seek( 0)
if csv.Sniffer().h as_header(sampl e):
colnames=csvfil e.next()
else:
row=csvfile.nex t()
colnames=[]
for i in len(row):
colnames.append ('col%d' % i)
self.file.seek( 0)
if getattr(self, 'grid', 0): self.grid.Destr oy()
self.grid=wx.gr id.Grid(self, -1)
self.grid.Creat eGrid(0, len(colnames))
#fill in headings
for i in range(len(colna mes)):
self.grid.SetCo lLabelValue(i, colnames[i])
#fill in rows
r=0
for row in csvfile:
self.grid.Appen dRows(1)
for i in range(len(row)) :
try:
self.grid.SetCe llValue(r, i, row[i])
except:
self.grid.Appen dCols(1, True)
print r, i, len(row), row[i]
r += 1
self.file.close ()
self.grid.AutoS izeColumns(True )
self.Refresh(Tr ue, self.grid.GetRe ct())
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=MyFr ame(None, -1, 'CSV viewer', size=(800,500))
self.SetTopWind ow(self.frame)
return True
if __name__ == '__main__':
app=csv_view()
app.MainLoop()
############### ###########
Thanks for any help.
Comment