Principiante in cerca di spiegazione!

Peter Otten __peter__ at web.de
Wed Feb 11 12:32:59 EST 2004


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()

You may be mixing tabs and spaces. Try to use *only* spaces.
Every thing from here...
> 
>     # 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"})

... to here must be indented to the same level as the two first lines in 
the body of the __init__() method

> # 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

f is only known inside the body of __init__()

> 
> ?? Eppure la var f è definita nell' _INIT_
> 
> qualcuno mi sà spiegare perchè?
> 
> Grazie in anticipo!
> 
> Augusto

Remember: whitespace *is* significant in Python.

Peter

PS: Try to post in english, nobody will care for occasional mistakes.




More information about the Python-list mailing list