import wx
import cStringIO
from Main import opj
ID_GRILLA2 = 2
ID_GRILLA3 = 3
ID_GRILLA4 = 4
ID_GRILLA5 = 5
ID_GRILLA6 = 6
ID_GRILLA7 = 7
ID_UP=111
ID_DOWN=112
ID_LEFT=113
ID_RIGHT=114
ID_START=103
ID_EXIT=999
ID_RESET=222
class MyFrame(wx.Frame):
arreglo = []
def __init__(self, *args, **kargs):
wx.Frame.__init__(self, *args, **kargs)
#topsizer = wx.GridSizer(1, 2)
topsizer = wx.BoxSizer( wx.HORIZONTAL )
#menusizer = wx.GridSizer(4, 1)
menusizer = wx.BoxSizer( wx.VERTICAL )
asdf = wx.GridBagSizer()
contentsizer = wx.BoxSizer( wx.VERTICAL )
self.CreateStatusBar()
#Creamos un Panel dibujable y el MenuBar
self.Panel = DrawPanel(self)
menuBar = wx.MenuBar()
self.entrada = wx.TextCtrl(self,-1,value=u"",size=(350,-1))
imageFile = "images/arriba.png"
image1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
arriba = wx.BitmapButton(self, ID_UP, bitmap=image1,size = (image1.GetWidth()+10, image1.GetHeight()+10))
arriba.Bind(wx.EVT_BUTTON, self.button1Click)
imageFile = "images/atras.png"
image1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
atras = wx.BitmapButton(self, ID_DOWN, bitmap=image1,size = (image1.GetWidth()+10, image1.GetHeight()+10))
atras.Bind(wx.EVT_BUTTON, self.button1Click)
imageFile = "images/izquierda.png"
image1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
izquierda = wx.BitmapButton(self, ID_LEFT, bitmap=image1,size = (image1.GetWidth()+10, image1.GetHeight()+10))
imageFile = "images/derecha.png"
image1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
derecha = wx.BitmapButton(self, ID_RIGHT, bitmap=image1,size = (image1.GetWidth()+10, image1.GetHeight()+10))
imageFile = "images/start.png"
image1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
start = wx.BitmapButton(self, ID_START, bitmap=image1,size = (image1.GetWidth()+10, image1.GetHeight()+10))
imageFile = "images/exit.png"
image1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
reset = wx.BitmapButton(self, ID_RESET, bitmap=image1,size = (image1.GetWidth()-3, image1.GetHeight()-3))
arriba.Bind(wx.EVT_BUTTON, self.button1Click)
atras.Bind(wx.EVT_BUTTON, self.button1Click)
derecha.Bind(wx.EVT_BUTTON, self.button1Click)
izquierda.Bind(wx.EVT_BUTTON, self.button1Click)
reset.Bind(wx.EVT_BUTTON, self.OnReplace)
contentsizer.Add(self.Panel,1,wx.EXPAND)
asdf.Add(self.entrada, (0,0),(1,10))
asdf.Add(arriba, (3,2))
asdf.Add(atras, (5,2))
asdf.Add(derecha, (4,3))
asdf.Add(izquierda, (4,1))
asdf.Add(start, (7,1))
asdf.Add(reset, (7,3))
menusizer.Add(asdf, 0)
topsizer.Add(menusizer, 0)
topsizer.Add(contentsizer, 1, wx.EXPAND)
self.SetSizer(topsizer)
#Aniadimos menus
filemenu = wx.Menu()
grillamenu = wx.Menu()
#Aniadimos elementos a esos menus
filemenu.Append(wx.ID_EXIT, "&Close", "Close the program")
for i in range(2, 7 + 1):
grillamenu.Append(i, "%dx%d"%(i, i), "Crear grilla %dx%d" % (i, i), wx.ITEM_RADIO)
#Aniadimos los menus a la barra de menus
menuBar.Append(filemenu, "&File")
menuBar.Append(grillamenu, "&Grill")
#Por ultimo ajustamos la barra de menu
self.SetMenuBar(menuBar)
#Linkeamos los eventos a funciones con su respectiva ID
self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)
for i in range(2, 7 +1):
self.Bind(wx.EVT_MENU, self.SET_ID, id=i)
def button1Click(self,event):
if event.GetId() == 111:
buff = "/\ "
buffT = "adelante"
if event.GetId() == 113:
buff = "< "
buffT = "izquierda"
if event.GetId() == 114:
buff = "> "
buffT = "derecha"
if event.GetId() == 112:
buff = "\/ "
buffT = "atras"
self.arreglo.append(buffT)
print self.arreglo
self.entrada.WriteText(buff+",")
def OnReplace(self, evt):
#self.tc.Replace(5, 9, "IS A")
self.entrada.Remove(0,100)
self.arreglo = []
def SET_ID(self, event):
#Ajustamos el numero de cuadritos a dibujar
self.Panel.n = event.GetId()
self.Panel.Refresh() #Importante redibuja todo el frame
def OnExit(self, event):
self.Close(True)
class DrawPanel(wx.Panel):
"""Draw a line to a panel."""
def __init__(self, *args, **kwargs):
wx.Panel.__init__(self, *args, **kwargs)
self.Bind(wx.EVT_PAINT, self.OnPaint)
#Por defecto son 2
self.n = 2
data = open(opj('images/robot.jpg'), "rb").read()
stream = cStringIO.StringIO(data)
bmp = wx.BitmapFromImage( wx.ImageFromStream( stream ))
ver=320 #centro caja 1 2x2
hor=460
wx.StaticBitmap(self, -1, bmp, (ver, hor))#, (bmp.GetWidth(), bmp.GetHeight()))}
def OnPaint(self, event):
dc = wx.PaintDC(self)
#dc.Clear() #Prueba
dc.SetPen(wx.Pen("BLACK", 4))
n = self.n #Acortamos el nombre
#
x=200 #pos inicial de la col 1
y=50 #pos inicial de la fila 1
z=560/n
for i in range(n):
for j in range(n):
dc.DrawRectangle(i * z+x, j * z+y, z, z)
app = wx.PySimpleApp(False)
frame = MyFrame(None, title="Simulador!!!")
frame.SetSizeHints(1280, 720)
frame.Show(True)
app.MainLoop()