FAQ
Hi

I am trying to write an PAINT like application where on the mouse
click a circle is drawn on canvas. I am new to python and using
wxpython to create this.

here is the code:

import wx

class SketchWindow(wx.Window):

def __init__ (self, parent,ID):

wx.Window.__init__(self, parent, ID)

self.panel =wx.Panel(self, size= (350,350))
self.pen=wx.Pen( 'blue',4)
self.pos=(0,0)
self.InitBuffer()
self.Bind(wx.EVT_LEFT_DOWN,self.OnLeftDown)

def InitBuffer(self):

size=self.GetClientSize()
self.Buffer=wx.EmptyBitmap(size.width,size.height)
dc=wx.BufferedDC(None,self.buffer)
dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
dc.Clear()
self.Drawcircle(dc)
self.reInitBuffer=False

def OnLeftDown(self,event):
self.pos=event.GetPositionTuple()
self.CaptureMouse()

def Drawcircle(self,dc):
pen=wx.Pen(colour,thickness,wx.SOLID)
dc.SetPen(pen)
dc.DrawCircle(self.pos.x,self.pos.y,r)

class SketchFrame(wx.Frame):
def __init__(self, parent):

wx.Frame.__init__(self, parent, -1, "Sketch Frame",size=(800,600))
self.sketch = SketchWindow(self, -1)

if __name__=='__main__':
app=wx.PySimpleApp()
frame=SketchFrame(None)
frame.Show(True)
app.MainLoop()

I am getting the following error:

Traceback (most recent call last):
File "C:/Python26/circle.py", line 42, in <module>
frame=SketchFrame(None)
File "C:/Python26/circle.py", line 38, in __init__
self.sketch = SketchWindow(self, -1)
File "C:/Python26/circle.py", line 12, in __init__
self.InitBuffer()
File "C:/Python26/circle.py", line 19, in InitBuffer
dc=wx.BufferedDC(None,self.buffer)
AttributeError: 'SketchWindow' object has no attribute 'buffer'

Please tell me what I am doing wrong.

Thanks









--
Madhura

