Description
gtk.PrintOperation
is the high-level, portable printing API. It looks a bit different than
other GTK+ dialogs such as the gtk.FileChooser
,
since some platforms don't expose enough infrastructure to implement a
good print dialog. On such platforms, gtk.PrintOperation
uses the native print dialog. On platforms which do not provide a native
print dialog, GTK+ uses its own, see gtkunixprint.PrintUnixDialog
.
The typical way to use the high-level printing API is to create a
gtk.PrintOperation
object with the gtk.PrintOperation
constructor when the user selects to print. Then you set some properties
on it, e.g. the page size, any gtk.PrintSettings
from previous print operations, the number of pages, the current page,
etc.
Then you start the print operation by calling the gtk.PrintOperation.run()
method. It will then show a dialog, let the user select a printer and
options. When the user finished the dialog various signals will be
emitted on the gtk.PrintOperation
,
the main one being "draw-page", which you are supposed to catch and
render the page on the provided gtk.PrintContext
using Cairo.
Example 3. The high-level printing API
settings = None
def do_print():
print_op = gtk.PrintOperation()
if settings != None:
print_op.set_print_settings(settings)
print_op.connect("begin_print", begin_print)
print_op.connect("draw_page", draw_page)
res = print_op.run(gtk.PRINT_OPERATION_ACTION_PRINT_DIALOG, main_window)
if res == gtk.PRINT_OPERATION_RESULT_APPLY:
settings = print_op.get_print_settings()
Printing support was added in GTK+ 2.10.
Methods
gtk.PrintOperation.set_default_page_setup
def set_default_page_setup(default_page_setup
=None)
Note
This method is available in PyGTK 2.10 and above.
The set_default_page_setup
() method
makes default_page_setup
the default page
setup.
This page setup will be used by the gtk.PrintOperation.run()
method, but it can be overridden on a per-page basis by connecting to
the "request-page-setup" signal.
gtk.PrintOperation.get_default_page_setup
def get_default_page_setup()
Returns : | the default page setup |
Note
This method is available in PyGTK 2.10 and above.
The get_default_page_setup
() method
returns the default page setup, see the gtk.PrintOperation.set_default_page_setup()
method.
gtk.PrintOperation.set_print_settings
def set_print_settings(print_settings
=None)
Note
This method is available in PyGTK 2.10 and above.
The set_print_settings
() method sets
the print settings. This is typically used to re-establish print
settings from a previous print operation, see the gtk.PrintOperation.run()
method.
gtk.PrintOperation.get_print_settings
def get_print_settings()
Returns : | the current print settings. |
Note
This method is available in PyGTK 2.10 and above.
The get_print_settings
() method returns
the current print settings.
Note that the return value is None
until
either the gtk.PrintOperation.set_print_settings()
or gtk.PrintOperation.run()
methods have been called.
gtk.PrintOperation.set_job_name
def set_job_name(job_name
)
job_name :
| a string that identifies the print
job |
Note
This method is available in PyGTK 2.10 and above.
The set_job_name
() method sets the name
of the print job. The name is used to identify the job (e.g. in
monitoring applications like eggcups).
If you don't set a job name, GTK+ picks a default one by
numbering successive print jobs.
gtk.PrintOperation.set_n_pages
def set_n_pages(n_pages
)
n_pages :
| the number of pages |
Note
This method is available in PyGTK 2.10 and above.
The set_n_pages
() >method sets the
number of pages in the document.
This must be set to a positive number
before the rendering starts. It may be set in a "begin-print" signal
hander.
Note that the page numbers passed to the "request-page-setup"
and "draw-page" signals are 0-based, i.e. if the user chooses to print
all pages, the last "draw-page" signal will be for page
n_pages
- 1.
gtk.PrintOperation.set_current_page
def set_current_page(current_page
)
current_page :
| the current page, 0-based |
Note
This method is available in PyGTK 2.10 and above.
The set_current_page
() method sets the
current page.
If this is called before gtk.PrintOperation.run()
,
the user will be able to select to print only the current page.
Note that this only makes sense for pre-paginated
documents.
gtk.PrintOperation.set_use_full_page
def set_use_full_page(full_page
)
Note
This method is available in PyGTK 2.10 and above.
The set_use_full_page
() method sets the
"full-page" property to the value of
full_page
. If full_page
is True
, the transformation for the cairo context
obtained from gtk.PrintContext
puts the origin at the top left corner of the page (which may not be
the top left corner of the sheet, depending on page orientation and
the number of pages per sheet). Otherwise, the origin is at the top
left corner of the imageable area (i.e. inside the margins).
gtk.PrintOperation.set_unit
def set_unit(unit
)
Note
This method is available in PyGTK 2.10 and above.
The set_unit
() method sets up the
transformation for the cairo context such distances are measured in
units of unit
.
gtk.PrintOperation.set_export_filename
def set_export_filename(filename
)
filename :
| the filename for the exported file |
Note
This method is available in PyGTK 2.10 and above.
The set_export_filename
() method sets
up the gtk.PrintOperation
to generate a file instead of showing the print dialog. The intended
use of this method is for implementing "Export to PDF"
actions. Currently, PDF is the only supported format.
"Print to PDF" support is independent of this and is done by
letting the user pick the "Print to PDF" item from the list of
printers in the print dialog.
gtk.PrintOperation.set_track_print_status
def set_track_print_status(track_status
)
track_status :
| if True track status after
printing |
Note
This method is available in PyGTK 2.10 and above.
The set_track_print_status
() method
sets the "track-print-status" to the value of
track_status
. If
track_status
is True
, the
print operation will try to continue report on the status of the print
job in the printer queues and printer. This can allow your application
to show things like "out of paper" issues, and when the print job
actually reaches the printer.
This method is often implemented using some form of polling, so
it should not be enabled unless needed.
gtk.PrintOperation.set_show_progress
def set_show_progress(show_progress
)
show_progress :
| if True show a progress
dialog |
Note
This method is available in PyGTK 2.10 and above.
The set_show_progress
() method sets the
"show-progress" property to the value of
show_progress
. If
show_progress
is True
, the
print operation will show a progress dialog during the print
operation.
gtk.PrintOperation.set_allow_async
def set_allow_async(allow_async
)
allow_async :
| if True allow asynchronous
operation |
Note
This method is available in PyGTK 2.10 and above.
The set_allow_async
() method sets the
"allow-async" to the value of allow_async
. If
allow_async
is True
the
gtk.PrintOperation.run()
may return before the print operation is completed. Note that some
platforms may not allow asynchronous operation.
gtk.PrintOperation.set_custom_tab_label
def set_custom_tab_label(label
)
Note
This method is available in PyGTK 2.10 and above.
The set_custom_tab_label
() method sets
the label for the tab holding custom widgets.
gtk.PrintOperation.run
def run(action
, parent
=None)
action :
| the action to start - one of the GTK Print Operation Action Constants |
parent :
| Transient parent of the dialog, or None |
Returns : | the result of the print operation - one of the
GTK Print Operation Result Constants. A
return value of
gtk.PRINT_OPERATION_RESULT_APPLY indicates that
the printing was completed successfully. In this case, it is a
good idea to obtain the used print settings with the gtk.PrintOperation.get_print_settings()
method and store them for reuse with the next print operation. A
value of gtk.PRINT_OPERATION_RESULT_IN_PROGRESS
means the operation is running asynchronously, and will emit the
"done" signal when done. |
Note
This method is available in PyGTK 2.10 and above.
The run
() method runs the print
operation, by first letting the user modify print settings in the
print dialog, and then print the document.
Normally that this method does not return until the rendering of
all pages is complete. You can connect to the "status-changed" signal
to obtain some information about the progress of the print operation.
Furthermore, it may use a recursive mainloop to show the print
dialog.
If you call the gtk.PrintOperation.set_allow_async()
method or set the "allow-async" property the operation will run
asyncronously if this is supported on the platform. The "done" signal
will be emitted with the operation results when the operation is done
(i.e. when the dialog is canceled, or when the print succeeds or
fails).
gtk.PrintOperation.get_error
def get_error()
Returns : | the error message or
None |
Note
This method is available in PyGTK 2.10 and above.
The get_error
() method returns the
error message or None
. Call this when the result of
a print operation is
gtk.PRINT_OPERATION_RESULT_ERROR
, either as
returned by the gtk.PrintOperation.run()
method, or in the "done" signal handler.
gtk.PrintOperation.get_status_string
def get_status_string()
Returns : | a string representation of the status of the
print operation |
Note
This method is available in PyGTK 2.10 and above.
The get_status_string
() method returns
a string representation of the status of the print operation. The
string is translated and suitable for displaying the print status
e.g. in a gtk.Statusbar
.
Use the gtk.PrintOperation.get_status()
method to obtain a status value that is suitable for
programmatic use.
gtk.PrintOperation.is_finished
def is_finished()
Returns : | True , if the print operation
is finished. |
Note
This method is available in PyGTK 2.10 and above.
The method returns True if the print operation is
completed. This is a convenience method to find out if the print
operation is finished, either successfully
(gtk.PRINT_STATUS_FINISHED
) or unsuccessfully
(gtk.PRINT_STATUS_FINISHED_ABORTED
).
Note
When you enable print status tracking, the print operation can
be in a non-finished state even after done has been called, as the
operation status then tracks the print job status on the
printer.
gtk.PrintOperation.cancel
def cancel()
Note
This method is available in PyGTK 2.10 and above.
The cancel
() method cancels a running
print operation. This method may be called from a "begin-print",
"paginate" or "draw-page" signal handler to stop the currently running
print operation.
Signals
The "begin-print" gtk.PrintOperation Signal
def callback(operation
, context
, user_param1
, ...
)
operation :
| the gtk.PrintOperation
on which the signal was emitted |
context :
| the gtk.PrintContext
for the current operation |
user_param1 :
| the first user parameter (if any) specified
with the connect ()
method |
... :
| additional user parameters (if any) |
Note
This signal is available in GTK+ 2.10 and above.
The "begin-print" signal is emitted after the user has finished
changing print settings in the dialog, before the actual rendering
starts.
A typical use for this signal is to use the parameters from the
gtk.PrintContext
and paginate the document accordingly, and then set the number of
pages with gtk.PrintOperation.set_n_pages()
.
The "create-custom-widget" gtk.PrintOperation Signal
def callback(operation
, user_param1
, ...
)
operation :
| the gtk.PrintOperation on which the signal was emitted |
user_param1 :
| the first user parameter (if any) specified
with the connect ()
method |
... :
| additional user parameters (if any) |
Returns : | A custom widget that gets embedded in the print
dialog, or None |
Note
This signal is available in GTK+ 2.10 and above.
The "create-custom-widget" signal is emitted when displaying the
print dialog. If you return a widget in a handler for this signal it
will be added to a custom tab in the print dialog. You typically
return a container widget with multiple widgets in it.
The print dialog owns the returned widget, and its lifetime
isn't controlled by the app. However, the widget is guaranteed to stay
around until the "custom-widget-apply" signal is emitted on the
operation. Then you can read out any information you need from the
widgets.
The "custom-widget-apply" gtk.PrintOperation Signal
def callback(operation
, widget
, user_param1
, ...
)
operation :
| the gtk.PrintOperation on which the signal was emitted |
widget :
| the custom widget added in create-custom-widget |
user_param1 :
| the first user parameter (if any) specified
with the connect ()
method |
... :
| additional user parameters (if any) |
Note
This signal is available in GTK+ 2.10 and above.
The "custom-widget-apply" signal is emitted right before
"begin-print" if you added a custom widget in the
"create-custom-widget" handler. When you get this signal you should
read the information from the custom widgets, as the widgets are not
guaraneed to be around at a later time.
The "done" gtk.PrintOperation Signal
def callback(operation
, result
, user_param1
, ...
)
operation :
| the gtk.PrintOperation on which the signal was emitted |
result :
| the result of the print operation |
user_param1 :
| the first user parameter (if any) specified
with the connect ()
method |
... :
| additional user parameters (if any) |
Note
This signal is available in GTK+ 2.10 and above.
The "done" signal is emitted when the print operation run has
finished doing everything required for
printing. result
(one of the GTK Print Operation Result Constants) gives
you information about what happened during the run. If
result
is
gtk.PRINT_OPERATION_RESULT_ERROR
then you can call
the gtk.PrintOperation.get_error()
method for more information.
If you enabled print status tracking then the gtk.PrintOperation.is_finished()
method may still return False
after this was
emitted.
The "draw-page" gtk.PrintOperation Signal
def callback(operation
, context
, page_nr
, user_param1
, ...
)
operation :
| the gtk.PrintOperation on which the signal was emitted |
context :
| the gtk.PrintContext for the current operation |
page_nr :
| the number of the currently printed page |
user_param1 :
| the first user parameter (if any) specified
with the connect ()
method |
... :
| additional user parameters (if any) |
Note
This signal is available in GTK+ 2.10 and above.
The "draw-page" signal is emitted for every page that is
printed. The signal handler must render the
page_nr
's page onto the cairo context obtained
from context
using gtk.PrintContext.get_cairo_context()
.
Use the gtk.PrintOperation.set_use_full_page()
and gtk.PrintOperation.set_unit()
methods before starting the print operation to set up the
transformation of the cairo context according to your
needs.
The "end-print" gtk.PrintOperation Signal
def callback(operation
, context
, user_param1
, ...
)
operation :
| the gtk.PrintOperation on which the signal was emitted |
context :
| the gtk.PrintContext for the current operation |
user_param1 :
| the first user parameter (if any) specified
with the connect ()
method |
... :
| additional user parameters (if any) |
Note
This signal is available in GTK+ 2.10 and above.
The "end-print" signal is emitted after all pages have been
rendered. A handler for this signal can clean up any resources that
have been allocated in the "begin-print" handler.
The "paginate" gtk.PrintOperation Signal
def callback(printoperation
, context
, user_param1
, ...
)
printoperation :
| the object which received the signal. |
context :
| the gtk.PrintContext
for the current operation |
user_param1 :
| the first user parameter (if any) specified
with the connect ()
method |
... :
| additional user parameters (if any) |
Returns : | |
Note
This signal is available in GTK+ 2.10 and above.
The "paginate" signal is emitted after the "begin-print" signal,
but before the actual rendering starts. It keeps getting emitted until
it returns False
.
This signal is intended to be used for paginating the document
in small chunks, to avoid blocking the user interface for a long
time. The signal handler should update the number of pages using the
gtk.PrintOperation.set_n_pages
()
method, and return True
if the document has been
completely paginated.
If you don't need to do pagination in chunks, you can simply do
it all in the "begin-print" handler, and set the number of pages from
there.
The "preview" gtk.PrintOperation Signal
def callback(operation
, preview
, context
, parent
, user_param1
, ...
)
operation :
| the gtk.PrintOperation on which the signal was emitted |
preview :
| the GtkPrintPreviewOperation for the current operation |
context :
| the gtk.PrintContext that will be used |
parent :
| the gtk.Window to use as window parent, or None |
user_param1 :
| the first user parameter (if any) specified
with the connect ()
method |
... :
| additional user parameters (if any) |
Returns : | True if the listener wants to take over control of the preview |
Note
This signal is available in GTK+ 2.10 and above.
The "preview" signal is emitted when a preview is requested from
the native dialog. If you handle this you must set the cairo context
on the printing context.
If you don't override this, a default implementation using an
external viewer will be used.
The "request-page-setup" gtk.PrintOperation Signal
def callback(operation
, context
, page_nr
, setup
, user_param1
, ...
)
operation :
| the gtk.PrintOperation on which the signal was emitted |
context :
| the gtk.PrintContext for the current operation |
page_nr :
| the number of the currently printed page |
setup :
| the gtk.PageSetup |
user_param1 :
| the first user parameter (if any) specified
with the connect ()
method |
... :
| additional user parameters (if any) |
Note
This signal is available in GTK+ 2.10 and above.
The "request-page-setup" signal is emitted once for every page
that is printed, to give the application a chance to modify the page
setup. Any changes done to setup
will be in
force only for printing this page.
The "status-changed" gtk.PrintOperation Signal
def callback(operation
, printoperation
, user_param1
, ...
)
operation :
| the gtk.PrintOperation on which the signal was emitted |
printoperation :
| the object which received the signal. |
user_param1 :
| the first user parameter (if any) specified
with the connect ()
method |
... :
| additional user parameters (if any) |
Note
This signal is available in GTK+ 2.10 and above.
The "status-changed" signal is emitted at between the various
phases of the print operation. See the GTK Print Status Constants for the phases that
are being discriminated. Use the gtk.PrintOperation.get_status
()
method to find out the current status.