simple oop question (hopefully)

Discussion in 'Python' started by Eric_Dexter@msn.com, Oct 30, 2006.

  1. Guest

    I am just trying to acess a function in wordgrid (savefile) to a button
    that is defined in TestFrame. I can't seem to make it work I either
    get an error that my variable isn't global or it makes other
    complaints. thanks in advance.. sorry for the simple question..




    import wx
    import wx.grid as gridlib
    import sys



    #---------------------------------------------------------------------------

    class WordGrid(gridlib.Grid):

    def __init__(self, parent, log):
    gridlib.Grid.__init__(self, parent, -1)
    self.loadFile()

    self.CreateGrid(len(self.rows), self.widestRow)

    for r, row in enumerate(self.rows):
    for c, col in enumerate(row):
    self.SetCellValue(r, c, col)
    self.SetColSize(c, 10*self.widestCol)

    for c, label in enumerate(self.header):
    self.SetColLabelValue(c, label)

    def loadFile(self):
    #from_file
    infile = open(sys.argv[1], 'r') #The first argument passed in is
    the file name
    foundHeader = False
    self.rows = []
    for line in infile:
    if sys.argv[2] in line: #look for the second argument and
    make that the header
    #removefirst = line.split(' ')
    self.header = line.split()
    #foundHeader = 'true'
    continue # we don't want to process this line any
    further
    else:
    self.rows.append(line.split())

    self.widestRow = max([len(r) for r in self.rows])
    self.widestCol = max([len(c) for c in [r for r in self.rows]])
    def savefile(self):
    outfile = open(sys.argv[1], 'w') #open the file defined in the
    output line for writing
    for row in self.rows:
    outfile.write(row)

    print('this is a test to see if I can Crash it')



    class TestFrame(wx.Frame):
    def __init__(self, parent, log):

    wx.Frame.__init__(self, parent, -1, "Dex Tracker Sco Editor",
    size=(640,480))
    p = wx.Panel(self, -1, style=0)
    grid = WordGrid(p, log)
    #grid = CustTableGrid(p, log)
    b = wx.Button(p, -1, "Save Grid")
    b.SetDefault()
    self.Bind(wx.EVT_BUTTON, self.OnButton, b)
    b.Bind(wx.EVT_SET_FOCUS, self.OnButtonFocus)
    bs = wx.BoxSizer(wx.VERTICAL)
    bs.Add(grid, 1, wx.GROW|wx.ALL, 5)
    bs.Add(b)
    p.SetSizer(bs)

    def OnButton(self, evt):
    print "button selected"
    grid = WordGrid(self, log).savefile()
    #self.WordGrid.savefile(self)

    def OnButtonFocus(self, evt):
    print "button focus"

    #---------------------------------------------------------------------------
    #def main():

    def main(From_File, find_string):
    """This is the entire editor for .sco files.. It doesn't realy
    care if it is music or not. Any file that you lay out with even rows
    and collums
    can be displayed The first argument passed to main is the file to
    be used and the second if the string to be used as the command to set
    up the header of the grid.
    The sting you wish to use to identify the header should be placed
    last so it doesn't show up in the grid.
    """

    import sys

    app = wx.PySimpleApp()
    frame = TestFrame(None, sys.stdout)
    frame.Show(True)
    app.MainLoop()
    pass

    if __name__ == '__main__':
    import sys
    #try:
    main(sys.argv[1], sys.argv[2])
     
    , Oct 30, 2006
    #1
    1. Advertising

  2. Guest

    I am tagging this so I can find it again (google groups) www.dexrow.com
    wrote:
    > I am just trying to acess a function in wordgrid (savefile) to a button
    > that is defined in TestFrame. I can't seem to make it work I either
    > get an error that my variable isn't global or it makes other
    > complaints. thanks in advance.. sorry for the simple question..
    >
    >
    >
    >
    > import wx
    > import wx.grid as gridlib
    > import sys
    >
    >
    >
    > #---------------------------------------------------------------------------
    >
    > class WordGrid(gridlib.Grid):
    >
    > def __init__(self, parent, log):
    > gridlib.Grid.__init__(self, parent, -1)
    > self.loadFile()
    >
    > self.CreateGrid(len(self.rows), self.widestRow)
    >
    > for r, row in enumerate(self.rows):
    > for c, col in enumerate(row):
    > self.SetCellValue(r, c, col)
    > self.SetColSize(c, 10*self.widestCol)
    >
    > for c, label in enumerate(self.header):
    > self.SetColLabelValue(c, label)
    >
    > def loadFile(self):
    > #from_file
    > infile = open(sys.argv[1], 'r') #The first argument passed in is
    > the file name
    > foundHeader = False
    > self.rows = []
    > for line in infile:
    > if sys.argv[2] in line: #look for the second argument and
    > make that the header
    > #removefirst = line.split(' ')
    > self.header = line.split()
    > #foundHeader = 'true'
    > continue # we don't want to process this line any
    > further
    > else:
    > self.rows.append(line.split())
    >
    > self.widestRow = max([len(r) for r in self.rows])
    > self.widestCol = max([len(c) for c in [r for r in self.rows]])
    > def savefile(self):
    > outfile = open(sys.argv[1], 'w') #open the file defined in the
    > output line for writing
    > for row in self.rows:
    > outfile.write(row)
    >
    > print('this is a test to see if I can Crash it')
    >
    >
    >
    > class TestFrame(wx.Frame):
    > def __init__(self, parent, log):
    >
    > wx.Frame.__init__(self, parent, -1, "Dex Tracker Sco Editor",
    > size=(640,480))
    > p = wx.Panel(self, -1, style=0)
    > grid = WordGrid(p, log)
    > #grid = CustTableGrid(p, log)
    > b = wx.Button(p, -1, "Save Grid")
    > b.SetDefault()
    > self.Bind(wx.EVT_BUTTON, self.OnButton, b)
    > b.Bind(wx.EVT_SET_FOCUS, self.OnButtonFocus)
    > bs = wx.BoxSizer(wx.VERTICAL)
    > bs.Add(grid, 1, wx.GROW|wx.ALL, 5)
    > bs.Add(b)
    > p.SetSizer(bs)
    >
    > def OnButton(self, evt):
    > print "button selected"
    > grid = WordGrid(self, log).savefile()
    > #self.WordGrid.savefile(self)
    >
    > def OnButtonFocus(self, evt):
    > print "button focus"
    >
    > #---------------------------------------------------------------------------
    > #def main():
    >
    > def main(From_File, find_string):
    > """This is the entire editor for .sco files.. It doesn't realy
    > care if it is music or not. Any file that you lay out with even rows
    > and collums
    > can be displayed The first argument passed to main is the file to
    > be used and the second if the string to be used as the command to set
    > up the header of the grid.
    > The sting you wish to use to identify the header should be placed
    > last so it doesn't show up in the grid.
    > """
    >
    > import sys
    >
    > app = wx.PySimpleApp()
    > frame = TestFrame(None, sys.stdout)
    > frame.Show(True)
    > app.MainLoop()
    > pass
    >
    > if __name__ == '__main__':
    > import sys
    > #try:
    > main(sys.argv[1], sys.argv[2])
     
    , Oct 30, 2006
    #2
    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. Chris Gardner
    Replies:
    0
    Views:
    371
    Chris Gardner
    Oct 29, 2003
  2. Jon Thackray

    Hopefully simple XPath question

    Jon Thackray, Nov 10, 2004, in forum: XML
    Replies:
    9
    Views:
    437
    Joris Gillis
    Nov 13, 2004
  3. Replies:
    4
    Views:
    280
  4. Chuft Captain

    A hopefully simple question

    Chuft Captain, Apr 9, 2009, in forum: Ruby
    Replies:
    5
    Views:
    180
    Chuft Captain
    Apr 9, 2009
  5. Harold Pritchett

    A hopefully simple question

    Harold Pritchett, Feb 16, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    150
    Chris Mattern
    Feb 16, 2005
Loading...

Share This Page