Tooltips aren't showing up on my buttons. I should probably clear this up: they show up on my regular buttons, but any buttons that are added to my overlay object don't show the tooltips.
# Overlay
self.overlay = Gtk.Overlay()
self.overlay.show()
# Image
self.image_pixbuf = GdkPixbuf.Pixbuf.new_from_file("pics/sgcity.png")
self.image_pixbuf = self.image_pixbuf.scale_simple(1200, 720, GdkPixbuf.InterpType.BILINEAR)
self.image = Gtk.Image()
self.image.set_from_pixbuf(self.image_pixbuf)
self.overlay.add(self.image)
self.image.show()
# Printer Buttons
for i in range(len(self.printbuttons)):
self.printbuttons[i].destroy()
self.printbuttons = []
self.button_attribs = []
self.logic.create_printer_button_attributes()
self.button_attribs = self.logic.get_printer_button_attributes()
for i in range(len(printer_store)):
self.printbuttons.append(Gtk.Button(None))
self.printerimg = Gtk.Image()
self.printerimg.set_from_file("pics/printer.png")
self.printbuttons[i].set_image(self.printerimg)
self.overlay.add_overlay(self.printbuttons[i])
self.printbuttons[i].set_name('printbuttons')
self.printbuttons[i].set_opacity(0.8)
self.printbuttons[i].set_tooltip_text(printer_store[i][0])
self.printbuttons[i].show()
for i in range(len(self.printbuttons)):
self.printbuttons[i].connect("enter", self.on_printer_image_entered)
self.printbuttons[i].connect("leave", self.on_printer_image_left)
self.printbuttons[i].connect("clicked", self.on_printer_button_clicked, printer_store[i])
# Quit Button
self.quit_button = Gtk.Button("Quit")
self.quit_button.show()
self.quit_button.set_tooltip_text("GET OUTTA HERE!!!")
def register_handlers(self):
self.department_combo.connect("changed", self.on_department_combo_changed)
self.building_combo.connect("changed", self.on_building_combo_changed)
self.floor_combo.connect("changed", self.on_floor_combo_changed)
self.printer_combo.connect("changed", self.on_printer_combo_changed)
self.update_button.connect('clicked', self.update_floor_plan)
self.install_button.connect('clicked', self.on_install_button_clicked)
self.uninstall_button.connect('clicked', self.on_uninstall_button_clicked)
self.help_button.connect('clicked', self.on_help_button_clicked)
self.quit_button.connect('clicked', self.destroy_handler)
self.main_window.connect('delete-event', self.delete_event_handler)
self.main_window.connect('destroy', self.destroy_handler)
return
def run(self):
Gtk.main()
# This method is called by the 'X' button on the window,
# or as a result of Gtk.main_quit()
def delete_event_handler(self, widget, event, data=None):
# return False to indicate that we agree with the window
# being deleted.
self.helpWindow.destroy_handler()
return False
# This method is called by the 'quit' button
def destroy_handler(self, widget, data=None):
self.helpWindow.destroy_handler()
Gtk.main_quit()
return
def get_child_position(self, overlay, widget, allocation):
for i in range(len(self.printbuttons)):
if widget == self.printbuttons[i]:
allocation.x = self.button_attribs[i][0]
allocation.y = self.button_attribs[i][1]
allocation.height = self.button_attribs[i][2]
allocation.width = self.button_attribs[i][3]
print "changed"
return True
def on_printer_image_entered(self, button, tip):
button.set_opacity(1)
button.set_tooltip_text(tip)
return
def on_printer_image_left(self, button):
button.set_opacity(0.8)
return
def on_printer_button_clicked(self, button, printer):
for i in range(len(self.printer_combo.get_model())):
if printer[0] == self.printer_combo.get_model()[i][0]:
active_printer = i
self.printer_combo.set_active(active_printer)
print printer[0]
return
Obviously this isn't my whole program, but it has all the important elements. The quit button comes up with a tool tip, but the print buttons do not. As I said, I suspect its because they're part of the overlay, but I really do not know. If anyone has any insight, it would be greatly appreciated!
I'm somewhat guessing here, but I think this is a (yet another) CSS problem. If you run this demo code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# untitled.py
#
# Copyright 2016 John Coppens <john@jcoppens.com>
#
# 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.
#
#
from gi.repository import Gtk
class MainWindow(Gtk.Window):
def __init__(self):
super(MainWindow, self).__init__()
self.connect("destroy", lambda x: Gtk.main_quit())
self.set_default_size(200, 200)
overlay = Gtk.Overlay()
(yet another) textview = Gtk.TextView()
textview.set_wrap_mode(Gtk.WrapMode.WORD_CHAR)
textbuffer = textview.get_buffer()
textbuffer.set_text("Welcome to the PyGObject Tutorial\n\nThis guide aims to provide an introduction to using Python and GTK+.\n\nIt includes many sample code files and exercises for building your knowledge of the language.", -1)
overlay.add(textview)
button = Gtk.Button(label="Overlayed Button")
button.set_property("tooltip-text", "Test tooltip")
#button.connect("enter-notify-event", self.on_overlay_btn_entered)
button.set_valign(Gtk.Align.CENTER)
button.set_halign(Gtk.Align.CENTER)
overlay.add_overlay(button)
self.add(overlay)
overlay.show_all()
self.show_all()
def on_overlay_btn_entered(self, btn, event):
print("Overlay button entered")
return True
def run(self):
Gtk.main()
def main(args):
mainwdw = MainWindow()
mainwdw.run()
return 0
Then it seems, at first sight, that the tooltip doesn't work. But carefully position the cursor on the (1px wide) border of the box, and the tooltip does appear. With the initial popup delay of the tooltip, you've probably just never seen it.
I suspect that no CSS is defined for the color/background of the overlaid tooltip.
Here's a screenshot for the tooltip that appeared: