You are not logged in.

#1 2011-05-21 09:16:47

martvefun
Member
From: The Internets
Registered: 2009-12-19
Posts: 177

Python3 and Gtk3

Hello,

I would like to program a small utility for gnome3 (so using  gtk3). As python is the language I know the best, I wanted to use it and as I'm on arch, I wanted to use py3k.

On the pyGTK's page, I read

Attention: PyGTK only supports the Gtk-2.X API (NOT Gtk-3.0) and the information on this page may be outdated. We strongly recommend not using PyGTK for new projects and to port existing applications from PyGTK to PyGObject. For more information see the PyGObject documentation.

Ok never heard of PyGObject. I understood it works with both python2 and 3.

It seems I've it already installed but seeing the version number, it looks like gnome2

$ yaourt -Qs pygobject
extra/pygobject 2.28.4-1
    Python 2 bindings for GObject
extra/pygobject-devel 2.28.4-1
    Development files for the pygobject bindings

Should I install the git version ?

I had a look on the pyobject git repository but I haven't see something for both python 3 and gtk3.
Is there any nice doc about this module somewhere ?

Any idea ?

Thank you

Last edited by martvefun (2011-05-21 09:17:35)


English is not my native language, sorry for the mistakes
Arch amd64, GNOME, Thinkpad

Offline

#2 2011-05-21 09:23:00

wonder
Developer
From: Bucharest, Romania
Registered: 2006-07-05
Posts: 5,941
Website

Re: Python3 and Gtk3

py3gobject is pygobject with python 3 support. the idea is that now you are using gboject-introspection to use methods from gtk3 and from any other library/application that support it.

pacman -Ql gtk3 | grep gir

Last edited by wonder (2011-05-21 09:24:20)


Give what you have. To someone, it may be better than you dare to think.

Offline

#3 2011-05-21 10:17:22

martvefun
Member
From: The Internets
Registered: 2009-12-19
Posts: 177

Re: Python3 and Gtk3

Thank you
I've found this page giving a small example on how it works and did a quick hello world.

Can I use the full GTK documentation or is there something better ?

$ pacman -Ql gtk3 | grep gir
gtk3 /usr/lib/girepository-1.0/
gtk3 /usr/lib/girepository-1.0/Gdk-3.0.typelib
gtk3 /usr/lib/girepository-1.0/GdkX11-3.0.typelib
gtk3 /usr/lib/girepository-1.0/Gtk-3.0.typelib
gtk3 /usr/share/gir-1.0/
gtk3 /usr/share/gir-1.0/Gdk-3.0.gir
gtk3 /usr/share/gir-1.0/GdkX11-3.0.gir
gtk3 /usr/share/gir-1.0/Gtk-3.0.gir

Last edited by martvefun (2011-05-21 10:28:03)


English is not my native language, sorry for the mistakes
Arch amd64, GNOME, Thinkpad

Offline

#4 2011-05-24 13:59:41

spiritwalker
Member
From: USA
Registered: 2010-07-15
Posts: 85

Re: Python3 and Gtk3

I think the link you have for GTK documentation is for version 2. You may want to use http://developer.gnome.org/gtk3/3.1/

Last edited by spiritwalker (2011-05-24 13:59:54)

Offline

#5 2011-05-24 14:04:21

martvefun
Member
From: The Internets
Registered: 2009-12-19
Posts: 177

Re: Python3 and Gtk3

Thank you, it'll works better with this one !


English is not my native language, sorry for the mistakes
Arch amd64, GNOME, Thinkpad

Offline

#6 2011-05-24 14:12:02

wonder
Developer
From: Bucharest, Romania
Registered: 2006-07-05
Posts: 5,941
Website

Re: Python3 and Gtk3

spiritwalker wrote:

I think the link you have for GTK documentation is for version 2. You may want to use http://developer.gnome.org/gtk3/3.1/

actually that's the devel version, you want 3.0 a.k.a stable


Give what you have. To someone, it may be better than you dare to think.

Offline

#7 2011-05-24 14:50:14

spiritwalker
Member
From: USA
Registered: 2010-07-15
Posts: 85

Re: Python3 and Gtk3

wonder, you're probably right.
martvefun, if you want to view the docs offline, install devhelp from extra and gtk3-docs from aur.

Offline

#8 2011-05-26 20:17:05

fzzbt
Member
Registered: 2011-02-19
Posts: 17

Re: Python3 and Gtk3

Do  you guys have gi available in  python3?
I can't import it even after installing the pygobject packages.
this example only works with python2 http://paste.pocoo.org/show/392618/

i talked with a gtk developer who said it should  work.

is this a bug in arch?

Last edited by fzzbt (2011-05-26 20:29:23)

Offline

#9 2011-05-26 20:31:35

wonder
Developer
From: Bucharest, Romania
Registered: 2006-07-05
Posts: 5,941
Website

Re: Python3 and Gtk3

pygobject is python2
py3gobject is python 3


Give what you have. To someone, it may be better than you dare to think.

Offline

#10 2011-06-29 15:35:15

MaTachi
Member
Registered: 2011-06-07
Posts: 52

Re: Python3 and Gtk3

I'm pretty new to Python and I'm trying to learn Python 3. But I can't figure out how I shall get GTK 3 to work. You guys have written a lot about this above, but I don't really understand what I should do. ^^ What am I supposed to download, and is there an easy tutorial or something out there?

Offline

#11 2011-07-06 07:27:18

Padfoot
Member
Registered: 2010-09-03
Posts: 381

Re: Python3 and Gtk3

What you are looking for is:

from gi.repository import Gtk, GdkPixbuf, Gdk

My online references are http://docs.python.org/py3k/ and http://www.gtk.org/documentation.php

I recommend doing the following:

pacman -S anjuta glade

Glade is a Gtk+ wysiwyg designer with Gtk+ 3 support, and Anjuta is a multi language IDE, which actually integrates Glade.

Anjuta is handy as all your makefiles etc. required for building your app from source are auto generated. You can include version control and even required dependencies. Ideal for creating a package for uploading to AUR. All you need to do is create a pkgbuild.

Cheers.

Offline

#12 2011-07-18 17:16:33

MaTachi
Member
Registered: 2011-06-07
Posts: 52

Re: Python3 and Gtk3

Ah, thank you very much.

But I don't really understand the http://www.gtk.org/documentation.php page. Am I supposed to visit the page for the GTK library? It sends me to the "GTK+ 3 Reference Manual", but the examples aren't coded in Python, right? It's so confusing. ^^

Anyway, Anjuta looks pretty neat. I suppose that I only need to work in it and not in Glade since it is already integrated in Anjuta? But I think that I need to find a tutorial about Anjuta, since I'm not very familiar with all files it auto creates in the project folder. But yeah, it looks really handy and simple.

Offline

#13 2011-07-19 08:23:28

Padfoot
Member
Registered: 2010-09-03
Posts: 381

Re: Python3 and Gtk3

Have a look at http://www.anjuta.org/ the Ajunta project page. There is documentation there and I think you may even find some tutorials.

For Gtk you could still refer to http://www.pygtk.org/ but this is Gtk 2/Python2, so use as a general guide only, as the Gtk api has changed.

Using theGtk 3 docs page is better and you just have to convert to python, which isn't too difficult. For example:

gtk_window_new () creates a new window object. In python, assuming you have imported gtk as Gtk, you would use Gtk.window_new(). So you generally replace the "gtk_" with "Gtk.".

Hope this helps.

Cheers.

Offline

#14 2011-07-22 15:55:03

Camus
Member
Registered: 2011-07-03
Posts: 71

Re: Python3 and Gtk3

I myself have a question about py(3)gobject. I managed to get left-click and right-click StatusIcon menu working, but the menu doesn't appear below the Gnome 3 panel (I'm using fallback mode), like it does with other applets (e.g. sound icon); it appears at the point of the click, which makes it look a bit awkward (the menu covers part of the icon). Any way to change this behavior? Right-click code:

        self.icon.connect('popup-menu', self.rightclick)
...
    def rightclick(self, icon, button, time):
        menu = Gtk.Menu()
        menuQuit = Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_QUIT, None)
        menuQuit.connect('activate', lambda q: Gtk.main_quit())
        menu.append(menuQuit)
        menu.show_all()
        menu.popup(None, None, Gtk.StatusIcon.position_menu, None, button, time)

Offline

#15 2011-07-22 21:49:22

Padfoot
Member
Registered: 2010-09-03
Posts: 381

Re: Python3 and Gtk3

Camus wrote:

