简体   繁体   中英

How to print image with pygtk

I have an application that show an image. I want to be able to print the picture, but I can't figure how to send the image to the printer. I ran into gtk.PrintOperation , but I seen nothing to tell what image file I want to print (so I have a blank page).

Does anyone knows how to set the data to print with pygtk and maybe gtk.PrintOperation?

In gtk.PrintOperation for painting in pages you must connect "draw-page" to a function that get PrintOperation object, a context as a parameter that you can draw page by cairo and pango libraries and page_number as third parameter, and draw pages by this function, similar below:

def print_page(print_dialog, context, n):
    ctx = context.get_cairo_context()
    img = cairo.ImageSurface.create_from_png("an-image.png")
    cr.set_source_surface(img, 20, 20)
    cr.paint()

print_dialog = gtk.PrintOperation()
print_dialog.set_default_page_setup(self.print_page_setup)
print_dialog.set_unit(gtk.Unit.POINTS)
print_dialog.set_n_pages(1)
print_dialog.set_export_filename("/path/to/export/file.pdf")
print_dialog.connect("draw-page", print_page)

win = gtk.Window()
win.connect('clicked', lambda widget: print_dialog.run())
win.set_size_request(200, 200)
win.show_all()
gtk.main()

Read more...

Thanks a lot for the hint!
I don't have only PNG images, so I had to adapt the code like this:

def print_page(self,print_dialog, context, n):
    ctx = context.get_cairo_context()
    gdkcr = gtk.gdk.CairoContext(ctx)
    gdkcr.set_source_pixbuf(self.getPixbuf(), 0,0)
    gdkcr.paint ()

def print_image(self):
    print_dialog = gtk.PrintOperation()
    print_dialog.set_n_pages(1)
    print_dialog.connect("draw-page", self.print_page)

Thanks again .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM