Principiante in cerca di spiegazione!
James Henderson
james at logicalprogression.net
Wed Feb 11 12:05:28 EST 2004
On Wednesday 11 February 2004 4:43 pm, Augusto wrote:
> Ciao a tutti,
> premetto a tutti che stò imparando proprio adesso a studiare python ed
> ho il seguente problema ... questo script copiato pari pari da un
> tutorial mi ridà un'errore che non capisco.. questo è il codice:
>
> from Tkinter import * # importo il modulo
>
> # costruisco una mia classe che gestisce la finestra
> class Application(Frame):
>
> # metodo che scrive un messaggio a video
> def scrivi_messaggio(self):
> self.mess["text"] = "Ciao a tutti!",
>
> # metodo che pulisce il messaggio a video
> def cancella_messaggio(self):
> self.mess["text"] = "",
>
> # metodo costruttore che crea gli oggetti grafici
> def __init__(self, master=None):
> f = Frame(master)
> f.pack()
>
> # crea il bottone di uscita (di colore rosso)
> self.esci = Button(f)
> self.esci["text"] = "QUIT"
> self.esci["fg"] = "red"
> self.esci["command"] = f.quit
> self.esci.pack({"side": "left"})
>
> # crea il bottone che permette di scrivere il messaggio
> self.butt_mess = Button(f)
> self.butt_mess["text"] = "Scrivi",
> self.butt_mess["command"] = self.scrivi_messaggio
> self.butt_mess.pack({"side": "left"})
>
> # crea il bottone che permette di pulire il messaggio
> self.butt_canc_mess = Button(f)
> self.butt_canc_mess["text"] = "Cancella",
> self.butt_canc_mess["command"] = self.cancella_messaggio
> self.butt_canc_mess.pack({"side": "left"})
>
> # crea l'oggetto grafico che contiene il messaggio
> self.mess = Message(f)
> self.mess["text"] = "",
> self.mess.pack({"side": "left"})
>
>
> # corpo principale del programma
> finestra = Tk()
> app = Application(finestra)
> finestra.mainloop()
>
> e mi ritorna un'errore come:
>
> Traceback (most recent call last):
> File "C:/Python23/tests/tkinter.py", line 4, in -toplevel-
> class Application(Frame):
> File "C:/Python23/tests/tkinter.py", line 20, in Application
> self.esci = Button(f)
> NameError: name 'f' is not defined
>
> ?? Eppure la var f è definita nell' _INIT_
>
> qualcuno mi sà spiegare perchè?
>
> Grazie in anticipo!
>
> Augusto
f is indeed defined in __init__ and is therefore local to __init__. The name
is not available outside that method. I think that your problem may be that
the code following "f.pack()" to the end of the class definition is meant
also to be part of the __init__ function. Try indenting it all to be level
with "f.pack()".
James
--
James Henderson, Logical Progression Ltd.
http://www.logicalprogression.net/
http://sourceforge.net/projects/mailmanager/
More information about the Python-list
mailing list