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?

  • 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
up vote 6 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:

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)

    def on_button_clicked(self, widget):
        pd = Gtk.PrintOperation()
        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)

win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
  • 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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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