2017-10-15 9 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 
関連する問題