0

I am making a notepad using Tkinter and I want to be able to print to the printer the notes taken.I am using a Text object in order to allow the user to write his notes.I couldn't find anything on to how to print to printer with Tkinter and I want to avoid using PyQT or win32api that I know have printer support(due to complexity and lack of experience).I am planning on releasing the application only on windows so I donnot need the it to be cross-platform.

RafD
  • 65
  • 1
  • 8
  • Tkinter has nothing to do with printers. Printer interface is never simple, so PyQT/win32api shouldn't matter – rizerphe Apr 04 '20 at 16:14
  • How can I easily print to printer?Is there a Python Library that isn't complex and can print to printer by promting the user with a printing window? – RafD Apr 04 '20 at 16:17
  • https://stackoverflow.com/questions/12723818/print-to-standard-printer-from-python – rizerphe Apr 04 '20 at 16:23

1 Answers1

0

Fact to Accept: Cross-Platform Printing is hard.

Depending on the system you use, the commands to send text / files to a printer will be very different.

For Windows, you should probably use the Python win32print module, even if you say you don't want to use it because of complexity etc. I honestly think that trying to solve it any other way would be a lot more complicated in the end.

For Linux, Mac, Unix, you can send commands much more directly using LPR to the system printer through the built-in os.popen() or the new subprocess module in Python, but for Windows, my bet is you're better off using the win32print module.

Providing cross-platform printing functionality will always be a challenge because of the differences in the underlying sub-systems on different operating systems.

The basic approach

You'll need to separate the logic of your code, so that depending on the underlying OS, your program will choose the right method for executing the printing functionality you need. I don't know of any way around this.

Using the Python win32 modules needn't be as complex as you might think.

For Windows

This can be done with the module win32print, Documented nicely here

For Linux, macOS, Unix

Check out the use of LPR commands, and combine this with basic Python os.popen calls or using the new Python subprocess module

I know you probably wanted a more "copy/paste friendly" way, but that would be very hard without having the rest of your code and not knowing the exact requirements / specs for your app.

Bottom line is that you'd probably end up writing custom code for each platform for printing anyway, so might as well jump in head first.

C. Sederqvist
  • 2,830
  • 19
  • 27
  • Thank you very much for taking the time to explain that win32print is the best approach for me.I will take your views into account. – RafD Apr 04 '20 at 20:35
  • @RafD no problem. At least using the win32print module is more or less the "default" way for printing on windows from Python programs written without using a framework with its own implementation of a printing / device access abstraction (like Qt), as this requires quite a bit of "behind the scenes" code for wrapping the needed system calls into something manageable from outside the Windows ecosystem. – C. Sederqvist Apr 04 '20 at 21:23