Discussion:
minimal: drawing a rectangle
andrea valle
2004-10-12 23:31:00 UTC
Permalink
Here total newbie to wxpython.
As I was translating an example I did in Tkinter I was asking myself
how to do it in wx.
So I was in need of a simple rectangle and tested with this code from
lesson 1 of wiki.
It doesn't work.

from wxPython.wx import *

class MyApp(wxApp):
def OnInit(self):
frame = wxFrame(NULL, -1, "Hello from wxPython")
frame.Show(true)
rect = wxRect(0, 0, 250, 250)
dc = wxDC()
dc.DrawRectangle(rect)
self.SetTopWindow(frame)

return true

app = MyApp(0)
app.MainLoop()

Two prayers:
a) what have I to do to draw a rectangle? (Then I will have to set
dynamically its coordinates).
b) how can I read the references? Dunno exactly what the whole stuff
means, as I wasn't able to find any example of code in it (I'm not used
with all those :: )

Thanks a lot and sorry for the silly question
Best
-a-
Chris Mellon
2004-10-13 00:40:12 UTC
Permalink
Post by andrea valle
Here total newbie to wxpython.
As I was translating an example I did in Tkinter I was asking myself
how to do it in wx.
So I was in need of a simple rectangle and tested with this code from
lesson 1 of wiki.
It doesn't work.
from wxPython.wx import *
The new wx namespace is preferred for new code.
Post by andrea valle
frame = wxFrame(NULL, -1, "Hello from wxPython")
frame.Show(true)
rect = wxRect(0, 0, 250, 250)
dc = wxDC()
If you just want to draw once, then doing it here is fine. You need to
use wxClientDC(frame).
Post by andrea valle
dc.DrawRectangle(rect)
self.SetTopWindow(frame)
return true
app = MyApp(0)
app.MainLoop()
a) what have I to do to draw a rectangle? (Then I will have to set
dynamically its coordinates).
You can do it as above, but a better way is to subclass wxFrame,
provide a handler for the paint event, and draw it there. This will
ensure that the rectangle is, for example, refreshed when the window
is redrawn.
Post by andrea valle
b) how can I read the references? Dunno exactly what the whole stuff
means, as I wasn't able to find any example of code in it (I'm not used
with all those :: )
The official wx docs are at http://wxwidgets.org/docs.htm. These are
the C++ docs, so you'll need to translate them to C++. The wxPython
sources also have docstrings with much of the same information in
them. For getting started and tutorials, the wxPyWiki
(http://wiki.wxpython.org) is a good place to start.
Post by andrea valle
Thanks a lot and sorry for the silly question
Best
-a-
John Fabiani
2004-10-13 03:44:14 UTC
Permalink
Post by Chris Mellon
Post by andrea valle
Here total newbie to wxpython.
As I was translating an example I did in Tkinter I was asking myself
how to do it in wx.
So I was in need of a simple rectangle and tested with this code from
lesson 1 of wiki.
It doesn't work.
from wxPython.wx import *
The new wx namespace is preferred for new code.
Post by andrea valle
frame = wxFrame(NULL, -1, "Hello from wxPython")
frame.Show(true)
rect = wxRect(0, 0, 250, 250)
dc = wxDC()
If you just want to draw once, then doing it here is fine. You need to
use wxClientDC(frame).
Post by andrea valle
dc.DrawRectangle(rect)
self.SetTopWindow(frame)
return true
app = MyApp(0)
app.MainLoop()
a) what have I to do to draw a rectangle? (Then I will have to set
dynamically its coordinates).
You can do it as above, but a better way is to subclass wxFrame,
provide a handler for the paint event, and draw it there. This will
ensure that the rectangle is, for example, refreshed when the window
is redrawn.
Post by andrea valle
b) how can I read the references? Dunno exactly what the whole stuff
means, as I wasn't able to find any example of code in it (I'm not used
with all those :: )
The official wx docs are at http://wxwidgets.org/docs.htm. These are
the C++ docs, so you'll need to translate them to C++. The wxPython
sources also have docstrings with much of the same information in
them. For getting started and tutorials, the wxPyWiki
(http://wiki.wxpython.org) is a good place to start.
Post by andrea valle
Thanks a lot and sorry for the silly question
Best
Your suggestions do not work for me.
DrawRectangle requires five parameters using 'rect' only provides two.
Even when I pass '0, 0, 250, 250' I get nothing drawn on the window.
I did not attempt to use the EVT_PAINT (not sure that's right at the moment) -
either way the paint event.

John
andrea valle
2004-10-13 06:53:29 UTC
Permalink
Thanks a lot Chris

Anycase this doesn't raise errors, but doesn't do anything too.

from wxPython.wx import *

class MyApp(wxApp):
def OnInit(self):
frame = wxFrame(NULL, -1, "Hello from wxPython")
frame.Show(true)
dc = wxClientDC(frame)
dc.DrawRectangle(0,0, 200, 200)
self.SetTopWindow(frame)
return true

app = MyApp(0)
app.MainLoop()

Could you provide some code please?
Post by Chris Mellon
The official wx docs are at http://wxwidgets.org/docs.htm. These are
the C++ docs, so you'll need to translate them to C++. The wxPython
sources also have docstrings with much of the same information in
them. For getting started and tutorials, the wxPyWiki
(http://wiki.wxpython.org) is a good place to start.
The main problem for me with wxpython is that I use Python exactly
because I don't want to use C++...Is there a guide with some hints in
order to understand the docs? wiki does not say anything about it.

Best

-a-
Peter Damoc
2004-10-13 07:52:04 UTC
Permalink
Post by andrea valle
Thanks a lot Chris
Anycase this doesn't raise errors, but doesn't do anything too.
from wxPython.wx import *
frame = wxFrame(NULL, -1, "Hello from wxPython")
frame.Show(true)
dc = wxClientDC(frame)
dc.DrawRectangle(0,0, 200, 200)
self.SetTopWindow(frame)
return true
app = MyApp(0)
app.MainLoop()
Could you provide some code please?
try the attached script... maybe it will shed some light on the subject.
basicaly you'll need a wx.PaintDC of a visible widget and draw on that.
In my example I used a offsecreen DC to draw the stuff and then I blit-ed
the result to the PaintDC... this way you can avoid some of the flickering.
One word of warning... the code was written for a rather new version of
wxpython, it will not work with the old 2.4
--
Peter Damoc
Hacker Wannabe
http://www.sigmacore.net/
andrea valle
2004-10-13 11:31:02 UTC
Permalink
Thanks a lot,
I also found again this
http://wiki.wxpython.org/index.cgi/C_2b_2bGuideForwxPythoneers

-a-
On Wed, 13 Oct 2004 08:53:29 +0200, andrea valle
Post by andrea valle
Thanks a lot Chris
Anycase this doesn't raise errors, but doesn't do anything too.
from wxPython.wx import *
frame = wxFrame(NULL, -1, "Hello from wxPython")
frame.Show(true)
dc = wxClientDC(frame)
dc.DrawRectangle(0,0, 200, 200)
self.SetTopWindow(frame)
return true
app = MyApp(0)
app.MainLoop()
Could you provide some code please?
try the attached script... maybe it will shed some light on the subject.
basicaly you'll need a wx.PaintDC of a visible widget and draw on that.
In my example I used a offsecreen DC to draw the stuff and then I
blit-ed the result to the PaintDC... this way you can avoid some of
the flickering.
One word of warning... the code was written for a rather new version
of wxpython, it will not work with the old 2.4
--
Peter Damoc
Hacker Wannabe
http://www.sigmacore.net/
<draw.py>--------------------------------------------------------------
-------
Andrea Valle
Laboratorio multimediale "G. Quazza"
Facoltà di Scienze della Formazione
Università degli Studi di Torino
***@unito.it


_____________________________________________________________________
For your security, this mail has been scanned and protected by Inflex
Jean-Michel Fauth
2004-10-13 19:11:16 UTC
Permalink
Here, what I think is a good piece of code to start with. Note:
- separation of classes
- wxPython version, very important for drawing
- import wx instead of from...
- None instead of NULL
- True instead of true
- wx.NewId() instead of -1
- wx.FULL_REPAINT_ON_RESIZE
- setup of pen *and* brush before drawing
- redirect=False

Enjoy.
Jean-Michel Fauth, Switzerland

#
# -*- coding: iso-8859-1 -*-
# rectangle.py
# win98, Py234, wxPy2528

import wx

class MyWindow(wx.Window):

def __init__(self, parent, id, pos, size, style):
wx.Window.__init__(self, parent, id, pos, size, style)
self.SetBackgroundColour(wx.WHITE)

self.Bind(wx.EVT_PAINT, self.OnPaint)

def OnPaint(self, event):
dc = wx.PaintDC(self)
dc.BeginDrawing()
dc.SetBrush(wx.Brush(wx.GREEN, wx.SOLID))
dc.SetPen(wx.Pen(wx.RED, 1, wx.SOLID))
dc.DrawRectangle(10, 10, 200, 100)
dc.EndDrawing()

class MyPanel(wx.Panel):

def __init__(self, parent, id, pos, size):
wx.Panel.__init__(self, parent, id, pos, size)

style = wx.SIMPLE_BORDER | wx.FULL_REPAINT_ON_RESIZE
self.win = MyWindow(self, wx.NewId(), (10, 10), (300, 200), style)

class MyFrame(wx.Frame):

def __init__(self, parent, id, title, pos, size, style):
wx.Frame.__init__(self, parent, id, title, pos, size, style)

self.panel = MyPanel(self, wx.NewId(), wx.DefaultPosition, wx.DefaultSize)

self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)

def OnCloseWindow(self, event):
self.Destroy()

class MyApp(wx.App):

def OnInit(self):
style = wx.DEFAULT_FRAME_STYLE
frame = MyFrame(None, wx.NewId(), __file__, (0, 0), (500, 400), style)
frame.Show(True)
self.SetTopWindow(frame)
return True

def main():
app = MyApp(redirect=False)
app.MainLoop()

if __name__ == "__main__" :
main()
John Fabiani
2004-10-13 21:04:11 UTC
Permalink
On Wednesday 13 October 2004 12:11, Jean-Michel Fauth wrote:

Thanks it works for me.

I'm wondering if an additional group shouldn't be created
wxPython-tutor.
John
andrea valle
2004-10-13 21:59:21 UTC
Permalink
Thanks to all for you help.
I haven't any problem with tutorial.
For me the uncovered passage is the one from tutorial to reference.
Anycase, I'm starting having fun.

Best

-a-


Andrea Valle
Laboratorio multimediale "G. Quazza"
Facoltà di Scienze della Formazione
Università degli Studi di Torino
***@unito.it

Loading...