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?

有帮助吗?

解决方案

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):
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top