Cominciare a programmare Python+libglade

Linguaggi di programmazione: php, perl, python, C, bash e tutti gli altri.
attorianzo
Scoppiettante Seguace
Scoppiettante Seguace
Messaggi: 351
Iscrizione: giovedì 23 novembre 2006, 23:21
Contatti:

Cominciare a programmare Python+libglade

Messaggio da attorianzo »

Ho visto che creare finestre con glade 3 non è difficile.. ma non riesco proprio a fare funzionare la finestra da me creata!

Ho creato il file prova.py:

Codice: Seleziona tutto

import gtk
import gtk.glade

def some_handler(widget):
    pass

xml = gtk.glade.XML('prova.glade')
widget = xml.get_widget('window1')
xml.autoconnect({
  'some_handler': some_handler
})
gtk.main()
Ed ecco prova.glade :

Codice: Seleziona tutto

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
<!--Generated with glade3 3.2.0 on Tue May 22 12:43:47 2007 by attorianzo@attorianzo-desktop-->
<glade-interface>
  <widget class="GtkWindow" id="window1">
    <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
    <child>
      <widget class="GtkLabel" id="label1">
        <property name="visible">True</property>
        <property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
        <property name="label" translatable="yes">Ciao magre</property>
      </widget>
    </child>
  </widget>
</glade-interface>
Su shell scrivo "python prova.py" e mi dà questo errore:

Codice: Seleziona tutto

attorianzo@attorianzo-desktop:~/Desktop$ python prova.py 
Traceback (most recent call last):
  File "prova.py", line 9, in <module>
    xml.autoconnect({
AttributeError: 'glade.XML' object has no attribute 'autoconnect'
attorianzo@attorianzo-desktop:~/Desktop$ 
Cosa sbaglio?
Avatar utente
DR astico
Prode Principiante
Messaggi: 168
Iscrizione: giovedì 26 ottobre 2006, 12:39

Re: Cominciare a programmare Python+libglade

Messaggio da DR astico »

ciao

sto usando python2.4.

dando

Codice: Seleziona tutto

 
import gtk
from gtk import glade
dir(glade.XML)
ottengo:

Codice: Seleziona tutto

>>> dir(glade.XML)
['__class__', '__cmp__', '__delattr__', '__dict__', '__doc__', '__gdoc__', '__getattribute__', '__gobject_init__', '__grefcount__', '__gtype__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', 'chain', 'connect', 'connect_after', 'connect_object', 'connect_object_after', 'disconnect', 'disconnect_by_func', 'emit', 'emit_stop_by_name', 'freeze_notify', 'get_data', 'get_property', 'get_widget', 'get_widget_prefix', 'handler_block', 'handler_block_by_func', 'handler_disconnect', 'handler_is_connected', 'handler_unblock', 'handler_unblock_by_func', 'notify', 'props', 'relative_file', 'set_data', 'set_property', 'signal_autoconnect', 'signal_connect', 'stop_emission', 'thaw_notify']
il metodo "autoconnect" parrebbe non esistere (non in questo modulo).
o ti manca qualche import o il metodo non esiste proprio.
il metodo "connect" vuole come argomenti:
1) Il nome del dell'evento che vuoi connettere all'handler
2) Il nome dell'handler,
3) i dati da passare all'handler (se ci sono).

i dati NON possono essere passati come dizionario. sto proprio vedendomi libglade e ancora non ho visto un simile passaggio di parametri.
che guida stai usando?
A volte è sufficiente leggere. ;)
attorianzo
Scoppiettante Seguace
Scoppiettante Seguace
Messaggi: 351
Iscrizione: giovedì 23 novembre 2006, 23:21
Contatti:

Re: Cominciare a programmare Python+libglade

Messaggio da attorianzo »

Ciao grazie per il tuo intervento.

Lo script l'ho copiato da qui:
http://www.jamesh.id.au/software/libglade/

Mentre il file .glade è una semplicissima finestra (con solo una scritta, niente bottoni) generata da glade 3...

