Python3: How can I find out what version of GTK+ I

2019-07-28 18:58发布

问题:

I need to find out what version of GTK+ I am using. The problem is there seem to be so many different version numbers involved I am not sure which ones are most relevant.

My system is Ubuntu 12.04 and comes as standard with libgtk2.0-0, but when I installed my Python 3.2 development environment I also installed a number of other packages to bind to GTK3.

Most of the time this all 'just works', but I want to implement something that (in devhelp) is only available in Version 3.2 of GTK. Needless to say, my reason for asking is that Python cannot find the method in the API.

So, now I am wondering what (if anything) I can do about it, but first I need to find out exactly what I have got on my system.

This question seems to point in the right direction, but it is four years out of date. Does anyone have any more recent information that could help?

EDIT: Thanks to @ptomato and @Pablo for their helpful answers. My problem now is how to make sense of the different hieroglyphics that come out. The dpkg output gives (among other things) the following

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....]

while in the Python3 shell I get the following

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

If I read this correctly (I am not sure I am) this means I am using GTK+ version 3.4.2, but there is some doubt because of the number on the libraries, viz, libgtk-3-0. Also, if I am using 3.4.2, why is a method labelled as being available in 3.2 not present?

Can someone explain what the different numbers mean?

EDIT2: To be more specific, the method I am investigating is Gtk.Grid().get_child_at(). From the DevHelp GTK+ manual,

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

I tried using this method in my current project and I get the following message in the stack trace;

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

But if I am using Gtk 3.4.2, and the method was available 'since 3.2' that does not seem to make much sense. Maybe I made a mistake elsewhere?

Here is a short test program that illustrates the error (see the line marked <--------)

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()

回答1:

Type the following at your Python prompt or do the equivalent in a script:

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

This number is the actual version number of GTK that you are using. The libgtk-3-0 package indicates that that package is for the 3.0 series, which every subsequent version with a major version number of 3 is compatible with. If they renamed the package for 3.2, etc., then you wouldn't be able to upgrade it and all your other packages would break.

As for Gtk.Grid.get_child_at(), it's not available in my Python setup with Gtk 3.4.2 either. This is odd, since the Python API is automatically generated from the C API. If you are facing a problem like this, then the first thing to do is double-check that there's not a different error in your program:

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

So there really is no method. Sometimes methods are marked as (skip) in the documentation comments, meaning they are C-only and shouldn't show up in bindings in other languages; so check that too. The GTK source code is online here; search for get_child_at and you'll see there is no (skip) annotation. So that's not the problem either.

The next thing to do is to check the PyGObject source code for an override. Sometimes certain methods are superseded by something that's more Pythonic; for example, grid[0, 0] would be much more Pythonic than grid.get_child_at(0, 0) and it would be awesome if the PyGObject team had thought of that.

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

No such luck. So look at PyGObject's GTK overrides here and search for get_child_at, Grid, etc. No mention.

So the only possibility remaining that I can think of, is a bug. Go to http://bugzilla.gnome.org and report the lack of this method in the Python bindings. If you're feeling ambitious, why not suggest the grid[0, 0] notation to them as well?



回答2:

Open your preferred shell and do with the gtk name that is currently installed

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

or look closer at this page. http://forums.fedoraforum.org/showthread.php?t=119358

This should work for debian I think

 dpkg -l | grep libgtk

more info ca be found here http://manpages.ubuntu.com/manpages/lucid/man1/dpkg.1.html