Skip to content Skip to sidebar Skip to footer

Python/wxpython: Importing Csv File Onto Wxgrid Not Displaying To Fit On Frame

Am working on a project where I have to import CSV files into wx.Grid for further manipulations. I have searched and found a useful way here http://wxpython-users.1045709.n5.nabble

Solution 1:

The biggest issue is that you have a parenting problem. When you create the grid widget, you add it to the frame, but not to a sizer. That causes the grid to be initialized to a small size and it gets stacked on top of the panel. To fix this, you need to set the parent of the grid to one of the panels, add the grid to a sizer and then call Layout on the panel. Here's the updated code for csv2.py:

import wx
import os
import sys, csv
import wx.grid
from csv1 import MyFrame3

classMyFrame(MyFrame3):
    def__init__(self, parent, size = wx.Size(900,600)):
        MyFrame3.__init__ (self, parent)

        self.dirname = os.getcwd()



    # Import/Open CSVdefImportFunc( self, event ):
        '''THIS IMPORTED CSV WILL NEVER EXPAND TO FIT INTO THE FRAME, PLEASE HELP?'''   

        dlg=wx.FileDialog(self, 'Choose a file', self.dirname, '','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)
            filedata = [] #put contents of csvfile into a list
            filedata.extend(csvfile)
            self.file.seek(0)

            #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): #if there is a header
                colnames=csvfile.next() # label columns from first line
                datalist=[] # create a list without the header
                datalist.extend(filedata[1:len(filedata)]) #append data without headerelse:
                row1=csvfile.next() #if there is NO header
                colnames=[]
                for i inrange(len(row1)):
                    colnames.append('col_%d' % i) # label columns as col_1, col_2, etc
                self.file.seek(0)
                datalist=filedata #append data to datalist

        self.file.close()
        self.createGrid(datalist, colnames)
        grid_sizer = wx.BoxSizer(wx.VERTICAL)
        grid_sizer.Add(self.grid, 1, wx.EXPAND)
        self.Right_Panel.SetSizer(grid_sizer)
        self.Right_Panel.Layout()



    #create the griddefcreateGrid(self, datalist, colnames):
        ifgetattr(self, 'grid', 0): self.grid.Destroy()
        self.grid=wx.grid.Grid(self.Right_Panel, 0)
        self.grid.CreateGrid(len(datalist), len(colnames)) #create grid, same size as file (rows, cols)#fill in headingsfor i inrange(len(colnames)):
            self.grid.SetColLabelValue(i, colnames[i])

        #populate the gridfor row inrange(len(datalist)):
            for col inrange(len(colnames)):
                try: 
                    self.grid.SetCellValue(row,col,datalist[row][col])
                except: 
                    pass


        self.grid.AutoSizeColumns(False) # size columns to data (from cvsomatic.py)
        self.twiddle()

    deftwiddle(self): # from http://www.velocityreviews.com/forums/t330788-how-to-update-window-after-wxgrid-is-updated.html
        x,y = self.GetSize()
        self.SetSize((x, y+1))
        self.SetSize((x,y))

    defExit(self, event):
        ifgetattr(self, 'file',0):
            self.file.close()
            self.Close(True)

import wx.lib.mixins.inspection
app = wx.App(0)
Frame_02 = MyFrame(None)
Frame_02.Show()
wx.lib.inspection.InspectionTool().Show()
app.MainLoop()

I also added the Widget Inspection Tool to your code to help me figure out how the panels were laid out and where to put the grid. It is very useful for figuring out problems with widget layout. You can read more about this handy tool on the wxPython wiki:

Post a Comment for "Python/wxpython: Importing Csv File Onto Wxgrid Not Displaying To Fit On Frame"