我正在尝试学习pygtk并理解文档和教程中术语的含义。
根据文件
所以我得出结论,工具提示不适用于按钮。这似乎是错误的,下面的示例代码似乎证明它是错误的。所以我对这些术语的含义并不了解?上述陈述是否不正确?谁能解释我在这里缺少的东西。是不是方法get_has_window()没有回答与工具提示是否有效相同的问题?
#!/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()
答案 0 :(得分:2)
我不是Gtk内部的专家,所以我可能错了。我怀疑有一些混乱......
Gtk.Window是一个顶层窗口,由窗口管理器“管理”,显示最大化,关闭按钮和程序名称。
Gtk.gdk.window()(取决于Gdk / Gtk /语言,它有不同的名称)是一个适合Gdk的“窗口”,它提供了一个用于绘制小部件的区域,它为按钮按下和按键等事件提供了一个敏感区域。
Gtk.Label着名的没有gdk.window,并且无法捕获这样的事件(你必须在它后面加上一个Gtk.EventBox来解决它)。
据我所知(并且我找不到任何建议,否则),Gtk.Button 确实有一个Gdk.Window,按钮的外观画在那个窗口上(和当然它可以接收事件)。
显示工具提示是Gtk.Widget的任务,而Gtk.Label确实派生自Gtk.Widget(做Gtk.Button):
+-- gobject.GObject
+-- gtk.Object
+-- gtk.Widget
+-- gtk.Misc
+-- gtk.Label
+-- gobject.GObject
+-- gtk.Object
+-- gtk.Widget
+-- gtk.Container
+-- gtk.Bin
+-- gtk.Button
因此,两者都应该能够显示工具提示,如下面的程序所示:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# test_label_tooltip.py
#
# Copyright 2017 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.
#
#
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))
简而言之:显示工具提示的能力并不取决于窗口小部件是否具有Gdk.Window。事实上,工具提示可以落在顶级Gtk.Window之外,我怀疑它会表明它有自己的Gtk.Window或者由Window Manager管理(我真的应该调查一下):