【问题标题】:Python3: How can I find out what version of GTK+ I am using?Python3:如何找出我使用的 GTK+ 版本?
【发布时间】:2012-09-14 12:19:41
【问题描述】:

我需要找出我正在使用的 GTK+ 版本。问题是涉及的版本号似乎太多了,我不确定哪些是最相关的。

我的系统是 Ubuntu 12.04,标配有 libgtk2.0-0,但是当我安装 Python 3.2 开发环境时,我还安装了许多其他包来绑定到 GTK3。

大多数情况下,这一切都“正常工作”,但我想实现一些(在 devhelp 中)仅在 GTK 3.2 版中可用的东西。不用说,我问的原因是Python在API中找不到方法。

所以,现在我想知道我能做些什么(如果有的话),但首先我需要弄清楚我的系统到底有什么。

This question 似乎指向了正确的方向,但它已经过时了四年。有没有人有任何可以提供帮助的最新信息?

编辑:感谢@ptomato 和@Pablo 提供的有用答案。我现在的问题是如何理解出现的不同象形文字。 dpkg 输出给出(除其他外)以下内容

bob@bobStudio:~$ dpkg -l libgtk* | grep ^i
ii  libgtk-3-0                             3.4.2-0ubuntu0.4                        GTK+     graphical user interface library
ii  libgtk-3-bin                           3.4.2-0ubuntu0.4                           programs for the GTK+ graphical user interface library
ii  libgtk-3-common                        3.4.2-0ubuntu0.4                        common files for the GTK+ graphical user interface library
ii  libgtk-3-dev                           3.4.2-0ubuntu0.4                        development files for the GTK+ library
ii  libgtk-3-doc                           3.4.2-0ubuntu0.4                        documentation for the GTK+ graphical user interface library
[etc....]

在 Python3 shell 中我得到以下内容

>>> from gi.repository import Gtk
>>> Gtk.MAJOR_VERSION, Gtk.MINOR_VERSION, Gtk.MICRO_VERSION
(3, 4, 2)

如果我没看错(我不确定我是否正确),这意味着我使用的是 GTK+ 版本 3.4.2,但由于库中的编号,即libgtk-3-0,因此存在一些疑问。另外,如果我使用的是 3.4.2,为什么在 3.2 中标记为可用的方法不存在?

谁能解释一下不同数字的含义?

EDIT2:更具体地说,我正在调查的方法是Gtk.Grid().get_child_at()。来自 DevHelp GTK+ 手册,

gtk_grid_get_child_at ()

GtkWidget *         gtk_grid_get_child_at               (GtkGrid *grid,
                                                         gint left,
                                                         gint top);
Gets the child of grid whose area covers the grid cell whose upper left corner is at left, top.

grid :    a GtkGrid
left :    the left edge of the cell
top :     the top edge of the cell
Returns : the child at the given position, or NULL
Since 3.2

我尝试在当前项目中使用此方法,并在堆栈跟踪中收到以下消息;

    neighbour = self.parent.grid.get_child_at(x, y)
AttributeError: 'Grid' object has no attribute 'get_child_at'

但是,如果我使用的是 Gtk 3.4.2,并且该方法“自 3.2 以来”可用,那似乎没有多大意义。也许我在其他地方犯了错误?

