Reputation: 357
I am trying to learn pygtk and understand the meaning of the terms in the documentation and tutorial.
According to documentation
So I would conclude that tooltips do not work with buttons. That seems wrong and example code below seems to prove it is wrong. So there is something I don't understand about the meaning of the terms? Are the above statements incorrect? Can anyone explain what I am missing here. Is it that the method get_has_window() does not answer the same question as whether a tooltip will work?
#!/usr/bin/env python
import pygtk
pygtk.require('2.0')
import gtk
class IsButtonNoWindowWidget:
def sillycallback(self, widget, data):
print data
if widget.get_has_window():
print "Which has a window"
else:
print "Which does *not* have a window"
def __init__(self):
# create a properly dismissable top level
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect("destroy", lambda w: gtk.main_quit())
button = gtk.Button("PRESS ME")
button.connect("clicked", self.sillycallback,
"You have clicked the button")
tooltips = gtk.Tooltips()
# according to pygtk documentation:
# Widgets that do not receive events
# (widgets that do not have their own window)
# will *not* work with tooltips.
tooltips.set_tip(button, "just press me, already!")
self.window.add(button)
button.show()
self.window.show()
def main():
gtk.main()
return 0
if __name__ == "__main__":
IsButtonNoWindowWidget()
main()
Upvotes: 1
Views: 734
Reputation: 5440
I'm no expert on the internals of Gtk, so I may be wrong. I suspect there is some confusion...
Gtk.Window is a toplevel window, which is 'managed' by the window manager, and shows the maximize, close buttons and the program name.
Gtk.gdk.window() (depending on the Gdk/Gtk/Language it has different names) is a 'window' which is proper to Gdk, and which provides an area for widgets to be drawn on, and which provides a sensitive area to events such as button presses, and key presses.
Gtk.Label famously has no gdk.window, and cannot capture such events (you have to put a Gtk.EventBox behind it to solve that).
As far as I know (and I could not find any reference to suggest otherwise), Gtk.Button does have a Gdk.Window, and the button's appearance is drawn on that window (and of course it can receive events).
Showing tooltips is the task of Gtk.Widget, and Gtk.Label does derive from Gtk.Widget (do does Gtk.Button):
+-- gobject.GObject
+-- gtk.Object
+-- gtk.Widget
+-- gtk.Misc
+-- gtk.Label
+-- gobject.GObject
+-- gtk.Object
+-- gtk.Widget
+-- gtk.Container
+-- gtk.Bin
+-- gtk.Button
So both should be able to show tooltips, as the following program shows:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# test_label_tooltip.py
#
# Copyright 2017 John Coppens <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('GooCanvas', '2.0')
from gi.repository import Gtk, GooCanvas
class MainWindow(Gtk.Window):
def __init__(self):
super(MainWindow, self).__init__()
self.connect("destroy", lambda x: Gtk.main_quit())
label = Gtk.Label("This is a label",
tooltip_text = "This is the label's tooltip")
button = Gtk.Label("This is a button",
tooltip_text = "This is the button's tooltip")
vbox = Gtk.VBox()
vbox.pack_start(label, False, False, 3)
vbox.pack_start(button, False, False, 3)
self.add(vbox)
self.show_all()
def run(self):
Gtk.main()
def main(args):
mainwdw = MainWindow()
mainwdw.run()
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
In short: The capacity to show tooltips does not depend on if the widget has a Gdk.Window or not. In fact, the tooltip can fall outside the toplevel Gtk.Window, which I suspect would indicate that either it has its own Gtk.Window or is managed by the Window Manager (I really should investigate that):
Upvotes: 2