I am trying for a long time to figure how to proceed arguments in Python, also name scoping rules gives me some hard times.
I’m working with SVG+Javascript which is soooo simple to deal with and I wrote some essential code in JS just to explain me on example, because reading about Python didn’t solve my stupidity. THIS is link where you may see below code in action. So simple.
<svg id="svgRoot" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<rect id="background" x="1%" y="2%" width="98%" height="97%" fill="khaki" onmousedown="takePos(evt)" />
<script type="text/ecmascript">
<![CDATA[
var x1, x2;
var Root=document.documentElement;
function takePos(evt){
x1=evt.clientX;
y1=evt.clientY;
var w=0;
var h=0;
Root.setAttributeNS(null, "onmousemove", "mouseMove(evt)");
Root.setAttributeNS(null, "onmouseup", "endPos()");
buildRect(w, h);
}
function mouseMove(evt){
var w=evt.clientX-x1;
var h=evt.clientY-y1;
var r=document.getElementById("svgRoot").lastChild;
if ((w>0)&&(h>0)) {
r.setAttributeNS(null, "width", w);
r.setAttributeNS(null, "height", h);
}
}
function endPos(){
Root.setAttributeNS(null, "onmousemove", null);
Root.setAttributeNS(null, "onmouseup", null);
}
function buildRect(w, h){
var cont=document.getElementById("svgRoot");
var r=document.createElementNS("http://www.w3.org/2000/svg", "rect");
r.setAttributeNS(null, "id", "svgR");
r.setAttributeNS(null, "x", x1);
r.setAttributeNS(null, "y", y1);
r.setAttributeNS(null, "width", w);
r.setAttributeNS(null, "height", h);
r.setAttributeNS(null, "rx", "10");
r.setAttributeNS(null, "fill", "darkblue");
r.setAttributeNS(null, "fill-opacity", "0.5");
cont.appendChild(r);
}
]]>
</script>
</svg>
The simplest mouse drawing rectangle. And now bellow is my Titanic try to write something like that in wx.Python.
# HOW TO DRAW RECTANGLE AS IN MY SVG-EXAMPLE ?
import wx
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, wx.ID_ANY)
self.SetBackgroundColour('white')
self.Bind(wx.EVT_LEFT_DOWN, self.onDown)
self.Bind(wx.EVT_MOTION, self.onMove)
self.Bind(wx.EVT_LEFT_UP, self.onUp)
def onDown(self, evt):
pt1 = evt.GetPosition() # firstPosition tuple
w = pt1.x # starting with zero width and height
h = pt1.y
self.drawRect(pt1, w, h) #??? args
def onMove(self, evt):
pt2 = evt.GetPosition()
w = pt2.x - pt1.x
h = pt2.y - pt1.y
# should to append new width and height while moving
def onUp(self):
# had to finish drawing
def drawRect(self, pt1, w, h):
pdc = wx.PaintDC(self)
try:
# needed to give realistic transparency
dc = wx.GCDC(pdc)
except:
# wx.PaintDC alone will not give transparency
dc = pdc
r, g, b = (30, 140, 220)
pencolour = wx.Colour(r, g, b, 180)
brushcolour = wx.Colour(r, g, b, 120)
dc.SetPen(wx.Pen(pencolour))
dc.SetBrush(wx.Brush(brushcolour))
rect = wx.Rect(pt1, w, h)
dc.DrawRoundedRectangleRect(rect, 8)
app = wx.App(0)
caption = "wx.GCDC() is here to achieve transparency"
frame = wx.Frame(None, wx.ID_ANY, caption, size=(400, 310))
MyPanel(frame)
frame.Show(True)
app.MainLoop()
Please refer to Javascript code to explain me how to do the job in wx.Python. I already tryed to read examples on Internet and from books. Other things in Python seems so simple and logical with no hassling at all.
Since you are using a class (
MyPanel), you should store your rectangles/data in class members (usingself).E.g. (untestet, but you’ll get the idea):