Ver Mensaje Individual
  #10 (permalink)  
Antiguo 19/01/2010, 13:26
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

Bueno la verdad es que corrí el código :p (usas imagenes y otros archivos como main.py, así que no lo pude correr)

Cita:
Iniciado por IamEdo
puedo usar un mismo botón para llamar a 2 funciones diferentes en clases diferentes????
Si, si puedes.
Código Python:
Ver original
  1. class DrawPanel(wx.Panel):
  2.     def __init__(self, *args, **kwargs):
  3.         #Constructor de DrawPanel
  4.         self.pos = (0, 0) #Posición inicial de la imagen en la matriz.
  5.         #En el método OnPaint de la clase DrawPanel dibuja la imagen de acuerdo al contenido de la variable self.pos
  6.         ...
  7.  
  8. class MyFrame(wx.Frame):
  9.     def __init__(self, *args, **kargs):
  10.         #Constructor de MyFrame
  11.         wx.Frame.__init__(self, *args, **kargs)
  12.         #Creamos un Panel dibujable
  13.         self.Panel = DrawPanel(self)
  14.         ... #Esto significa resto del código
  15.  
  16.     def button1Click(self, event):
  17.         ... #Aqui va el código para ver que botón se presiono
  18.         self.Panle.pos = (x, y) #Establecemos la nueva posición, esta posición esta en términos de la matriz.
Espero haber comprendido la idea.

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

Última edición por razpeitia; 19/01/2010 a las 14:01