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 3 1474
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.
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/
All right.. Thanks! Now it's ok..
Python rulez! bye! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: qwweeeit |
last post by:
Hi all,
Elliot Temple on the 1 June wrote:
> How do I make Python press a button on a webpage? I looked at
> urllib, but I only see how to open a URL with that. I searched
> google but no...
|
by: Roman Mashak |
last post by:
Hello, All!
I'm implementing simple CLI (flat model, no tree-style menu etc.). Command
line looks like this: <command> <param1> <param2> ... <paramN> (where
N=1..4)
And idea is pretty simple:
...
|
by: sara |
last post by:
Hi,
I have a little problem.
This is the code one of my page. My problem is that the function
"leggi1" is not executed. Where do I make a mistake?
<%@ Page Language="VB" AutoEventWireup="false"...
|
by: sara |
last post by:
Hi,
this is the code of a (2005) asp page. I've a problem during the
execution. When I click on "Leggi" button the javascript function
"leggi1" is not done! Why?
Someone can I helm me, please!
...
|
by: ash |
last post by:
hi friends,
i have some questions whch is in my last year question papers.i need
some help to get logic of these questions.
1) write a C function, that takes two strings as arguments and...
|
by: seba |
last post by:
Ciao a tutti,
vorrei sviluppare in C++ sotto Fedora Core 5. Ho dei problemi con
ECLIPSE, quindi chiedo se qualcuno puo' consigliarmi qualche buon tool
di sviluppo.
CIAO!
|
by: molcaleviATyahoogroupsDOTcom |
last post by:
B " H _
KO mafia ! Global Democracy ARTSENU & Fusione
Fredda = TRIVOLUZIONE W post OPEC ! ! !
e' SAUDITA - Bin Laden la cosi' detta '' mafia ebraica CFR
globalmafia ILLUMINATI '' ....
|
by: diaboliko80 |
last post by:
Salve a tutti.
Ho un problema con IE7.
Ho implementato una pagina .asp che tramite tecnologia AJAX mi crea una
serie di select nidificate (il classico esempio delle Regioni-Province
--scelgo...
|
by: molcaleviATyahoogroupsDOTcom |
last post by:
KO mafia CFR ILLUMINATI ! Global Democracy TRIVOLUZIONE ARTSENU
COLD FUSION W post OPEC !
B '' H _
KO mafia ! Global Democracy ARTSENU & Fusione
Fredda = TRIVOLUZIONE
W post OPEC ! ! !
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |