Welcome, guest | Sign In | My Account | Store | Cart

PyGTK is a very powerful GUI-Toolkit. Nearly everything is well documented, except how to print. I messed around for hours just to solve this problem so I decided to share this cool piece of code!

Python, 65 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
# 
# written by Mark Muzenhardt <mark.muzenhardt@googlemail.com>
# published under BSD-License
 
import pygtk
pygtk.require('2.0')
import gtk
 
 
class DrawingAreaExample:
    def __init__(self):
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_title("Drawing Area Example")
        window.connect("destroy", lambda w: gtk.main_quit())
        self.area = gtk.DrawingArea()
        self.area.set_size_request(400, 300)
        window.add(self.area)
 
        self.area.connect("expose-event", self.area_expose_cb)
        self.area.show()
        window.show()
        self.do_print()
 
    def area_expose_cb(self, area, event):
        self.style = self.area.get_style()
        self.gc = self.style.fg_gc[gtk.STATE_NORMAL]
        self.draw_text()
        return True
 
    def do_print(self):
        print_op = gtk.PrintOperation()
        print_op.set_n_pages(1)
        print_op.connect("draw_page", self.print_text)
        res = print_op.run(gtk.PRINT_OPERATION_ACTION_PRINT_DIALOG, None)
 
    def draw_text(self):
        self.pangolayout = self.area.create_pango_layout("")
        self.format_text()
        self.area.window.draw_layout(self.gc, 10, 10, self.pangolayout)
        return
 
    def print_text(self, operation=None, context=None, page_nr=None):
        self.pangolayout = context.create_pango_layout()
        self.format_text()
        cairo_context = context.get_cairo_context()
        cairo_context.show_layout(self.pangolayout)
        return
 
    def format_text(self):
        self.pangolayout.set_text(unicode("""
Dies ist ein Text-Test. Er funktioniert gut und zeigt, dass auch PyGTK
das drucken kann, was man auf eine DrawingArea geschrieben hat.
Anwendungen dafür gibt es genug! 
""", "latin-1"))
 
 
def main():
    gtk.main()
    return 0
 
if __name__ == "__main__":
    DrawingAreaExample()
    main()

4 comments

Dave McCormack 14 years, 9 months ago  # | flag

Ok. Now that I have this, how do I install it so I can use Python from the command line? I need specific step by step procedures. I'm just a newphyte with ZERO programming experience.

Mark Muzenhardt (author) 14 years, 9 months ago  # | flag

This is a GTK application which means you have to install pyGTK before it runs. You can start it from the command line like any other python program too. It doesn't have to be installed, because it is just a script (not a printing library).

dilshan 11 years, 1 month ago  # | flag

can i get a print of an image..pls help me..

Mark Muzenhardt (author) 11 years, 1 month ago  # | flag

You can print an Image by using cairo to render it and then print it just like in this example with pangolayout!

Created by Mark Muzenhardt on Wed, 24 Jun 2009 (MIT)
Python recipes (4591)
Mark Muzenhardt's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks