[Tutor] Need Help Modifying a wxPython GUI (scrolling display and logging)

Prasad, Ramit ramit.prasad at jpmorgan.com
Wed Jun 19 20:25:53 CEST 2013


Matt D wrote:
> >
> >
> > Also note, that unless you do self.logfile.close() it is not guaranteed
> > that the data is being written to file. I prefer to use the following
> > idiom for Python 2.6+ (might be in 2.5, but not sure offhand when it was added).
> >
> > with open('filename.txt', 'a') as f:
> >     # write data
> >
> >
> Thanks!
> Now with some experience using this logger i have found that the items,
> while they may not be in an ideal order, are nonetheless always in the
> same order starting with date/time.  In the interest of getting this
> thing working ASAP the current ordering is acceptable for now; at some
> later time I may try to arrange into some other desired order.
> 
> I am testing the 'a' append mode now.  hoping this will allow for not
> overwriting existing data.
> 
> Where in the program do I put the:
> 	self.logfile.close()
> Is there someway to trigger this from the UI? or even when the program
> is stopped?

A common way to trigger UI actions is a button whose callback calls that.
Or you can bind in an event hook for closing the window.

in __init__ add this line - 
self.Bind(wx.EVT_CLOSE, self.onExit)


def onExit(self, event):
   '''Run when closing'''
   self.logfile.close()
   self.Destroy() # Continue closing the app otherwise 
                  # you will not be able to close it via UI


Although, exiting the program will probably close the file for you....


If you are using append mode ('a') you can just do the following
which will append and close the file for you. 
with open(filename, 'a' ) as logfile:
    logfile.write( data_string )


Make sure you add a newline ('\n') to what you write otherwise all 
your output will be on one line with no spaces

for x in xrange(15):
    with open( filename, 'a' ) as f:
        f.write( str(x) )

========filename=================
01234567891011121314
=================================


~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list