Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'd like to add a print dialog to my Python + GTK app, and I believe there are at least two methods:

  1. The upstream GTK print dialog
  2. The CUPS print dialog

I'm wondering which would be the recommended one to use for application development. I believe 2. is what Ubuntu uses, so I assume it will work well with indicators and with detecting system printers. However, I haven't been able to find any documentation at all for it.

Any recommendation on the best way to add a print dialog to my app?

share|improve this question
    
what do you mean by "ubuntu uses the CUPS dialog"? What part of Ubuntu? I think most (all) applications inherited from Gnome use the upstream GTK dialog. I would recommend you to do the same. –  Timo Kluck Nov 21 '12 at 22:22
    
I simply said "I believe", as I'm not familiar with printing at all, so any advice, corrections or code snippets are welcome. I looked at blueprints.launchpad.net/ubuntu/+spec/… and I got the impression that there isn't a standard print dialog all apps use yet. –  David Planella Nov 21 '12 at 22:51
    
Programming questions should be asked on stackoverflow. –  psusi Nov 22 '12 at 0:58
    
@psusi: Programming in Ubuntu is a topic of AskUbuntu, please read the FAQ. –  Javier Rivera Nov 22 '12 at 7:59
add comment

1 Answer 1

up vote 4 down vote accepted

It seems that a reasonable way to add printing to your app with Gtk would be the Gtk.PrintOperation. This is supposed to be the high-level, portable version of a print dialog.

In Python, a basic example could look like this:

#!/usr/bin/python
from gi.repository import Gtk

class MyWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Hello World Printing")
        self.button = Gtk.Button(label="Print A Rectangle")
        self.button.connect("clicked", self.on_button_clicked)
        self.add(self.button)

    def on_button_clicked(self, widget):
        pd = Gtk.PrintOperation()
        pd.set_n_pages(1)
        pd.connect("draw_page", self.draw_page)
        result = pd.run(
            Gtk.PrintOperationAction.PRINT_DIALOG, None)
        print result  # handle errors etc.

    def draw_page(self, operation=None, context=None, page_nr=None):
        ctx = context.get_cairo_context()
        w = context.get_width()
        h = context.get_height()
        ctx.set_source_rgb(0.5, 0.5, 1)
        ctx.rectangle(w*0.1, h*0.1, w*0.8, h*0.8)
        ctx.stroke()
        return


win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
share|improve this answer
    
This is the correct way. It will try to run the native printdialog for the user's system. –  Timo Nov 22 '12 at 12:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.