PyGTK DrawingArea Class - PyGTK

What is PyGTK DrawingArea Class?

The DrawingArea widget offers a blank canvas covering a gtk.gdk.Window on which objects such as line, rectangle, arc, etc. can be drawn.
PyGTK uses Cairo library for such drawing processes. Cairo is a popular 2D vector graphics library. It is written in C., though, it has bindings in most Languages such as C++, Java, Python, PHP etc. Cairo library can be used to draw on normal output devices in numerous operating systems. It can also be used to create PDF, SVG and post-script files.

In order to do alter drawing operations, we must make the device on text of the target output object. In this case, meanwhile the drawing is appearing on gtk.DrawingArea widget, the device context of gdk.Window limited inside it is obtained. This class has a cairo-create() method which returns the device context.

The DrawingArea widget can be connected to the callbacks based on the following signals emitted by it −

Realize

To take any necessary actions when the widget is instantiated on a particular display.

configure_event

To take any necessary actions when the widget changes size.

expose_event

To handle redrawing the contents of the widget when a drawing area first comes on screen, or when it's covered by another window and then uncovered (exposed).

The Mouse and Keyboard events can also be used to appeal callbacks by add_events() method of the gtk.Widget class.

Of specific attention is the expose-event signal which is produced when the DrawingArea canvas first comes up. The dissimilar approaches for drawing 2D objects that are defined in the Cairo library are called from this callback linked to the expose-event signal. These methods draw consistent objects on the Cairo device context.

The resulting are the obtainable drawing methods −

  • dc.rectangle(x,y,w,h) − This draws a rectangle at the specified top left coordinate and having givwn width and height.
  • dc.arc(x,y,r,a1,a2) − This draws a circular arc with given radius and two angles.
  • dc.line(x1, y1, x2, y2) − This draws a line between two pairs of coordinates.
  • dc.line_to(x,y) − This draws a line from the current position to (x,y)
  • dc.show_text(str) − draws string at current cursor position
  • dc.stroke() − draws outline
  • dc.fill() − fills shape with current color
  • dc.set_color_rgb(r,g,b) − sets color to outline and fill with r, g and b values between 0.0 to 1.0

Example

The resulting script draws different shapes and test using Cairo methods.

The above script will generate the resulting output –

basic_shapes_cairo

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PyGTK Topics