Kent's suggestions are great, but I wanted to add two.
> Hello list.
>
> I'd really appreciate any comments, particulary regarding style
> corrections. I'm a newbie...
>
> Thanks!
> Ismael
>
>
> import random
> import tkMessageBox
> from Tkinter import *
>
> class GUI:
> def __init__(self):
> self._crearGUI()
>
> def _crearGUI(self):
> self.root = Tk()
> self.root.title("Lights")
> self.botones = [[0,0,0],[0,0,0],[0,0,0]]
> for i in range(3):
> for j in range(3):
> action = lambda n=i, m=j: self._accion(n,m)
> self.botones[i][j] = Button(self.root, height=5,
> width=10, command=action)
> self.botones[i][j].grid(row=i, column=j, padx= 4, pady=4)
>
> self._updateBotones()
> self.root.mainloop()
>
> def _accion(self, i, j):
> self._click(i, j)
> try:
> self._checkWin()
> except ValueError:
> tkMessageBox.showinfo(title = "You won!", message = "You
> rock, kid!")
>
> if True == tkMessageBox.askyesno(title="What shall we do?",
> message="Play another?"):
> self.root.destroy()
> lights.__init__() ## Is this... too ugly? Is there a
> beter way?
> self.__init__()
> else:
> self.root.destroy()
> def _updateBotones(self):
> for i in range(3):
> for j in range(3):
> if lights.tablero[i][j] == True:
> self.botones[i][j].configure(bg="red")
> else:
> self.botones[i][j].configure(bg="grey")
>
> def _click(self, i, j):
> lights.click(i,j)
> self._updateBotones()
>
> def _checkWin(self):
> conteo = 0
> for i in range(3):
> for j in range(3):
> if lights.tablero[i][j] == True: conteo +=1
if lights.tablero[i][j] == True:
can be simplified to
if lights.tablero[i][j]:
> if conteo == 9:
> print "GANO"
> raise ValueError
>
> return
>
> class Luces:
> def __init__(self):
> self.tablero = [[0,0,0],[0,0,0],[0,0,0]]
> self._hacerTablero()
>
> def _hacerTablero(self):
> for i in range(3):
> for j in range(3):
> self.tablero[i][j] = not random.randint(0,1)
Why use
not random.randint(0,1)?
If it's random, wouldn't just
random.randint(0,1)
give the same result?
Why get a random number and then immediately return the opposite?
It's like resolving to write down heads if you flip tails, and tails if you
flip heads when you flip a coin! Why bother?
> def _cambiarCelda(self, i, j):
> self.tablero[i][j] = not self.tablero[i][j]
>
> def _descartarNegativos(self, n):
> if n < 0:
> raise IndexError
> return n
>
> def click(self, i,j):
> self._cambiarCelda(i,j)
> try:
> self._cambiarCelda(self._descartarNegativos(i-1), j)
> except IndexError:
> pass
> try:
> self._cambiarCelda(self._descartarNegativos(i+1), j)
> except IndexError:
> pass
> try:
> self._cambiarCelda(i, self._descartarNegativos(j-1))
> except IndexError:
> pass
> try:
> self._cambiarCelda(i, self._descartarNegativos(j+1))
> except IndexError:
> pass
>
> if __name__ == '__main__':
> lights = Luces()
> lightsGUI = GUI()
>
> _______________________________________________
> Tutor maillist - [email protected]
> http://mail.python.org/mailman/listinfo/tutor
>
>
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor