Hubertrum
Posts: 29
Joined: Tue May 22, 2012 11:56 am

Python not talking to .glade

Sat Oct 06, 2012 6:09 pm

I have created my first .glade object. When I go into Python 3 to write the code behind the Glade object it doesn't recognise import gtk or import glade.

What do I need to write in my import statement? If it is gtk then do I need to download or configure something to make it work?

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Python not talking to .glade

Sat Oct 06, 2012 6:16 pm

@Hubertrum: Gtk bindings for Python 3 are done with the `gi` module. Here is the tutorial: http://python-gtk-3-tutorial.readthedoc ... index.html

It uses Python 2 so you may need to translate to Python 3 at some points.

Code: Select all

while not self.asleep():
    sheep += 1

Hubertrum
Posts: 29
Joined: Tue May 22, 2012 11:56 am

Re: Python not talking to .glade

Sun Oct 07, 2012 9:48 am

Maybe I need to start again. From the right place this time.

What I want to do is have a GUI front-end and Python code behind it. What is the preferred method of doing this on the RasPi? I have written a simple Adder screen in Glade. If Glade is not the best GUI on the RasPi then which is? I have read several threads on this and no firm conclusion seems to be made. Tkinter and Glade seem to be "old" solutions. Is QT the way to go? Does that work on Wheezy?

I have downloaded jhbuild. That won't work. I get command not found. I have downloaded pygtk. Now I am doing pyorbit. But most of these tar files are 4 years old.

It seems very difficult to do what , in essence, should be an easy thing to do.

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Python not talking to .glade

Sun Oct 07, 2012 10:24 am

@Hubertrum: Glade is not a GUI but a GUI designer for the Gtk GUI toolkit.

Asking for the best GUI toolkit is like asking for the best programming language — you will get different answers from different people about *their* preferences. That's why you can't find a discussion with *the* answer to that question. :-)

It is not a RasPi specific question because you can use all the GUI toolkits available from the Linux distribution you are using. Don't download tar files from the internet if there are packages in the package management of your Linux distribution! Python bindings for `Tk`, `Gtk`, and `Qt` should be there to install.

`Tk` is somewhat „old” and has not that many widget types „built in” as the other two. There is no widely used graphical GUI designer, but I never saw that as a negative thing. `Gtk` and `Qt` are actively developed and alive. There are `Glade` and `Qt Creator` for graphical GUI design.

Code: Select all

while not self.asleep():
    sheep += 1

Hubertrum
Posts: 29
Joined: Tue May 22, 2012 11:56 am

Re: Python not talking to .glade

Sun Oct 07, 2012 2:59 pm

Thanks Blackjack. Which command should I use to install GTK.. I thought it would be sudo apt-get install gtk. But that doesn't work.
WIll Qt work with the latest Raspbian Wheezy? For some reason I'm thinking that is only for Arch Linux distro. Again what would be the command to install that.

Hubertrum
Posts: 29
Joined: Tue May 22, 2012 11:56 am

Re: Python not talking to .glade

Sun Oct 07, 2012 3:33 pm

It seems to be release dependent. import gtk will not work in Python 3. But is fine in Python 2. I will switch to using 2!

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Python not talking to .glade

Sun Oct 07, 2012 4:39 pm

@Hubertrum: Even with Python 2 you should not ``import gtk`` anymore. The Python bindings are switching to the introspection API like shown in the tutorial I linked to earlier. The old bindings are not developed any longer. Package name is `python-gi`.

And of course Raspbian Wheezy has Qt packages (and Python bindings for them). Package `python-qt4` or `python-pyside`. Those are two different Python bindings for Qt4 with slightly different coverage and license terms. `python-qt4` is a little bit more complete.

Code: Select all

while not self.asleep():
    sheep += 1

Hubertrum
Posts: 29
Joined: Tue May 22, 2012 11:56 am

Re: Python not talking to .glade

Sun Oct 07, 2012 9:06 pm

@Blackjack - taking your advice and quitting on the gtk front. Now switched to Qt4.
In Python 2.7 I can "from PyQt4 import QtGui" OK. But same statement won't work in Python 3.2.

I seem to be steered towards Python 2.7. Although I think I would prefer to work in 3.2 as it's the newer version.

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Python not talking to .glade

Mon Oct 08, 2012 9:08 am

@Hubertrum: I never gave the advice to quit on the Gtk front‽

If you want to use Qt bindings for Python 3 you have to install them for Python 3. Package names are `python3-pyqt4` or `python3-pyside`.

Python 2 and 3 are both current. It will still take some time until 3 is as usable as 2 because there are still many third party packages not available for Python 3. The situation becomes better and better but as long as Python 2 is still the standard Python on most Linux distributions and at most web hosters I won't — and can't — switch. The few but incompatible changes in Python 3 don't justify the cost of missing modules IMHO. Too less gain for too much trouble.

Code: Select all

while not self.asleep():
    sheep += 1

Ph1lj2
Posts: 35
Joined: Sat Oct 20, 2012 9:21 pm

Re: Python not talking to .glade

Thu Nov 22, 2012 4:24 pm

Hi
I new to glade, I've downloaded it to the Py (rasbian - latest) I'm able to create GUI's - but I can NOT get to 'talk' to python - I'm trying to use the example code from the link below

http://python-gtk-3-tutorial.readthedoc ... index.html "Simple Example"

Code: Select all

#!/usr/bin/python
from gi.repository import Gtk

win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
When I attempt to run it I get

File "SE1.py", line 3
from gi.repository import Gtk
ImportError: No module named gi.repository


Any ideas ?

I'm using (or trying to) Glade 3.12.1 and python 2.7

Hope someone can help

Return to “Python”