user1106979
user1106979

Reputation: 33

How to refer to a base instance python gtk widget from a function in a module

I am writing a Python GTK application for studying some sort of math data. The main script has a single class with only three methods: __INIT__, main(self) for starting the loop and delete_event for killing it.

__INIT__ creates the GUI, which includes a TextBuffer and TextView widgets so that the analysis functions (defined on a separate functions.py module) can output their results to a common log/message area. A relevant extract follows:

include module functions(.py)

(...)

class TURING:

  def __init__(self):

    self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

    (...)

    self.logscroll = gtk.ScrolledWindow()
    self.logscroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)

    self.logbuffer = gtk.TextBuffer()

    self.logpage = gtk.TextView(self.logbuffer)
    self.logpage.set_editable(gtk.FALSE)
    self.logpage.set_cursor_visible(gtk.FALSE)
    self.logpage.set_wrap_mode(gtk.WRAP_CHAR)
    self.logscroll.add(self.logpage)
    self.logscroll.show()
    self.logpage.show()

    (...)

    enditer = self.logbuffer.get_end_iter()
    self.logbuffer.insert(enditer, 'Welcome!')

    (...)

  def main(self):
    gtk.main()

if __name__ == "__main__":
  turing = TURING()
  turing.main()

The intermediate two lines successfully print a welcome message onto the message area defined by self.logpage.

Now, one of the functions in method functions checks whether the database is up to date and if not asks the user to load a new batch of raw data.

One way of doing this is to include a menu item that triggers that function, like this:

item_dataCheck.connect("activate", functions.data_check, '')

functions.data_check runs fine however when it tries to write its output to self.logbuffer an error is thrown complaining that menu item item_dataCheck has no property logbuffer. The offending code is

 enditer = self.logbuffer.get_end_iter()
 self.logbuffer.insert(enditer, 'Please update the database.')

Obviously the name self is representing the widget that invoked the function, viz., item_dataCheck. My question is how can I from functions.data_check refer directly to logbuffer as a member of the turing instance of the TURING class. I tried to write

 enditer = turing.logbuffer.get_end_iter()
 turing.logbuffer.insert(enditer, 'Please update the database.')

but that's is not working. I have tried hard to find a solution but with no success.

I believe the matter is quite trivial and I know I still have some serious conceptual problems with Python and OOP, but any help will be heartly appreciated. (I started out card punching Fortran programs on a Burroughs mainframe, so I could count on some community mercy...)

Upvotes: 0

Views: 312

Answers (1)

C2H5OH
C2H5OH

Reputation: 5602

You can provide additional arguments when connecting signals to functions or methods. So your handler functions.data_check can accept extra arguments apart from self:

def data_check(self, logbuffer):
    # ...

Then, you can connect with arguments:

item_dataCheck.connect("activate", functions.data_check, logbuffer)

Also, the self parameter is normally used as the first parameter in method definitions. This is a very strong convention so you should use obj or something similar instead. Specially since your signal handlers may be methods too; in which case you could mess it up with its arguments.

Upvotes: 1

Related Questions