1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
import wx
class MyCanvas(wx.Frame):
def __init__(self, titre):
wx.Frame.__init__(self, parent = None, id = 1, title = titre, size = (1200, 1000))
self.conteneur = wx.Panel(self, 1, size = self.GetClientSize())
# Initialise les différents Sizer
vbox = wx.BoxSizer(wx.VERTICAL)
self.bordure1 = wx.BoxSizer(wx.VERTICAL)
self.Commandes = wx.BoxSizer(wx.VERTICAL)
# Création du Widget wxStaticBox
self.staticBox1 = wx.StaticBox ( self.conteneur, -1, 'Dessin' )
self.staticBoxSizer1 = wx.StaticBoxSizer ( box = self.staticBox1, orient=wx.VERTICAL )
# Ecran "Map"
self.sizer_Map = wx.BoxSizer(orient=wx.VERTICAL)
# Création du panel "P_Map"
global P_Map
P_Map = Interface_Map(self.conteneur)
self.sizer_Map.AddSizer(P_Map, 1, border=0, flag=wx.EXPAND)
self.staticBoxSizer1.AddSizer(self.sizer_Map, 1, border=0, flag=wx.EXPAND)
#-----------------------------------------------------------
# Configure the box sizers
vbox .Add(self.Commandes)
self.bordure1.Add(self.staticBoxSizer1, 1, wx.EXPAND | wx.ALL, 10)
self.bouton_ok=wx.Button(self,wx.ID_OK)
self.Commandes.Add(self.bouton_ok, flag=wx.ALIGN_RIGHT | wx.ALL, border=10)
self.Cellule=wx.TextCtrl(self,-1,"123", style=wx.TE_RIGHT)
self.Commandes.Add(self.Cellule)
self.bouton_ok.Bind(wx.EVT_BUTTON,P_Map.Id_Noir)
# Attach everything
self.conteneur.SetSizerAndFit (self.bordure1)
vbox.Add(self.conteneur, 1, wx.EXPAND)
# Rendu final du sizer
self.SetSizer(vbox)
self.pdc = wx.PseudoDC()
# self.cel=self.Cellule.GetValue()
#-------------------------------------------------------------------------
class Interface_Map(wx.ScrolledWindow):
"""Création de l'interface """
#-------------------------------------------------------------------------
def __init__(self, conteneur):
wx.ScrolledWindow.__init__(self, conteneur)
# Map "Dessin" :
self.SetBackgroundColour("white") # Couleur 'Blanc'
# create a PseudoDC to record our drawing
self.pdc = wx.PseudoDC()
self.OnAddGrille(self.pdc)
# Evenements
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_ERASE_BACKGROUND, lambda x:None)
self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseMove)
self.Bind(wx.EVT_LEAVE_WINDOW, self._onLeaveWindow)
self.Bind(wx.EVT_ENTER_WINDOW, self._onEnterWindow)
# vars for handling mouse clicks
self.dragid = -1
self.lastpos = (0,0)
#*********************************************************
def Id_Noir (self,evt):
# Recherche de la position
position = self.pdc.GetIdBounds(int(fen.Cellule.GetValue()))
self.pdc.SetBrush(wx.Brush("black"))
self.pdc.FloodFill(position[0], position[1], "white", style=wx.FLOOD_SURFACE)
self.RefreshRect(position, False)
def OnPaint(self, event):
# Create a buffered paint DC. It will create the real
# wx.PaintDC and then blit the bitmap to it when dc is
# deleted.
dc = wx.BufferedPaintDC(self)
# use PrepateDC to set position correctly
self.PrepareDC(dc)
# we need to clear the dc BEFORE calling PrepareDC
bg = wx.Brush(self.GetBackgroundColour())
dc.SetBackground(bg)
dc.Clear()
self.pdc.DrawToDC(dc)
#---------------------------------------------------------
def _onLeaveWindow(self, event):
self.dragidLeft = 0
self.dragidRight = 0
def _onEnterWindow(self, event):
self.dragidLeft = 0
self.dragidRight = 0
def OnMouseMove(self, event):
self.cel=fen.Cellule.GetValue()
if event.LeftDown():
self.dragidLeft = 1
if event.RightDown():
self.dragidRight = 1
if wx.EVT_MOTION and (self.dragidLeft or self.dragidRight):
x,y = event.GetPosition()
l = self.pdc.FindObjects(x, y, 1)
for id in l:
if not self.pdc.GetIdGreyedOut(id):
self.dragid = id
break
if self.dragidLeft:
self.pdc.SetBrush(wx.Brush("black"))
self.pdc.FloodFill(x, y, "white", style=wx.FLOOD_SURFACE)
if self.dragidRight:
self.pdc.SetBrush(wx.Brush("white"))
self.pdc.FloodFill(x, y, "black", style=wx.FLOOD_SURFACE)
r = self.pdc.GetIdBounds(self.dragid)
self.RefreshRect(r, False)
if event.LeftUp():
self.dragidLeft = 0
self.dragid = -1
if event.RightUp():
self.dragidRight = 0
self.dragid = -1
#---------------------------------------------------------
def OffsetRect(self, r):
xView, yView = self.GetViewStart()
xDelta, yDelta = self.GetScrollPixelsPerUnit()
r.OffsetXY(-(xView*xDelta),-(yView*yDelta))
#---------------------------------------------------------
def OnAddGrille(self,dc):
self.pDC = dc
self.pDC.SetPen(wx.Pen('#d4d4d4'))
self.pDC.BeginDrawing()
self.pDC.SetPen(wx.Pen("grey",style=wx.TRANSPARENT))
self.pDC.SetBrush(wx.Brush("white", wx.SOLID))
taille=10
for y in range(100):
for x in range(120):
posX=x*(taille)
posY=y*(taille)
id = wx.NewId()
self.pDC.SetId(id)
self.pDC.DrawRectangle(posX,posY,taille,taille)
r = wx.Rect(posX,posY,taille,taille)
self.pDC.SetIdBounds(id,r)
self.pDC.SetId
self.pDC.EndDrawing()
return self.pDC
class MonApp(wx.App):
def OnInit(self):
global fen
fen = MyCanvas("Création Trame")
fen.Show(True)
self.SetTopWindow(fen)
return True
app = MonApp()
app.MainLoop() |
Partager