Principiante in cerca di spiegazione!

A

Augusto

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
 
J

James Henderson

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
 
P

Peter Otten

Augusto said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top