Ver Mensaje Individual
  #16 (permalink)  
Antiguo 03/02/2010, 12:04
Avatar de razpeitia
razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
Respuesta: problemita con wx.EVT_PAINT

Leyendo un poquito la documentación, me tope con el método Update en wx.Panel
Código Python:
Ver original
  1. #coding: utf-8
  2. import wx
  3. from time import sleep
  4.  
  5. ID_GRILLA2 = 2
  6. ID_GRILLA3 = 3
  7. ID_GRILLA4 = 4
  8. ID_GRILLA5 = 5
  9. ID_GRILLA6 = 6
  10. ID_GRILLA7 = 7
  11.  
  12. ID_UP = 10
  13. ID_DOWN = 11
  14. ID_LEFT = 12
  15. ID_RIGHT = 13
  16. ID_START = 14
  17.  
  18. class MyFrame(wx.Frame):
  19.     def __init__(self, *args, **kargs):
  20.         wx.Frame.__init__(self, *args, **kargs)
  21.  
  22.         #Creamos un Panel dibujable y el MenuBar
  23.         self.Panel = DrawPanel(self)
  24.         menuBar = wx.MenuBar()
  25.        
  26.         #Boton para empezar
  27.         self.StartButton = wx.Button(self, label="start")
  28.  
  29.         #Instrucciones a seguir:
  30.         self.inst = [11, 13, 10]
  31.  
  32.         #El sizer
  33.         sizer = wx.BoxSizer(wx.VERTICAL)
  34.         sizer.Add(self.Panel, 1, wx.EXPAND, 0)
  35.         sizer.Add(self.StartButton)
  36.  
  37.         #Añadimos menus
  38.         filemenu = wx.Menu()
  39.         grillamenu = wx.Menu()
  40.  
  41.         #Añadimos elementos a esos menus
  42.         filemenu.Append(wx.ID_EXIT, "&Close", "Close the program")
  43.         for i in range(2, 7 + 1):
  44.             grillamenu.Append(i, "%dx%d"%(i, i), "Crear grilla %dx%d" % (i, i), wx.ITEM_RADIO)
  45.  
  46.         #Añadimos los menus a la barra de menus
  47.         menuBar.Append(filemenu, "&File")
  48.         menuBar.Append(grillamenu, "&Grill")
  49.  
  50.         #Por ultimo ajustamos la barra de menu
  51.         self.SetMenuBar(menuBar)
  52.  
  53.         #Ajutes del sizer
  54.         self.SetSizer(sizer)
  55.         self.Layout()
  56.  
  57.         #Linkeamos los eventos a funciones con su respectiva ID
  58.         self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)
  59.         for i in range(2, 7 +1):
  60.             self.Bind(wx.EVT_MENU, self.SET_ID, id=i)
  61.         #Boton de inicio
  62.         self.StartButton.Bind(wx.EVT_BUTTON, self.MOVE_IMAGE)
  63.  
  64.     def MOVE_IMAGE(self, event):
  65.         print "Move Image, Click in button start"
  66.         n = self.Panel.n
  67.         pos = self.Panel.pos
  68.         #10 -> UP
  69.         #11 -> DOWN
  70.         #12 -> LEFT
  71.         #13 -> RIGHT
  72.         for id in self.inst:
  73.             if id == 10 and pos[1] > 0:
  74.                 pos[1] -= 1
  75.             elif id == 11 and pos[1] < n-1:
  76.                 pos[1] += 1
  77.             elif id == 13 and pos[0] < n-1:
  78.                 pos[0] += 1
  79.             elif id == 12 and pos[0] > 0:
  80.                 pos[0] -= 1
  81.             sleep(1)
  82.             self.Panel.Refresh() #Con esto llamamos al evento OnPaint al terminar este evento
  83.             self.Panel.Update()
  84.  
  85.     def SET_ID(self, event):
  86.         print "SET_ID"
  87.         #Ajustamos el numero de cuadritos a dibujar
  88.         self.Panel.n = event.GetId()
  89.         self.Panel.Refresh() #Importante redibuja todo el frame
  90.    
  91.     def OnExit(self, event):
  92.         self.Close(True)
  93.  
  94. class DrawPanel(wx.Panel):
  95.     """Draw a line to a panel."""
  96.     def __init__(self, *args, **kwargs):
  97.         wx.Panel.__init__(self, *args, **kwargs)
  98.         self.Bind(wx.EVT_PAINT, self.OnPaint)
  99.         #Por defecto son 2
  100.         self.n = 2
  101.         self.pos = [0, 0]
  102.  
  103.     def OnPaint(self, event):
  104.         print "Painting"
  105.         dc = wx.PaintDC(self)
  106.         dc.SetPen(wx.Pen("BLACK", 4))
  107.         n = self.n #Acortamos el nombre
  108.         z = 30 #Tamaño de los cuadritos
  109.         for i in range(n):
  110.             for j in range(n):
  111.                 dc.DrawRectangle(i * z, j * z, z, z)
  112.         dc.SetPen(wx.Pen("GREEN", 4))
  113.         dc.DrawRectangle(self.pos[0] * z, self.pos[1] * z, z, z)
  114.  
  115. app = wx.PySimpleApp(False)
  116. frame = MyFrame(None, title="Draw on Panel!!!")
  117. frame.SetSizeHints(640, 480)
  118. frame.Show()
  119. app.MainLoop()

Como puedes tienes un arreglo llamado inst (que contiene las instrucciones, en clase MyFrame).