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

Matt D md123 at nycap.rr.com
Thu Jun 20 05:25:45 CEST 2013


> 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
> =================================
> 
Hey thanks,
Currently my data is written like this:
2013-06-19
10:09:53,Unknown,(0x270),Plain,HDU,0x265932377fd55dd000,00x1,Standard
MFID (pre-2001),0x666a,
2013-06-19
10:34:43,Unknown,(0x270),Plain,HDU,0xb616ddc0dc3f7d6c00,0xfff,Standard
MFID (pre-2001),0x666a,

And so on and so on, the new line is only at the end of the '0x666a' in
the logfile but here the indentation is forced by my email formatting.
so this is good for now.  Thanks for the help.

But I need to be able to open a text file of choice to write the logfile
to. so now i put a button on my panel with the following:

    btn = wx.Button(self, -1, "Click me")
    sizer.Add(btn, pos=(7,2))

For some reason i cant not for the life of me figure out how to bind the
click event to wxFileDialog.  Like if i try:

    btn.Bind(wx.EVT_BUTTON, self.openfile)

I get the AttributeError: object has no attribute 'openfile'

Or if i put something like:

  def openfile(self, event):
        dlg = wx.FileDialog(self, "Choose a file", os.getcwd(), "",
"*.*", wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
               path = dlg.GetPath()
               mypath = os.path.basename(path)
               self.SetStatusText("You selected: %s" % mypath)
  dlg.Destroy()

or anything like it the whole layout of the panel gets destroyed.
I have been looking for examples and readying the
http://zetcode.com/wxpython/events/ and reading many others but i am
having lots of trouble grasping this.  The examples are always in a
'wxFrame' or part of an 'app' so the differences from mine (wxPanel)
confuse me.
THanks!


More information about the Tutor mailing list