PDA

View Full Version : [SOLVED] My Python/glade GUI isn't working



NovaAesa
January 11th, 2008, 08:44 AM
I've been learning Python, and decided that it's time I start messing around with GUIs. I've been following the instructions in a text book I have on Python, but they don't seen to be working. Here's the file I'm trying to run:


import gtk.glade

class SingleGUI(object):
def __init__(self):
self.window = gtk.glade.XML("OneButtonGUI.glade", "window1")

if __name__ == '__main__':
SingleGUI()
gtk.main()

And OneButtonGUI.glade is attached. You will have to change the file extension to .glade because the forum wouldn't let me upload a .glade file.

When I run the programme, nothing happens at all. So does anyone know what I am doing wrong?

af20001
January 11th, 2008, 12:25 PM
Do you need to import pygtk?

NovaAesa
January 11th, 2008, 01:35 PM
I tried this, but it still didn't work. Thx for your reply though.


import pygtk
import gtk.glade

class SingleGUI(object):
def __init__(self):
self.window = gtk.glade.XML("OneButtonGUI.glade", "window1")

if __name__ == '__main__':
SingleGUI()
gtk.main()

bobbocanfly
January 11th, 2008, 02:20 PM
if __name__ == '__main__':
SingleGUI()
gtk.main()

I think that is what is causing your problems as to me it doesnt look like the class is being called properly. I dont know much (read anything) about pyGTK but you could give this a try:


if __name__ == '__main__':
gui = SingleGUI()
gtk.main()[/PHP

NovaAesa
January 11th, 2008, 02:28 PM
Nope, that didn't work either.

af20001
January 11th, 2008, 03:06 PM
Do you get any errors written to the terminal?

NovaAesa
January 11th, 2008, 03:08 PM
No errors, it just goes to the next line, which is completely blank. I then have to press ctrl-c to stop the programme.

Wybiral
January 11th, 2008, 03:36 PM
Try adding "window.show_all()" at the end of your "__init__" method.

NovaAesa
January 11th, 2008, 03:41 PM
If I add window.show_all() I get a NameError: global name 'window' is not defined
and if I add self.window.show_all() I get an AttributeError: 'glade.XML' object has no attribute 'show_all'

af20001
January 11th, 2008, 03:43 PM
Have you got

#!/usr/bin/env python

at the top of your script?

Other than that the only other suggestion I have is follow the example here (http://216.239.59.104/search?q=cache:f0W3V10eoN0J:www.learningpython.com/2006/05/07/creating-a-gui-using-pygtk-and-glade/+pygtk+glade+example&hl=en&ct=clnk&cd=1&gl=uk&client=firefox-a).

Wybiral
January 11th, 2008, 03:46 PM
If I add window.show_all() I get a NameError: global name 'window' is not defined
and if I add self.window.show_all() I get an AttributeError: 'glade.XML' object has no attribute 'show_all'

Hmm, I've never used pythons glade module, I'm used to manually constructing them (where the "window" label would have been the actual "window" widget). Maybe glade stores the window widget somewhere that needs made visible?

NovaAesa
January 11th, 2008, 04:06 PM
Okay, I know what's gone wrong. In glade, I didn't set visible to Yes under Common for window1. I saw info about that in the guide that was linked to a few posts back.

Thanks everyone for your help and patients!