Gtk + 3工具提示未显示在按钮上(Gtk+ 3 tooltips not showing up on button)

编程入门 行业动态 更新时间:2024-10-28 11:22:27
Gtk + 3工具提示未显示在按钮上(Gtk+ 3 tooltips not showing up on button)

工具提示未显示在我的按钮上。 我应该清除它:它们显示在我的常规按钮上,但添加到我的覆盖对象的任何按钮都不会显示工具提示。

# 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

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

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!

最满意答案

我在这里猜测,但我认为这是一个(又一个)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。

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

工具提示图像

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:

Tooltip image

更多推荐

本文发布于:2023-07-26 19:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279964.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   提示   工具   Gtk   showing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!