How to update window after wxGrid is updated?

Discussion in 'Python' started by Tim Williams, May 11, 2004.

  1. Tim Williams

    Tim Williams Guest

    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(True,
    self.grid.GetRect()), 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.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 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.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)

    self.Show(True)
    return

    def OnOpen(self, event):
    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')
    csvfile=csv.reader(self.file)

    #grab a sample and see if there is a header
    sample=self.file.read(8192)
    self.file.seek(0)
    if csv.Sniffer().has_header(sample):
    colnames=csvfile.next()
    else:
    row=csvfile.next()
    colnames=[]
    for i in 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)

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

    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, 'CSV viewer', size=(800,500))
    self.SetTopWindow(self.frame)
    return True

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

    ##########################

    Thanks for any help.
     
    Tim Williams, May 11, 2004
    #1
    1. Advertising

  2. Tim Williams

    Brian Kelley Guest

    Tim Williams wrote:
    > 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(True,
    > self.grid.GetRect()), but that didn't help.


    Annoying isn't it? There are probably many solutions to this problem,
    but the one that I use is:

    > 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))

    This forces a repaint of the window and will also add scroll bars if
    necessary. You are welcome to join us on the wxpython user list as well
    for more in-depth answers.

    You have an unrelated bug in your code as well:

    for i in len(row):

    should be
    for i in range(len(row)):


    Brian
     
    Brian Kelley, May 11, 2004
    #2
    1. Advertising

  3. Tim Williams

    Hans Nowak Guest

    Tim Williams wrote:
    > 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(True,
    > self.grid.GetRect()), but that didn't help.


    I believe wx.Yield() should do the trick.
     
    Hans Nowak, May 12, 2004
    #3
  4. Tim Williams

    Tim Williams Guest

    Brian Kelley <> wrote in message news:<40a11859$0$560$>...
    > Tim Williams wrote:
    > > 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(True,
    > > self.grid.GetRect()), but that didn't help.

    >
    > Annoying isn't it? There are probably many solutions to this problem,
    > but the one that I use is:
    >
    > > 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))
    >
    > This forces a repaint of the window and will also add scroll bars if
    > necessary. You are welcome to join us on the wxpython user list as well
    > for more in-depth answers.
    >
    > You have an unrelated bug in your code as well:
    >
    > for i in len(row):
    >
    > should be
    > for i in range(len(row)):
    >
    >
    > Brian


    Thanks. I caught this bug after I posted. I've been in a class the
    last 3 days. When I get back on Monday, I'll try out your answer and
    the other poster's one.
     
    Tim Williams, May 15, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Lee

    wxGrid?

    Tom Lee, Sep 3, 2003, in forum: Python
    Replies:
    3
    Views:
    548
    Tom Lee
    Sep 3, 2003
  2. GrayGeek

    Can't get wxGrid to work

    GrayGeek, Nov 4, 2003, in forum: Python
    Replies:
    2
    Views:
    767
    GrayGeek
    Nov 4, 2003
  3. Johnny Geling

    wxGrid (wxPYTHON) rowlabels

    Johnny Geling, Nov 13, 2003, in forum: Python
    Replies:
    6
    Views:
    424
    Johnny Geling
    Nov 17, 2003
  4. Kepes Krisztian

    How can I create a wxgrid dinamically ?

    Kepes Krisztian, Nov 24, 2003, in forum: Python
    Replies:
    0
    Views:
    368
    Kepes Krisztian
    Nov 24, 2003
  5. Dmitry Korolyov

    Datagrid not updated during delete, but updated during insert and update

    Dmitry Korolyov, Sep 22, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    423
    Dmitry Korolyov
    Sep 22, 2003
Loading...

Share This Page