Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Python - expected an indented block
Forum - Python - expected an indented block

Avatar
Umberto (Member)
Pro


Messaggi: 156
Iscritto: 27/09/2011

Segnala al moderatore
Postato alle 22:21
Domenica, 10/06/2012
Quando tento di eseguire questo programma
Codice sorgente - presumibilmente Python

  1. from Tkinter import *
  2. import sys
  3.  
  4. class Finestra(Tk):
  5.         def __init__(self):
  6.                 """Costruttore della classe Finestra
  7.                 """
  8.                 iargv = 1
  9.                 Tk.__init__(self)
  10.                 self.title(sys.argv[iargv])
  11.                 self.geometry("%dx%d" % (220, 150))
  12.                 iargv = iargv +1
  13.                 if sys.argv[iargv] == "button":
  14.                 # creo il bottone
  15.                 b = Button(self, text="hello", command=self.helloWorld)
  16.                 b.place(x=15, y=15)
  17.                 break
  18.                 # registro l'evento di chiusura della finestra         
  19.                 self.protocol('WM_DELETE_WINDOW', self.__chiudi)
  20.                
  21.         def mostra(self):
  22.                 """Visualizza la finestra
  23.                 """
  24.                 # mando in loop l'applicazione
  25.                 self.mainloop()
  26.  
  27.         def helloWorld(self):
  28.                 """Stampa sullo standard output la stringa 'Hello world !'
  29.                 """
  30.                 print 'Ciao mondo !'
  31.  
  32.         def __chiudi(self):
  33.                 """Chiude l'applicazione alla chiusura della finestra
  34.                 """
  35.                 self.destroy()
  36.                
  37.                
  38. if __name__ == '__main__':
  39.         f = Finestra()
  40.         f.mostra()


Succede così:
C:\Users\umberto>C:\Python27\python.exe C:\Users\umberto\Documents\gui.py hello
button
  File "C:\Users\umberto\Documents\gui.py", line 15
    b = Button(self, text="button", command=self.helloWorld)
    ^
IndentationError: expected an indented block

Ultima modifica effettuata da Umberto il 10/06/2012 alle 22:21
PM Quote
Avatar
freenet (Member)
Newbie


Messaggi: 20
Iscritto: 02/05/2012

Segnala al moderatore
Postato alle 9:45
Lunedì, 11/06/2012
Perché credo che le righe che vanno dalla 15 alla 17 fanno parte del blocco if sulla riga 13 e quindi l'interprete si aspetta che indenti quelle righe.
Così dovrebbe andare
Codice sorgente - presumibilmente Python

  1. from Tkinter import *
  2. import sys
  3.  
  4. class Finestra(Tk):
  5.         def __init__(self):
  6.                 """Costruttore della classe Finestra
  7.                """
  8.                 iargv = 1
  9.                 Tk.__init__(self)
  10.                 self.title(sys.argv[iargv])
  11.                 self.geometry("%dx%d" % (220, 150))
  12.                 iargv = iargv +1
  13.                 if sys.argv[iargv] == "button":
  14.                         # creo il bottone
  15.                         b = Button(self, text="hello", command=self.helloWorld)
  16.                         b.place(x=15, y=15)
  17.                         break
  18.                 # registro l'evento di chiusura della finestra        
  19.                 self.protocol('WM_DELETE_WINDOW', self.__chiudi)
  20.                
  21.         def mostra(self):
  22.                 """Visualizza la finestra
  23.                """
  24.                 # mando in loop l'applicazione
  25.                 self.mainloop()
  26.  
  27.         def helloWorld(self):
  28.                 """Stampa sullo standard output la stringa 'Hello world !'
  29.                """
  30.                 print 'Ciao mondo !'
  31.  
  32.         def __chiudi(self):
  33.                 """Chiude l'applicazione alla chiusura della finestra
  34.                """
  35.                 self.destroy()
  36.                
  37.                
  38. if __name__ == '__main__':
  39.         f = Finestra()
  40.         f.mostra()


Ultima modifica effettuata da freenet il 11/06/2012 alle 9:46
PM Quote
Avatar
Umberto (Member)
Pro


Messaggi: 156
Iscritto: 27/09/2011

Segnala al moderatore
Postato alle 12:55
Lunedì, 11/06/2012
Mi dice break outside the loop

Ultima modifica effettuata da Umberto il 11/06/2012 alle 13:05
PM Quote
Avatar
Umberto (Member)
Pro


Messaggi: 156
Iscritto: 27/09/2011

Segnala al moderatore
Postato alle 13:13
Lunedì, 11/06/2012
Ho risolto non ci vuole il break

Codice sorgente - presumibilmente Python

  1. from Tkinter import *
  2.     import sys
  3.      
  4.     class Finestra(Tk):
  5.             def __init__(self):
  6.                     """Costruttore della classe Finestra
  7.                   """
  8.                     iargv = 1
  9.                     Tk.__init__(self)
  10.                     self.title(sys.argv[iargv])
  11.                     self.geometry("%dx%d" % (220, 150))
  12.                     iargv = iargv +1
  13.                     if sys.argv[iargv] == "button":
  14.                             # creo il bottone
  15.                             b = Button(self, text="hello", command=self.helloWorld)
  16.                             b.place(x=15, y=15)
  17.                     # registro l'evento di chiusura della finestra        
  18.                     self.protocol('WM_DELETE_WINDOW', self.__chiudi)
  19.                    
  20.             def mostra(self):
  21.                     """Visualizza la finestra
  22.                   """
  23.                     # mando in loop l'applicazione
  24.                     self.mainloop()
  25.      
  26.             def helloWorld(self):
  27.                     """Stampa sullo standard output la stringa 'Hello world !'
  28.                   """
  29.                     print 'Ciao mondo !'
  30.      
  31.             def __chiudi(self):
  32.                     """Chiude l'applicazione alla chiusura della finestra
  33.                   """
  34.                     self.destroy()
  35.                    
  36.                    
  37.     if __name__ == '__main__':
  38.             f = Finestra()
  39.             f.mostra()


Ultima modifica effettuata da Umberto il 11/06/2012 alle 13:55
PM Quote