Hello,
I'm very much new to Python and I am stumbling on my first 'experiment'.
What I want to do is create an application that operates as an on-screen log viewer where Python prints information it is processing into a wxTextCrl widget (called 'sampler' in my example).

I am using Boa Constructor to build the application. The problem I am having is understanding where in the code I can send information to the application. I have seen how to pass code into an application using event handlers from within the frame, but I want the event to happen outside of this context and then push the text into the frame to be formatted (unless there is a better way to handle this). Once application.MainLoop() is called I don't seem to be able to talk to the app.

Here is where my code stands at present:

App1.py

#!/usr/bin/env python
#Boa:App:BoaApp

import wx

import Frame1

modules ={'Frame1': [1, 'Main frame of Application', u'Frame1.py']}

class BoaApp(wx.App):
    def OnInit(self):
        self.main = Frame1.create(None)
        self.main.Show()
        self.SetTopWindow(self.main)
        return True

def main():
    application = BoaApp(0)
    application.main.sampler.AppendText('This is a test')
    application.MainLoop()

if __name__ == '__main__':
    main()

Frame1.py

#Boa:Frame:Frame1

import wx

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1SAMPLER, 
] = [wx.NewId() for _init_ctrls in range(2)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(563, 427), size=wx.Size(400, 250),
              style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
        self.SetClientSize(wx.Size(392, 212))

        self.sampler = wx.TextCtrl(id=wxID_FRAME1SAMPLER, name='sampler',
              parent=self, pos=wx.Point(0, 0), size=wx.Size(392, 212), style=0,
              value='')

    def __init__(self, parent):
        self._init_ctrls(parent)

So my top level object is called application.
My frame object is called main
My text control widget is called sampler

I have been using the line:

application.main.sampler.AppendText('This is a test')

to try and access the text control.

If I append text to the widget before the MainLoop() it works. If I try and append text afterwards it fails.

Can someone please point me in the right direction?

Recommended Answers

All 5 Replies

wxPython, like most other GUI modules is event driven. This means that until something triggers an event, processing is more or less waiting for something to happen.

I'm not sure what you mean by appending text afterwards? Do you mean literally after the mainloop() statement? Because the program stays in the mainloop() until the application is closed.

What you'll need to do is create an event that will allow you to input text and then use your appendText method to append it to the textCtrl.

I would suggest binding an event within Frame1's __init__ method that captures a key press, pops up a TextInputDialog, and then captures the input from the Text dialog and appends it to the textCtrl

Ultimately I want the application to do an automatic backup of file when a file is changed. It won't be triggered by user interaction so unless there is a way of capturing a non-user event then I'm going to be in trouble. I already have the code for this portion and I was using a print command to display the information within the console. Now I want to have this print command show in the GUI but I just can't find a way of getting this to work because the app is essentially closed as soon as MainLoop() runs.

wx.TextCtrl() does have an IsModified() method that returns True if the text has been modified. We might have to set up a simple example to test this all out.

Sorry, I haven't been very clear.

What I want is for the app to update programatically with no user input. I have another script that monitors a directory for changes to files and then makes a zip backup of the changes. It is essentially an autobackup script. Currently as feedback the autobackup script simply prints what it is doing on screen (in the command window) but now I want to set up a text control to accept the status report of what the script is doing. Ultimately I plan on having other functions for the application but I thought this would be a fairly simple start.

I am struggling to get a wxPython GUI to take events from within the program. If it was a user initiated event it wouldn't be an issue because all the samples I have seen rely on user input. I'm thinking that any programatic event would illustrate the solution - but I don't have any idea where to look. Even a random timer that sends a status report to the text control would probably show me the direction to head. I'm guessing I need to find a way of building a custom event.

You can set up a timer no problem and use that as your event-driving mechanism. Say a repeating five minute timer, which goes off and runs your autobackup script, dumping the results to the TextCtrl

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.