pythongtkpygtkgtktextview

PyGTK. How to show text in a window's TextView widget upon left click on the status icon


I've got a small PyGTK program, that has a statusicon. Upon left click on the statusicon a window with a TextView should appear and a predefined text should be shown in the TextView widget. My problem is that I don't know how to pass the text to shown as a parameter to the method that creates the window. I can create the window with a TextView without problems, but I cannot insert text into it. Here's my code:

import gtk
import keybinder

class PyPPrinter(object):
    def __init__(self):
        self.staticon = gtk.StatusIcon()
        self.staticon.set_from_stock(gtk.STOCK_INDEX)
        self.staticon.set_visible(True)
        self.staticon.connect('activate', self.browser(output_text = 'text'))
        gtk.main()

    def browser(self, window, output_text):
        browser = gtk.Window()
        browser.set_usize(600, 500)
        textbox = gtk.TextView()
        text = gtk.TextBuffer()
        text.set_text(output_text)
        textbox.set_buffer(text)
        browser.add(textbox)
        browser.show_all()        

if __name__ == '__main__':
    PyPPrinter()

This code gives me an exception: TypeError: browser() takes exactly 3 arguments (2 given). Perhaps I should also pass a value for the window parameter, but what should it be?


Solution

  • Two variants:

    Change connect part:

    self.staticon.connect('activate', self.browser(output_text = 'text'))
    

    to:

    self.staticon.connect('activate', self.browser, 'text')
    

    or change Handler-Signature:

    def browser(self, window, output_text):
    

    to:

    def browser(self, window):