2012-01-29 23 views
2

我有一個小的PyGTK程序,它有一個狀態圖標。在左側單擊狀態圖標時,將出現一個帶有TextView的窗口,並且應該在TextView小部件中顯示預定義的文本。我的問題是,我不知道如何將文本作爲參數傳遞給創建窗口的方法。我可以用TextView創建沒有問題的窗口,但是我不能在其中插入文本。 這裏是我的代碼:PyGTK。如何在窗口的TextView小部件中顯示文本左鍵單擊狀態圖標

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() 

此代碼給了我一個例外:TypeError: browser() takes exactly 3 arguments (2 given)。也許我還應該爲window參數傳遞一個值,但它應該是什麼?

回答

2

兩個變種:

更改連接部分:

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

到:

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

或更改處理程序簽名:

def browser(self, window, output_text): 

到:

def browser(self, window): 
相關問題