2017-10-15 31 views
1

如何在GTK中打印按鈕文本?如何獲取PyGObject中的按鈕文本?

import gi 
gi.require_version('Gtk','3.0') 
from gi.repository import Gtk 

class MainWindow(Gtk.Window): 
    def __init__(self): 
     Gtk.Window.__init__(self) 
     self.button = Gtk.Button("Hello") 
     self.button.connect('pressed',self.print_button_name) 
     self.add(self.button) 

    def print_button_name(self,widget): 
     print(MainWindow.button.name) # I want to print button text here 

win = MainWindow() 
win.show_all() 
win.connect('delete-event',Gtk.main_quit) 
Gtk.main() 

我正在使用python3與PyGObject,我想打印按鈕文本。在這種情況下,按鈕文本是「你好」。

我該怎麼做?

回答

1

您正在使用類MainWindow而不是實例屬性。

變化的回調方法:

def print_button_name(self,widget): 
    print(self.button.get_label()) # This will print correctly 
相關問題