Reputation: 5631
am fairly new to python and am trying to write a simple program using cairo and wxpython. I am used to using cairo with gtk and C, but am finiding myself confused.
I have built myself a simple ui with the following code:
import wx
class Frame(wx.Frame):
def __init__(self, *args, **kwargs):
super(Frame, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
#----------------------------------------------------
# Build menu bar and submenus
menubar = wx.MenuBar()
# file menu containing quit menu item
fileMenu = wx.Menu()
quit_item = wx.MenuItem(fileMenu, wx.ID_EXIT, '&Quit\tCtrl+W')
fileMenu.AppendItem(quit_item)
self.Bind(wx.EVT_MENU, self.OnQuit, quit_item)
menubar.Append(fileMenu, '&File')
# help menu containing about menu item
helpMenu = wx.Menu()
about_item = wx.MenuItem(helpMenu, wx.ID_ABOUT, '&About\tCtrl+A')
helpMenu.AppendItem(about_item)
self.Bind(wx.EVT_MENU, self.OnAboutBox, about_item)
menubar.Append(helpMenu, '&Help')
self.SetMenuBar(menubar)
#----------------------------------------------------
# Build window layout
panel = wx.Panel(self)
#panel.SetBackgroundColour('yellow')
vbox = wx.BoxSizer(wx.VERTICAL)
panel.SetSizer(vbox)
midPan = wx.Panel(panel)
#midPan.SetBackgroundColour('blue')
hbox = wx.BoxSizer(wx.HORIZONTAL)
vbox.Add(midPan, 1, wx.EXPAND | wx.ALL, 12)
midPan.SetSizer(hbox)
smallPan = wx.Panel(panel)
#smallPan.SetBackgroundColour('red')
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
vbox.Add(smallPan, 0, wx.ALIGN_RIGHT|wx.LEFT|wx.RIGHT|wx.BOTTOM, 12)
smallPan.SetSizer(hbox2)
#----------------------------------------------------
# Place buttons in correct box corresponding with panel
close_button = wx.Button(smallPan, wx.ID_CLOSE)
self.Bind(wx.EVT_BUTTON, self.OnQuit, close_button)
hbox2.Add(close_button)
#----------------------------------------------------
# Set window properties
self.SetSize((1600, 1200))
self.SetTitle('PROGRAM NAME')
self.Centre()
def OnQuit(self, e):
self.Close()
def main():
ex = wx.App()
f = Frame(None)
f.Show(True)
ex.MainLoop()
if __name__ == '__main__':
main()
I would like to be able to draw in the panel named midPan. How do I go about adding the OnDraw function and linking up the signal handler?
I greatly appreciate the help.
Upvotes: 1
Views: 1095
Reputation: 11657
If you are used to procedural programming then the confusion
is natural. Python is an OOP language and coding in an OOP
language is quite different. I have cleaned and updated the
provided example. The panel that is used as a drawing area
paints three coloured rectangles. You have not provided the
implementation of the OnAboutBox()
method, therefore, I have
commented out the line.
#!/usr/bin/python
import wx
import wx.lib.wxcairo
import cairo
class DrawingArea(wx.Panel):
def __init__ (self , *args , **kw):
super(DrawingArea , self).__init__ (*args , **kw)
self.SetDoubleBuffered(True)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, e):
dc = wx.PaintDC(self)
cr = wx.lib.wxcairo.ContextFromDC(dc)
self.DoDrawing(cr)
def DoDrawing(self, cr):
cr.set_source_rgb (0.2 , 0.23 , 0.9)
cr.rectangle(10 , 15, 90, 60)
cr.fill()
cr.set_source_rgb(0.9 , 0.1 , 0.1)
cr.rectangle(130 , 15, 90, 60)
cr.fill()
cr.set_source_rgb(0.4 , 0.9 , 0.4)
cr.rectangle(250 , 15, 90, 60)
cr.fill()
class Frame(wx.Frame):
def __init__(self, *args, **kwargs):
super(Frame, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
#----------------------------------------------------
# Build menu bar and submenus
menubar = wx.MenuBar()
# file menu containing quit menu item
fileMenu = wx.Menu()
quit_item = wx.MenuItem(fileMenu, wx.ID_EXIT, '&Quit\tCtrl+W')
fileMenu.AppendItem(quit_item)
self.Bind(wx.EVT_MENU, self.OnQuit, quit_item)
menubar.Append(fileMenu, '&File')
# help menu containing about menu item
helpMenu = wx.Menu()
about_item = wx.MenuItem(helpMenu, wx.ID_ABOUT, '&About\tCtrl+A')
helpMenu.AppendItem(about_item)
#~ self.Bind(wx.EVT_MENU, self.OnAboutBox, about_item)
menubar.Append(helpMenu, '&Help')
self.SetMenuBar(menubar)
#----------------------------------------------------
# Build window layout
panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
panel.SetSizer(vbox)
midPan = DrawingArea(panel)
vbox.Add(midPan, 1, wx.EXPAND | wx.ALL, 12)
smallPan = wx.Panel(panel)
hbox2 = wx.BoxSizer(wx.HORIZONTAL)
vbox.Add(smallPan, 1, wx.EXPAND|wx.ALL, 12)
smallPan.SetSizer(hbox2)
#----------------------------------------------------
# Place buttons in correct box corresponding with panel
close_button = wx.Button(smallPan, wx.ID_CLOSE)
self.Bind(wx.EVT_BUTTON, self.OnQuit, close_button)
hbox2.Add(close_button)
#----------------------------------------------------
# Set window properties
#~ self.SetSize((1600, 1200))
self.SetSize((400, 250))
#~ self.Maximize()
self.SetTitle('PROGRAM NAME')
self.Centre()
def OnQuit(self, e):
self.Close()
def main():
ex = wx.App()
f = Frame(None)
f.Show(True)
ex.MainLoop()
if __name__ == '__main__':
main()
To do the drawing, we create a custom class that will serve as a
drawing area. It inherits from a wx.Panel
widget.
class DrawingArea(wx.Panel):
def __init__ (self , *args , **kw):
super(DrawingArea , self).__init__ (*args , **kw)
self.SetDoubleBuffered(True)
self.Bind(wx.EVT_PAINT, self.OnPaint)
...
This is our custom class used for drawing. In the constructor
we bind the paint event to the OnPaint()
method.
def OnPaint(self, e):
dc = wx.PaintDC(self)
cr = wx.lib.wxcairo.ContextFromDC(dc)
self.DoDrawing(cr)
Inside the OnPaint()
method we create a cairo drawing
context and delegate the actual drawing code to the DoDrawing()
method.
midPan = DrawingArea(panel)
vbox.Add(midPan, 1, wx.EXPAND | wx.ALL, 12)
The drawing area is created and added to the vertical box.
#~ self.SetSize((1600, 1200))
self.SetSize((400, 250))
#~ self.Maximize()
Final note: if you want to show the window maximized, call the Maximize()
method. Computer screens have different sizes.
Upvotes: 1
Reputation: 704
As explained in Very Simple Drawing example, use wx.EVT_PAINT;
add OnPaint binding to your midPan:
# (...)
midPan = wx.Panel(panel)
#midPan.SetBackgroundColour('blue')
hbox = wx.BoxSizer(wx.HORIZONTAL)
vbox.Add(midPan, 1, wx.EXPAND | wx.ALL, 12)
midPan.SetSizer(hbox)
# binding here:
midPan.Bind(wx.EVT_PAINT, self.OnPaint)
# (...) rest of code
and define your OnPaint code:
# (...)
def OnQuit(self, e):
self.Close()
# your OnPaint():
def OnPaint(self,event):
dc = wx.PaintDC(event.GetEventObject())
dc.Clear()
# set up your pen
dc.SetPen(wx.Pen("BLACK", 4))
# draw whatever you like
dc.DrawLine(0, 0, 50, 50)
# (...) rest of code
Upvotes: 0