Sanjay Prajapat
Sanjay Prajapat

Reputation: 305

How to get button text in PyGObject?

How to print button text in GTK ?

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

I am using python3 with PyGObject, I want to print button text. In this case button text is "Hello".

How can i do that?

Upvotes: 1

Views: 616

Answers (1)

José Fonte
José Fonte

Reputation: 4104

Your are using the class MainWindow instead of the instance property.

change the callback method to:

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

Upvotes: 1

Related Questions