这是一个说明错误的简短测试程序(参见标记为

from gi.repository import Gtk

window = Gtk.Window()
grid = Gtk.Grid()
window.add(grid)

# the callout method
def on_button_clicked(widget):
    origin = grid.get_child_at(0, 0) #<-------------
    if widget == origin:
        print('You clicked (0,0)')
    else:
        print('You clicked (1,0)')

# add a couple of widgets
button00 = Gtk.Button()
button10 = Gtk.Button()
button00.set_label('(0,0)')
button10.set_label('(1,0)')
grid.attach(button00, 0, 0, 1, 1)
grid.attach(button10, 1, 0, 1, 1)

# attach the callouts
button00.connect("clicked", on_button_clicked)
button10.connect("clicked", on_button_clicked)

# display the window
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()

【问题讨论】:

    标签: python-3.x ubuntu-12.04 gtk3


    【解决方案1】:

    在 Python 提示符下键入以下内容或在脚本中执行等效操作:

    >>> from gi.repository import Gtk
    >>> Gtk.MAJOR_VERSION, Gtk.MINOR_VERSION, Gtk.MICRO_VERSION
    (3, 4, 3)
    

    此数字是您正在使用的 GTK 的实际版本号。 libgtk-3-0 包表示该包是针对 3.0 系列的,后续每个主版本号为 3 的版本都兼容。如果他们将包重命名为 3.2 等,那么您将无法升级它,并且您的所有其他包都会损坏。

    至于Gtk.Grid.get_child_at(),它在我使用 Gtk 3.4.2 的 Python 设置中也不可用。这很奇怪,因为 Python API 是从 C API 自动生成的。如果您遇到这样的问题,那么首先要做的是仔细检查您的程序中是否存在其他错误:

    >>> from gi.repository import Gtk
    >>> grid = Gtk.Grid()
    >>> 'get_child_at' in dir(grid)
    False
    

    所以真的没有办法。有时方法在文档 cmets 中被标记为 (skip),这意味着它们是仅限 C 语言的,不应出现在其他语言的绑定中;所以也检查一下。 GTK源码在线here;搜索get_child_at,您会看到没有(skip) 注释。所以这也不是问题。

    接下来要做的是检查 PyGObject 源代码是否有覆盖。有时某些方法会被更 Pythonic 的东西所取代;例如,grid[0, 0] 将比 grid.get_child_at(0, 0) 更加 Pythonic,如果 PyGObject 团队能想到这一点,那就太棒了。

    >>> grid[0, 0]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'Grid' object has no attribute '__getitem__'
    

    没有这样的运气。所以查看 PyGObject 的 GTK 覆盖 here 并搜索 get_child_atGrid 等。没有提及。

    所以我能想到的唯一可能就是一个错误。转到http://bugzilla.gnome.org 并报告 Python 绑定中缺少此方法。如果您有雄心壮志,何不也向他们推荐 grid[0, 0] 符号?

    【讨论】:

    • 我想这就是我最初想要的,谢谢!我现在的问题是理解不同的数字! (请参阅对原始问题的编辑)。
    • 我已经添加了一些解释。但现在真正的问题是为什么你的 3.2 方法不可用。如果你不告诉我们方法是什么,没人能回答。
    • 感谢您的澄清。我已经在主要问题中详细说明了我遇到问题的方法(参见 EDIT2)。
    • 好的,我已经添加了更多解释。长话短说,我认为这可能是一个错误。
    • 感谢您为这个番茄所做的所有工作。我认为您可能是对的,这种方法似乎在 Python 绑定中被忽略了,或者有一个未记录的替代方法。
    【解决方案2】:

    打开您喜欢的 shell 并使用当前安装的 gtk 名称进行操作

     rpm -q gtk2
     rpm -ql --info gtk2-2.8.20-1 |less
    

    或仔细查看此页面。 http://forums.fedoraforum.org/showthread.php?t=119358

    我认为这应该适用于 debian

     dpkg -l | grep libgtk
    

    更多信息可以在这里找到http://manpages.ubuntu.com/manpages/lucid/man1/dpkg.1.html

    【讨论】:

    • 谢谢,Pablo,但 Ubuntu 是基于 Debian,所以它使用 aptitude/dpkg,而不是 rpm。你知道 dpkg 等价物吗?
    • 哦,我忘了。给我一秒钟
    • 谢谢,好像可以了。通过 grep 管道为我提供了所有与 libgtk 相关的软件包的列表。我还发现这个命令很有用:dpkg -l libgtk* | grep ^i(这是我问题中链接的更新变体)
    猜你喜欢
    • 1970-01-01
    • 2016-09-23
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 2010-10-07
    相关资源
    最近更新 更多