Preselect Printer and printer properties without bothering the user

时间:2019-04-08 12:49:22

标签: python-3.x printing gtk3

I'm writing a Python3-Gtk3-application that prints QR-Labels on a label printer. Until now my code

...
psize = Gtk.PaperSize.new_custom('Our-QR-Label', 'QR-Label', 29, 42, Gtk.Unit.MM)
pagesetup = Gtk.PageSetup()
pagesetup.set_paper_size(psize)
...
pagesetup.set_orientation(Gtk.PageOrientation.PORTRAIT)

print_dialog = Gtk.PrintOperation()
print_dialog.set_n_pages(len(self.qrcodes))
print_dialog.set_default_page_setup(pagesetup)
print_dialog.connect("draw-page", self.print_page)
print_dialog.run(Gtk.PrintOperationAction.PRINT_DIALOG, None)
...

shows the printer dialog with the default printer selected. When the user selects the label printer and starts printing, anything works.

But since our users normally never see any printer dialogs, they will be confused and can mess up everything :-(

I know that I can call

print_dialog.run(Gtk.PrintOperationAction.PRINT, None)

to bypass the print dialog, but then the output goes to the standard printer, which is not the desired result.

So: How can I preset a printer (by name) to the Gtk.PrintOperation?

P.S.: I'm writing and testing my programs on Linux but our users work on Windows.

1 个答案:

答案 0 :(得分:0)

我知道的最简单的方法是将打印设置保存到文件中。如果该文件不存在,请让用户选择打印机,然后将这些设置保存到文件中。

从文件中检索打印设置:

self.settings_file = ("my/settings/file")
try:
    settings = Gtk.PrintSettings.new_from_file(self.settings_file)
    print_dialog.set_print_settings(settings)
    #do your print stuff here without a dialog
except Exception as e:
    print ("Error when loading print settings file: ", str(e))
    settings = Gtk.PrintSettings()
    print_dialog.set_print_settings(settings)
    #do your print stuff here using a dialog

捕获所选打印机并将其保存到文件:

result = print_dialog.run(Gtk.PrintOperationAction.PRINT_DIALOG, None)
if result == Gtk.PrintOperationResult.APPLY:
    settings = print_dialog.get_print_settings()
    settings.to_file(self.settings_file)