I myself have a question about py(3)gobject. I managed to get left-click and right-click StatusIcon menu working, but the menu doesn't appear below the Gnome 3 panel (I'm using fallback mode), like it does with other applets (e.g. sound icon); it appears at the point of the click, which makes it look a bit awkward (the menu covers part of the icon). Any way to change this behavior? Right-click code:

        self.icon.connect('popup-menu', self.rightclick)
...
    def rightclick(self, icon, button, time):
        menu = Gtk.Menu()
        menuQuit = Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_QUIT, None)
        menuQuit.connect('activate', lambda q: Gtk.main_quit())
        menu.append(menuQuit)
        menu.show_all()
        menu.popup(None, None, Gtk.StatusIcon.position_menu, None, button, time)

You are calling an invalid function, have an extra parameter, plus you need to add the icon to the end of the parameters so it will be passed to the positioning function.

Try this:

        self.icon.connect('popup-menu', self.rightclick)
...
    def rightclick(self, icon, button, time):
        menu = Gtk.Menu()
        menuQuit = Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_QUIT, None)
        menuQuit.connect('activate', lambda q: Gtk.main_quit())
        menu.append(menuQuit)
        menu.show_all()
        menu.popup(None, None, Gtk.status_icon_position_menu, button, time, self.icon)

Cheers.

Last edited by Padfoot (2011-07-23 06:47:54)

Offline

#16 2011-07-23 10:02:12

Camus
Member
Registered: 2011-07-03
Posts: 71

Re: Python3 and Gtk3

I think that's the same thing but for old PyGtk. Based on the outputted errors, I did this to stop them and make the menu work properly:

py3gobject:

    def rightclick(self, icon, button, time):
        menu = Gtk.Menu()
        menuQuit = Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_QUIT, None)
        menuQuit.connect('activate', lambda q: self.quit())
        menu.append(menuQuit)
        menu.show_all()
        menu.popup(None, None, self.position_menuFIX(menu), None, button, time)
    
    def position_menuFIX(self, menu):
        def func(a, b, c): # 3 dummy variables to stop the errors in py(3)gobject
            return self.icon.position_menu(menu, self.icon)
        return func

PyGtk:

    def rightclick(self, icon, button, time):
        menu = gtk.Menu()
        menuQuit = gtk.MenuItem('Quit')
        menuQuit.connect('activate', lambda q: self.quit())
        menu.append(menuQuit)
        menu.show_all()
        menu.popup(None, None, self.position_menuFIX(menu), button, time)
    
    def position_menuFIX(self, menu):
        def func(a): # 1 dummy variable in PyGtk
            return gtk.status_icon_position_menu(menu, self.icon)
        return func

Also, if anyone's wondering how to make statusicon menu for leftclick work, here's how I did in PyGtk (the issue was how to get proper event time format):

...
        self.icon.connect('activate', self.leftclick)
...
    def leftclick(self, icon):
        button = 1
        time = gtk.get_current_event_time()
        menu = gtk.Menu()
        menuQuit = gtk.MenuItem('Quit')
        menuQuit.connect('activate', lambda q: self.quit())
        menu.append(menuQuit)
        menu.show_all()
        menu.popup(None, None, self.position_menuFIX(menu), button, time)

Py(3)Gobject doesn't display status icon properly, it makes the icon area much wider than the icon itself, so there's a lot of space on both sides of the icon (which is accepting click events as if you were clicking directly on the icon).
Porting from PyGtk to Py3Gobject is really simple (I did vice-versa because of the issue with the status icon). I also noticed that, at least in Gnome, you can tell in which Gtk version are apps developed; Gtk2 in Gnome has blue tooltips while Gtk3 tooltips are black.

Offline

#17 2011-07-23 21:52:38

Padfoot
Member
Registered: 2010-09-03
Posts: 381

Re: Python3 and Gtk3

@camus

My example above works in pygtk2 (at least it did for me with one of my apps I am preparing to port to python and gtk 3) without any errors.

I haven't tried it in 3 yet. I'll have to keep your solution in mind, but will certainly try the standard method above as (according to the docs) that part of the api has not changed.

I have never actually implemented that code before as on a gtk2 based panel/tray I never experienced problems with a popup menu on a status icon, however, in gnome 3 (I have added an extension so the status icons appear on the top panel rather than in the message tray) the menu will popup under the top panel unless I use the positioning code.

Interested to see how that behaves in a python/gtk 3 situation.

Cheers.

Offline

Board footer

Powered by FluxBB