Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
wxPython通过鼠标拖动绘制基本Cairo图形_Python_Drawing_Wxpython_Cairo - Fatal编程技术网

wxPython通过鼠标拖动绘制基本Cairo图形

wxPython通过鼠标拖动绘制基本Cairo图形,python,drawing,wxpython,cairo,Python,Drawing,Wxpython,Cairo,我从来没有用Python编写过代码,也没有尝试过从Javascrpt/SVG转换过来。由于Python和流程流中的变量作用域让我感到困惑,我希望对这些基本代码进行任何修改,使其能够通过mousedown和mouseup事件绘制矩形。除非你没有告诉我代码中的错误,否则请不要把链接放在指令上 如果name==“main”: 导入wx 输入数学 class myframe(wx.Frame): pt1 = 0 pt2 = 0 def __init__(self):

我从来没有用Python编写过代码,也没有尝试过从Javascrpt/SVG转换过来。由于Python和流程流中的变量作用域让我感到困惑,我希望对这些基本代码进行任何修改,使其能够通过mousedown和mouseup事件绘制矩形。除非你没有告诉我代码中的错误,否则请不要把链接放在指令上

如果name==“main”: 导入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()

是的,您的作用域有问题(另外,您的代码没有正确显示)

让我给您一个简短的示例,说明如何在python中使用成员和全局变量:

# 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
在代码中,您混合了所有类型的变量。我认为您应该只创建pt1和pt2实例属性,这样您的代码将如下所示:

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

    ...

你可以考虑阅读一些一般的教程,来了解Python的范围是如何工作的。Python与javascript非常不同,非常棒,但我应该学习它。我现在修复了我的代码。Python与其他语言有点不同。正因为如此,尝试官方教程是值得的,它们不会太枯燥,您将学到很多Python特有的东西。