Reputation: 2462
I'm writing unit tests for my PyGTK2 GUI and at various points I need to check the text that is shown in a gtk.Statusbar
. The class defines a pop()
function but it does not return anything. How can I non-destructively get the text is currently displayed?
Upvotes: 0
Views: 242
Reputation: 5440
This should do it:
from gi.repository import Gtk
class MainWindow(Gtk.Window):
def __init__(self):
super(MainWindow, self).__init__()
self.connect("destroy", lambda x: Gtk.main_quit())
bigbtn = Gtk.Button("Show elements")
bigbtn.connect("clicked", self.on_bigbtn_clicked)
self.stat = Gtk.Statusbar()
for id, lbl in enumerate(["One", "Two", "Three"]):
self.stat.push(id, lbl)
vbox = Gtk.VBox()
vbox.pack_start(bigbtn, True, True, 0)
vbox.pack_start(self.stat, False, False, 0)
self.add(vbox)
self.show_all()
def on_bigbtn_clicked(self, btn):
for el in self.stat.get_message_area():
print(el.get_text())
def run(self):
Gtk.main()
def main(args):
mainwdw = MainWindow()
mainwdw.run()
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
Clicking on the top button will print the contents of the current message. The trick is to first access the message_area
of the status bar, which is of type Gtk.Box
. You can then access the element in the box (which is of type Gtk.Label
- so, a get_text()
gets you the text.
This is Gtk
through Introspection, but it is Python 2. You should be able to modify this code to pygtk
easily - probably just change the import and Gtk
to gtk
.
Upvotes: 2