2012-03-24 16 views
0

私は決してPythonでコードを作成せず、Javascrpt/SVGから切り替えようとしています。 Pythonで可変スコープとプロセスフローを混乱させるので、mousedownとmouseupイベントで長方形を描くように、これらの基本コードに修正を加えていただきたいと思います。あなたがコードでエラーを指摘していない限り、指示へのリンクを入れないでください。wxPythonの基本カイロのマウスドラッグによる描画

かの名前 == "メイン": インポートWX 輸入数学

class myframe(wx.Frame): 
    pt1 = 0 
    pt2 = 0 
    def __init__(self): 
     wx.Frame.__init__(self, None, -1, "test", size=(500,400)) 
     self.Bind(wx.EVT_LEFT_DOWN, self.onDown) 
     self.Bind(wx.EVT_LEFT_UP, self.onUp) 
     self.Bind(wx.EVT_PAINT, self.drawRect) 

    def onDown(self, event):   
     global pt1 
     pt1 = event.GetPosition() # firstPosition tuple 

    def onUp(self, event):   
     global pt2 
     pt2 = event.GetPosition() # secondPosition tuple 

    def drawRect(self, event): 
     dc = wx.PaintDC(self) 
     gc = wx.GraphicsContext.Create(dc) 
     nc = gc.GetNativeContext() 
     ctx = Context_FromSWIGObject(nc) 

     ctx.rectangle (pt1.x, pt1.y, pt2.x, pt2.y) # Rectangle(x0, y0, x1, y1) 
     ctx.set_source_rgba(0.7,1,1,0.5) 
     ctx.fill_preserve() 
     ctx.set_source_rgb(0.1,0.5,0) 
     ctx.stroke() 


app = wx.App() 
f = myframe() 
f.Show() 
app.MainLoop() 

答えて

1

葉は、あなたがスコープに問題がある(プラス - あなたのコードが正しく表示されません)。あなたは、変数のすべての種類を混合しているあなたのコードで

# Globals are defined globally, not in class 
glob1 = 0 

class C1: 
    # Class attribute 
    class_attrib = None # This is rarely used and tricky 

    def __init__(self): 
     # Instance attribute 
     self.pt1 = 0 # That's the standard way to define attribute 

    def other_method(self): 
     # Use of a global in function 
     global glob1 
     glob1 = 1 

     # Use of a member 
     self.pt1 = 1 

# Use of a class attribute 
C1.class_attrib = 1 

は私がどのようにPythonでメンバーとグローバルを使用する方法に簡単な例を挙げてみましょう。あなたはPythonのスコープがどのように機能するかを学ぶために、this oneのようないくつかの一般的なチュートリアルを読んで検討することもでき

class MyFrame(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self, None, -1, "test", size=(500,400)) 
     self.pt1 = self.pt2 = 0 
     ... 

    def onDown(self, event):   
     self.pt1 = event.GetPosition() # firstPosition tuple 

    ... 

:私はあなたのコードは次のようになり、あなただけpt1とpt2のインスタンスの属性を作るべきだと思います。

+0

Thanx a lot。 Pythonはjavascriptと大きく違っていますが、私はそれを学ばなければなりません。今すぐコードを修正しました。 – Alex

+0

Pythonは他の言語と少し異なります。そのため、公式のチュートリアルを試してみる価値はありますが、あまりにも退屈ではないので、Python固有のものをたくさん学ぶことになります。 – Tupteq

関連する問題