通过示例进行范围设定的Python问题

时间:2012-07-03 08:11:55

标签: python wxpython

我正在尝试很长时间来计算如何在Python中继续参数,名称范围规则也给了我一些困难时期。 我正在使用SVG + Javascript这很简单,我在JS中编写了一些基本代码只是为了解释我的例子,因为阅读Python并没有解决我的愚蠢。 THIS是您可能会在下面看到代码的链接。这么简单。

<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中的其他内容似乎非常简单和合乎逻辑,完全没有麻烦。

1 个答案:

答案 0 :(得分:2)

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

E.g。 (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)