Search Discussions

  • Dave Angel at Dec 2, 2009 at 9:06 am

    madhura vadvalkar wrote:
    Hi

    I am trying to write an PAINT like application where on the mouse
    click a circle is drawn on canvas. I am new to python and using
    wxpython to create this.

    here is the code:

    import wx

    class SketchWindow(wx.Window):

    def __init__ (self, parent,ID):

    wx.Window.__init__(self, parent, ID)

    self.panel =wx.Panel(self, size= (350,350))
    self.pen=wx.Pen( 'blue',4)
    self.pos=(0,0)
    self.InitBuffer()
    self.Bind(wx.EVT_LEFT_DOWN,self.OnLeftDown)

    def InitBuffer(self):

    size=self.GetClientSize()
    self.Buffer=wx.EmptyBitmap(size.width,size.height)
    dc=wx.BufferedDC(None,self.buffer)
    You spelled the instance attribute differently. You initialized it as
    "Buffer" but reference it as "buffer" Lowercase would be more standard.
    <snip>
    I am getting the following error:

    Traceback (most recent call last):
    File "C:/Python26/circle.py", line 42, in <module>
    frame=SketchFrame(None)
    File "C:/Python26/circle.py", line 38, in __init__
    self.sketch = SketchWindow(self, -1)
    File "C:/Python26/circle.py", line 12, in __init__
    self.InitBuffer()
    File "C:/Python26/circle.py", line 19, in InitBuffer
    dc=wx.BufferedDC(None,self.buffer)
    AttributeError: 'SketchWindow' object has no attribute 'buffer'

    Please tell me what I am doing wrong.

    Thanks

  • Krishnakant at Dec 3, 2009 at 4:05 am

    On Wed, 2009-12-02 at 00:20 -0800, madhura vadvalkar wrote:
    Hi

    I am trying to write an PAINT like application where on the mouse
    click a circle is drawn on canvas. I am new to python and using
    wxpython to create this.

    here is the code:

    import wx

    class SketchWindow(wx.Window):

    def __init__ (self, parent,ID):

    wx.Window.__init__(self, parent, ID)

    self.panel =wx.Panel(self, size= (350,350))
    self.pen=wx.Pen( 'blue',4)
    self.pos=(0,0)
    self.InitBuffer()
    self.Bind(wx.EVT_LEFT_DOWN,self.OnLeftDown)

    def InitBuffer(self):

    size=self.GetClientSize()
    self.Buffer=wx.EmptyBitmap(size.width,size.height)
    dc=wx.BufferedDC(None,self.buffer)
    dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
    dc.Clear()
    self.Drawcircle(dc)
    self.reInitBuffer=False

    def OnLeftDown(self,event):
    self.pos=event.GetPositionTuple()
    self.CaptureMouse()

    def Drawcircle(self,dc):
    pen=wx.Pen(colour,thickness,wx.SOLID)
    dc.SetPen(pen)
    dc.DrawCircle(self.pos.x,self.pos.y,r)

    class SketchFrame(wx.Frame):
    def __init__(self, parent):

    wx.Frame.__init__(self, parent, -1, "Sketch Frame",size=(800,600))
    self.sketch = SketchWindow(self, -1)

    if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=SketchFrame(None)
    frame.Show(True)
    app.MainLoop()

    I am getting the following error:

    Traceback (most recent call last):
    File "C:/Python26/circle.py", line 42, in <module>
    frame=SketchFrame(None)
    File "C:/Python26/circle.py", line 38, in __init__
    self.sketch = SketchWindow(self, -1)
    File "C:/Python26/circle.py", line 12, in __init__
    self.InitBuffer()
    File "C:/Python26/circle.py", line 19, in InitBuffer
    dc=wx.BufferedDC(None,self.buffer)
    AttributeError: 'SketchWindow' object has no attribute 'buffer'

    Please tell me what I am doing wrong.

    Thanks
    Madhura, Sorry to be a bit off-topic, but, I would really recommend you
    to use pygtk instead of wx.
    For one thing, the developers at pygtk are very active (they have their
    mailing list as well ) and it comes by default with python on almost all
    linux distros. You can also easily install it on windows.

    Most important, the api for pygtk is closely similar to wx.
    Not to mention the quick responses you will get with pygtk related
    problems.

    Happy hacking.
    Krishnakant.
  • R0g at Dec 3, 2009 at 7:55 am

    Krishnakant wrote:
    On Wed, 2009-12-02 at 00:20 -0800, madhura vadvalkar wrote:
    Hi

    I am trying to write an PAINT like application where on the mouse
    <snip>
    File "C:/Python26/circle.py", line 19, in InitBuffer
    dc=wx.BufferedDC(None,self.buffer)
    AttributeError: 'SketchWindow' object has no attribute 'buffer'

    Please tell me what I am doing wrong.

    Thanks
    Madhura, Sorry to be a bit off-topic, but, I would really recommend you
    to use pygtk instead of wx.
    For one thing, the developers at pygtk are very active (they have their
    mailing list as well ) and it comes by default with python on almost all
    linux distros. You can also easily install it on windows.

    Most important, the api for pygtk is closely similar to wx.
    Not to mention the quick responses you will get with pygtk related
    problems.

    Happy hacking.
    Krishnakant.


    I have to recommend to opposite, stick with wx. What's the point of
    tying yourself into GTK when wx works on Mac, PC and Linux?

    Roger.
  • Lie Ryan at Dec 3, 2009 at 3:15 pm

    On 12/3/2009 6:55 PM, r0g wrote:
    Krishnakant wrote:
    Madhura, Sorry to be a bit off-topic, but, I would really recommend you
    to use pygtk instead of wx.
    For one thing, the developers at pygtk are very active (they have their
    mailing list as well ) and it comes by default with python on almost all
    linux distros. You can also easily install it on windows.

    Most important, the api for pygtk is closely similar to wx.
    Not to mention the quick responses you will get with pygtk related
    problems.

    Happy hacking.
    Krishnakant.
    I have to recommend to opposite, stick with wx. What's the point of
    tying yourself into GTK when wx works on Mac, PC and Linux?

    Roger.
    This again... I recommend writing your own C extension module to access
    GDI using <windows.h>; it's ugly, it's non-cross-platform, it gets you
    bashed for reinventing the wheel, and it's buggy. Five thumbs up!
    (err... I only got four... whatever...)
  • Nobody at Dec 3, 2009 at 9:22 pm

    On Thu, 03 Dec 2009 07:55:19 +0000, r0g wrote:

    I have to recommend to opposite, stick with wx. What's the point of
    tying yourself into GTK when wx works on Mac, PC and Linux?
    The main drawbacks are that wxWidgets sucks compared to GTK or Qt (mostly
    due to being modelled on the Win32 API, which itself sucks compared to
    most Unix/X11 toolkits), and that wxPython is woefully under-documented
    (whereas PyGTK is better documented than GTK itself).

    But if you need portability, wxPython is the way to go.
  • Mike Driscoll at Dec 3, 2009 at 10:09 pm

    On Dec 3, 3:22?pm, Nobody wrote:
    On Thu, 03 Dec 2009 07:55:19 +0000, r0g wrote:
    I have to recommend to opposite, stick with wx. What's the point of
    tying yourself into GTK when wx works on Mac, PC and Linux?
    The main drawbacks are that wxWidgets sucks compared to GTK or Qt (mostly
    due to being modelled on the Win32 API, which itself sucks compared to
    most Unix/X11 toolkits), and that wxPython is woefully under-documented
    (whereas PyGTK is better documented than GTK itself).

    But if you need portability, wxPython is the way to go.
    There is currently an on-going documentation initiative going on for
    wxPython. Robin Dunn is doing some work on it that will allow much
    easier documentation patching (as I understand it), so that should get
    better.

    Also, I am slowly working my way through wxPython many widgets and
    doing tutorials on them on my blog, which I then stick in the official
    wxPython wiki if I think the article is good enough.

    -------------------
    Mike Driscoll

    Blog: http://blog.pythonlibrary.org
  • Dietmar Schwertberger at Dec 3, 2009 at 10:09 am

    On Wed, 2009-12-02 at 00:20 -0800, madhura vadvalkar wrote:
    def InitBuffer(self):

    size=self.GetClientSize()
    self.Buffer=wx.EmptyBitmap(size.width,size.height)
    dc=wx.BufferedDC(None,self.buffer)
    dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
    dc.Clear()
    self.Drawcircle(dc)
    self.reInitBuffer=False
    I am getting the following error:

    Traceback (most recent call last):
    File "C:/Python26/circle.py", line 42, in <module>
    frame=SketchFrame(None)
    File "C:/Python26/circle.py", line 38, in __init__
    self.sketch = SketchWindow(self, -1)
    File "C:/Python26/circle.py", line 12, in __init__
    self.InitBuffer()
    File "C:/Python26/circle.py", line 19, in InitBuffer
    dc=wx.BufferedDC(None,self.buffer)
    AttributeError: 'SketchWindow' object has no attribute 'buffer'

    Please tell me what I am doing wrong.
    As the traceback suggests, self.buffer does not exist. You need to write
    self.Buffer.
    I did not have a further look or try the code.
    Did you have a look at the wxPython demo?
    The demo "Core Windows/Controls -> ScrolledWindow" is probably very
    similar to what you want.

    Krishnakant schrieb:
    Madhura, Sorry to be a bit off-topic, but, I would really recommend you
    to use pygtk instead of wx.
    For one thing, the developers at pygtk are very active (they have their
    mailing list as well ) and it comes by default with python on almost all
    linux distros. You can also easily install it on windows.

    Most important, the api for pygtk is closely similar to wx.
    Not to mention the quick responses you will get with pygtk related
    problems.
    That's also true for wxPython on the related mail lists:
    http://www.wxpython.org/maillist.php


    Regards,

    Dietmar
  • Mike Driscoll at Dec 3, 2009 at 10:07 pm

    On Dec 2, 10:05?pm, Krishnakant wrote:
    On Wed, 2009-12-02 at 00:20 -0800, madhura vadvalkar wrote:
    Hi
    I am trying to write an ?PAINT like application where on the mouse
    click a circle is drawn on canvas. I am new to python and using
    wxpython to create this.
    here is the code:
    import wx
    class SketchWindow(wx.Window):
    ? ? def __init__ (self, parent,ID):
    ? ? ? ? wx.Window.__init__(self, parent, ID)
    ? ? ? ? self.panel =wx.Panel(self, size= (350,350))
    ? ? ? ? self.pen=wx.Pen( 'blue',4)
    ? ? ? ? self.pos=(0,0)
    ? ? ? ? self.InitBuffer()
    ? ? ? ? self.Bind(wx.EVT_LEFT_DOWN,self.OnLeftDown)
    ? ? def InitBuffer(self):
    ? ? ? ? size=self.GetClientSize()
    ? ? ? ? self.Buffer=wx.EmptyBitmap(size.width,size.height)
    ? ? ? ? dc=wx.BufferedDC(None,self.buffer)
    ? ? ? ? dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
    ? ? ? ? dc.Clear()
    ? ? ? ? self.Drawcircle(dc)
    ? ? ? ? self.reInitBuffer=False
    ? ? def OnLeftDown(self,event):
    ? ? ? ? self.pos=event.GetPositionTuple()
    ? ? ? ? self.CaptureMouse()
    ? ? def Drawcircle(self,dc):
    ? ? ? ? pen=wx.Pen(colour,thickness,wx.SOLID)
    ? ? ? ? dc.SetPen(pen)
    ? ? ? ? dc.DrawCircle(self.pos.x,self.pos.y,r)
    class SketchFrame(wx.Frame):
    ? ? def __init__(self, parent):
    ? ? ? ? wx.Frame.__init__(self, parent, -1, "Sketch Frame",size=(800,600))
    ? ? ? ? self.sketch = SketchWindow(self, -1)
    if __name__=='__main__':
    ? ? app=wx.PySimpleApp()
    ? ? frame=SketchFrame(None)
    ? ? frame.Show(True)
    ? ? app.MainLoop()
    I am getting the following error:
    Traceback (most recent call last):
    ? File "C:/Python26/circle.py", line 42, in <module>
    ? ? frame=SketchFrame(None)
    ? File "C:/Python26/circle.py", line 38, in __init__
    ? ? self.sketch = SketchWindow(self, -1)
    ? File "C:/Python26/circle.py", line 12, in __init__
    ? ? self.InitBuffer()
    ? File "C:/Python26/circle.py", line 19, in InitBuffer
    ? ? dc=wx.BufferedDC(None,self.buffer)
    AttributeError: 'SketchWindow' object has no attribute 'buffer'
    Please tell me what I am doing wrong.
    Thanks
    Madhura, Sorry to be a bit off-topic, but, I would really recommend you
    to use pygtk instead of wx.
    For one thing, the developers at pygtk are very active (they have their
    mailing list as well ) and it comes by default with python on almost all
    linux distros. ?You can also easily install it on windows.

    wxPython has some very active developers as well, but the core isn't
    released all that often. I think this may be caused somewhat by the
    slow release cycle of wx itself though.

    Most important, the api for pygtk is closely similar to wx.
    Not to mention the quick responses you will get with pygtk related
    problems.

    The wxPython mailing list has very fast responses to questions as well
    (most of the time)

    -------------------
    Mike Driscoll

    Blog: http://blog.pythonlibrary.org
  • Kevin Walzer at Dec 3, 2009 at 11:42 pm

    Madhura, Sorry to be a bit off-topic, but, I would really recommend you
    to use pygtk instead of wx.
    For one thing, the developers at pygtk are very active (they have their
    mailing list as well ) and it comes by default with python on almost all
    linux distros. You can also easily install it on windows.

    Most important, the api for pygtk is closely similar to wx.
    Not to mention the quick responses you will get with pygtk related
    problems.
    If you want to include the Mac in your distribution, PyGtk isn't a good
    choice. Native support is quite experimental at this point, which
    basically means you'll be running it under X11. (WingIDE's Mac version
    runs on PyGTK, and it doesn't feel native at all.)

    --
    Kevin Walzer
    Code by Kevin
    http://www.codebykevin.com

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedDec 2, '09 at 8:20a
activeDec 3, '09 at 11:42p
posts10
users9
websitepython.org

People

Translate

site design / logo © 2022 Grokbase