Gtk + 3工具提示未显示在按钮上

时间:2016-06-22 22:21:37

标签: python-2.7 button tooltip pygtk gtk3

工具提示不会显示在我的按钮上。我应该清楚这一点:它们显示在我的常规按钮上,但添加到我的叠加层对象的任何按钮都不会显示工具提示。

# 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

显然这不是我的整个计划,但它具有所有重要的元素。退出按钮带有工具提示,但打印按钮没有。正如我所说,我怀疑它是因为它们是叠加层的一部分,但我真的不知道。如果有人有任何见解,我们将不胜感激!

1 个答案:

答案 0 :(得分:1)

我在这里猜测,但我认为这是一个(又一个)CSS问题。 如果您运行此演示代码:

#!/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

乍一看,似乎工具提示不起作用。但 小心地将光标放在框的(1px宽)边框上,并显示工具提示 。随着工具提示的初始弹出延迟,您可能从未见过它。

我怀疑没有为重叠工具提示的颜色/背景定义CSS。

以下是出现的工具提示的屏幕截图:

Tooltip image