another wxPython-question

Chris Barker chrishbarker at home.net
Thu Oct 4 15:15:09 EDT 2001


Uwe Schmitt wrote:

> in the wxWindows documentation I found classes wxDC, wxPaintDC and
> others. But these are not known if I use wxPython.
> Is my wxPython installation to old ? I downloaded it from
> www.wxpython.org...

It's probably not too old, the ones you mention have been around
forever. You may not have wxPython installed properly, or you may not be
importing it correctly. try this:

>>> from wxPython.wx import *
>>> wxDC
<class wxPython.gdi.wxDC at 0x82554ec>
>>> wxPaintDC
<class wxPython.gdi.wxPaintDC at 0x8255d7c>
>>>

You get something like that, showing that thye do exist. AS for using
them, look at the demo, and I've enclosed a little simple drawing
program to get you started.

-Chris

#!/usr/bin/env python

from wxPython.wx import *
import random
#---------------------------------------------------------------------------


NumLines = 100

ID_DRAW_BUTTON = 100
ID_QUIT_BUTTON = 101

AllPens = [wxRED_PEN,
           wxCYAN_PEN,
           wxGREEN_PEN,
           wxBLACK_PEN,
           wxWHITE_PEN,
           wxTRANSPARENT_PEN,
           wxBLACK_DASHED_PEN,
           wxGREY_PEN,
           wxMEDIUM_GREY_PEN,
           wxLIGHT_GREY_PEN]

class DrawCanvas(wxPanel):
    def __init__(self, parent, id = -1, size = wxDefaultSize):
        
        wxPanel.__init__(self, parent, id, wxPoint(0, 0), size,
wxSUNKEN_BORDER)

        EVT_PAINT(self, self.OnPaint)
        
        self.SetBackgroundColour(wxNamedColor("WHITE"))
        
    def OnPaint(self, event):
        self.Draw()
        
    def Draw(self):
        dc = wxPaintDC(self)
        # Generate a whole bunch of random  lines.

        (maxx,maxy) =
(self.GetClientSize().width,self.GetClientSize().height)
        Lines = []
        Pens = []
        for i in xrange(NumLines):
            x,y = random.randint(1,maxx), random.randint(1,maxy)
            x2,y2 = random.randint(1,maxx), random.randint(1,maxy)
            Lines.append((x,y,x2,y2))
            Pens.append(AllPens[random.randint(0,len(AllPens)-1)])

        dc.BeginDrawing()
        dc.Clear()

        for line,pen in zip(Lines,Pens):
            dc.SetPen(pen)
            dc.DrawLine(line[0],line[1],line[2],line[3])
        dc.EndDrawing()

        
class DrawFrame(wxFrame):
    def __init__(self,parent, id,title,position,size):
        wxFrame.__init__(self,parent, id,title,position, size)
        
        tb = self.CreateToolBar(wxTB_HORIZONTAL|wxNO_BORDER)

        tb.AddControl(wxButton(tb, ID_DRAW_BUTTON,
"DRAW!",wxDefaultPosition, wxDefaultSize))
        EVT_BUTTON(self, ID_DRAW_BUTTON, self.OnClickDraw)

        tb.AddSeparator()

        tb.AddControl(wxButton(tb, ID_QUIT_BUTTON,
"QUIT",wxDefaultPosition, wxDefaultSize))
        EVT_BUTTON(self, ID_QUIT_BUTTON, self.OnClickQuit)
        
        tb.Realize()

        EVT_CLOSE(self, self.OnCloseWindow)
        self.Canvas = DrawCanvas(self,size=(500,500))
        self.Show(true)

        return None

    def OnClickDraw(self,event):
        self.Canvas.Draw()

    def OnClickQuit(self,event):
        self.Close(true)
    
    def OnCloseWindow(self, event):
        self.Destroy()
    
class App(wxApp):
    def OnInit(self):
        frame = DrawFrame(NULL, -1, "Simple Drawing
Window",wxDefaultPosition,wxSize(500,500))
        self.SetTopWindow(frame)
        return true
app = App(0)
app.MainLoop()


-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list