Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to use a Double Buffer technique:</p> <p><a href="http://en.wikipedia.org/wiki/Multiple_buffering#Double_buffering_in_computer_graphics" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Multiple_buffering#Double_buffering_in_computer_graphics</a></p> <p>That is you have an image, and you draw over that image: that image is the "behind the scenes" buffer. You can have a lot of methods that draw something to that image. Then, on the callback that responds to 'draw' signal, that is, the method that actually draws something to the graphics memory you just throw your "behind the scenes" image.</p> <p>Theory in code (<strong>test.py</strong>):</p> <pre><code>import cairo from gi.repository import Gtk from os.path import abspath, dirname, join WHERE_AM_I = abspath(dirname(__file__)) class MyApp(object): """Double buffer in PyGObject with cairo""" def __init__(self): # Build GUI self.builder = Gtk.Builder() self.glade_file = join(WHERE_AM_I, 'test.glade') self.builder.add_from_file(self.glade_file) # Get objects go = self.builder.get_object self.window = go('window') # Create buffer self.double_buffer = None # Connect signals self.builder.connect_signals(self) # Everything is ready self.window.show() def draw_something(self): """Draw something into the buffer""" db = self.double_buffer if db is not None: # Create cairo context with double buffer as is DESTINATION cc = cairo.Context(db) # Scale to device coordenates cc.scale(db.get_width(), db.get_height()) # Draw a white background cc.set_source_rgb(1, 1, 1) # Draw something, in this case a matrix rows = 10 columns = 10 cell_size = 1.0 / rows line_width = 1.0 line_width, notused = cc.device_to_user(line_width, 0.0) for i in range(rows): for j in range(columns): cc.rectangle(j * cell_size, i * cell_size, cell_size, cell_size) cc.set_line_width(line_width) cc.set_source_rgb(0, 0, 0) cc.stroke() # Flush drawing actions db.flush() else: print('Invalid double buffer') def main_quit(self, widget): """Quit Gtk""" Gtk.main_quit() def on_draw(self, widget, cr): """Throw double buffer into widget drawable""" if self.double_buffer is not None: cr.set_source_surface(self.double_buffer, 0.0, 0.0) cr.paint() else: print('Invalid double buffer') return False def on_configure(self, widget, event, data=None): """Configure the double buffer based on size of the widget""" # Destroy previous buffer if self.double_buffer is not None: self.double_buffer.finish() self.double_buffer = None # Create a new buffer self.double_buffer = cairo.ImageSurface(\ cairo.FORMAT_ARGB32, widget.get_allocated_width(), widget.get_allocated_height() ) # Initialize the buffer self.draw_something() return False if __name__ == '__main__': gui = MyApp() Gtk.main() </code></pre> <p>Glade file (<strong>test.glade</strong>):</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;interface&gt; &lt;!-- interface-requires gtk+ 3.0 --&gt; &lt;object class="GtkWindow" id="window"&gt; &lt;property name="can_focus"&gt;False&lt;/property&gt; &lt;property name="window_position"&gt;center-always&lt;/property&gt; &lt;property name="default_width"&gt;800&lt;/property&gt; &lt;property name="default_height"&gt;600&lt;/property&gt; &lt;signal name="destroy" handler="main_quit" swapped="no"/&gt; &lt;child&gt; &lt;object class="GtkDrawingArea" id="drawingarea1"&gt; &lt;property name="visible"&gt;True&lt;/property&gt; &lt;property name="can_focus"&gt;False&lt;/property&gt; &lt;signal name="draw" handler="on_draw" swapped="no"/&gt; &lt;signal name="configure-event" handler="on_configure" swapped="no"/&gt; &lt;/object&gt; &lt;/child&gt; &lt;/object&gt; &lt;/interface&gt; </code></pre> <p>Dependencies:</p> <p>Python 2: </p> <pre><code>sudo apt-get install python-cairo </code></pre> <p>Python 3:</p> <pre><code>sudo apt-get install python3-gi-cairo </code></pre> <p>Now execute with:</p> <pre><code>python test.py </code></pre> <p>or</p> <pre><code>python3 test.py </code></pre> <p>What it looks like:</p> <p><img src="https://i.stack.imgur.com/acMdA.png" alt="enter image description here"></p> <p>All the documentation for cairo can be found in <a href="http://cairographics.org/documentation/pycairo/3/reference/index.html" rel="nofollow noreferrer">http://cairographics.org/documentation/pycairo/3/reference/index.html</a></p> <p>The above is a port of an example I've made in C long ago for Gtk 2.16, you can check it too, but is in Spanish:</p> <p><a href="http://carlos.jenkins.co.cr/gtkcairo" rel="nofollow noreferrer">http://carlos.jenkins.co.cr/gtkcairo</a></p> <p>Kind regards</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload