1

我试图弄清楚如何在 Python 中处理参数,名称范围规则也给我带来了一些困难。我正在使用 SVG+Javascript,它处理起来非常简单,我在 JS 中编写了一些基本代码只是为了举例说明,因为阅读 Python 并没有解决我的愚蠢问题。是链接,您可以在其中看到下面的代码。很简单。

<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>

最简单的鼠标绘制矩形。现在下面是我的泰坦尼克号尝试在 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()

请参考 Javascript 代码来解释我如何在 wx.Python 中完成这项工作。我已经尝试在互联网和书籍中阅读示例。Python 中的其他东西看起来非常简单和合乎逻辑,一点也不麻烦。

4

1 回答 1

2

由于您使用的是类 ( MyPanel),因此您应该将矩形/数据存储在类成员中 (使用self)。

例如(untestet,但你会明白的):

def __init__(self, ...)
    ...
    self.pt1 = None
    self.pt2 = None
    self.rects = []

def onDown(self, evt):
    self.pt1 = evt.GetPosition() # new start position

def onMove(self, evt):
    self.pt2 = evt.GetPosition() # end position

def onUp(self, evt):
    # if mouse is released, store the points in the list
    self.rects.append((self.pt1, self.pt2))
    self.pt1 = self.pt2 = None

def onDraw(self, evt):
    dc = wx.PaintDC(self)

    # if we are currently creating a new rect, draw it
    if self.pt1 and self.pt2:
        self.drawRect(dc, self.pt1, self.pt2)

    # draw all finished rects
    for p1, p2 in self.rects:
        self.drawRect(dc, p1, p2)
于 2012-07-03T09:12:32.173 回答