Potresti suggerirmi un qualiasi script, anche supersemplice, basta che mi formi una finestra funzionante? :/
Avatar utente
DR astico
Prode Principiante
Messaggi: 168
Iscrizione: giovedì 26 ottobre 2006, 12:39

Re: Cominciare a programmare Python+libglade

Messaggio da DR astico »

Prego! non c'è di che!!
;D

questo è il primo esempio della guida ufficiale:

Codice: Seleziona tutto

#!/usr/bin/env python

# example helloworld.py

import pygtk
pygtk.require('2.0')
import gtk

class HelloWorld:

    # This is a callback function. The data arguments are ignored
    # in this example. More on callbacks below.
    def hello(self, widget, data=None):
        print "Hello World"
        
    def delete_event(self, widget, event, data=None):
        # If you return False in the "delete_event" signal handler,
        # GTK will emit the "destroy" signal. Returning TRUE means
        # you don't want the window to be destroyed.
        # This is useful for popping up 'are you sure you want to quit?'
        # type dialogs.
        print "delete event occurred!"
        
        # Change False to True and the main window will not be destroyed
        # with a "delete_event".
        return False
        
    # Another callback
    def destroy(self, widget, data=None):
        gtk.main_quit()
        
    def __init__(self):
        # Create a new window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        
        # When the window is given the "delete event" signal (this is given
        # by the window manager, usually by the "close" option, or on the
        # titlebar), we ask it to call the delete_event() function
        # as defined above. The data passed to the callback
        # function is NULL and is ignored in the callback function.
        self.window.connect("destroy", self.destroy)
        
        # Here we connect the "destroy" event to a signal handler.
        # This event occurs when we call gtk_widget_destroy() on the window,
        # or if we return FALSE in the "delete_event" callback.
        self.window.connect("destroy", self.destroy)
        
        # Sets the border width of the window.
        self.window.set_border_width(10)
        
        # Creates a new button with the label "Hello World".
        self.button=gtk.Button("Hello World")
        
        # When the button receives the "clicked" signal, it will call the
        # function hello() passing it None as its argument. the hello()
        # is defined above.
        self.button.connect("clicked", self.hello, None)
        
        # This will cause the windowto be destroyed by calling
        # gtk_widget_destroy(window) when "clicked". Again the destroy
        # signal could come from here, or the window manager.
        self.button.connect_object("clicked", gtk.Widget.destroy, self.window)
        
        # This packs the button  into the window (a GTK container).
        self.window.add(self.button)
        
        # The final step is to display this newly created widget.
        self.button.show()
        
        # And the window
        self.window.show()
        
    def main(self):
        # All pyGTK applications must have a gtk.main(). Control ends here
        # and waits for an event to occur (like a key press or mouse event).
        gtk.main()
        
# If the program srun directly or passed as an argument to the Python
# interpreter then create a HelloWorld instance and show it
if __name__ == "__main__":
    hello = HelloWorld()
    hello.main()
questo script fornisce pure un già valido esempio di utilizzo concreto dei segnali. La finestra che ne deriva (senza alcun file glade) ha un tasto che stampa "Hello World" sulla shell e poi chiude la finestra oppure stampa un messaggio diverso se la chiudi senza premere il bottone.

la guida completa (in inglese) lo trovi qui:
http://pygtk.org/tutorial.html

riferendomi al pdf, la guida sembra tutt'altro che "facile e veloce".... sono 412 pagine!!!
buona esercitazione!!

ti consiglierei di confrontarla direttamente con l'utilizzo delle librerie in una applicazione gtk reale scritta in python ( io sto usando il PROMOGEST (tutt'altro che semplice... sembra una giungla))

scaricabile anche in pdf o tarball di html.

così a prima vista direi che o è sbagliata la guida (magari il tipo non si intende di python) oppure si riferisce ad una vecchia versione delle librerie.
mah...
A volte è sufficiente leggere. ;)
Scrivi risposta

Ritorna a “Programmazione”

Chi c’è in linea

Visualizzano questa sezione: 0 utenti iscritti e 3 ospiti