Python Forum
[Tkinter] How to print data to the printer - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] How to print data to the printer (/thread-27040.html)

Pages: 1 2


How to print data to the printer - scratchmyhead - May-23-2020

Does anyone know how to make your program print data from my program to the printer?


RE: How to print data to the printer - DT2000 - May-23-2020

As an example you could use the following:
    printText = your_widget_namet.get("1.0", END)
    print(printText)
    filename = tempfile.mktemp(".txt")
    open(filename, "w").write(printText)
    # Bellow is call to print text from your_widget_name textbox
    win32api.ShellExecute(0,"printto", filename, '"%s"' % win32print.GetDefaultPrinter(), ".", 0)



RE: How to print data to the printer - scratchmyhead - May-23-2020

OK. I'll try thanks.


RE: How to print data to the printer - scratchmyhead - May-26-2020

That didn't work.


RE: How to print data to the printer - DT2000 - May-26-2020

Please show your complete code and any errors received in the appropriate tags so it can be analysed, that will make it easier to see what you have done and what might be your issue.


RE: How to print data to the printer - scratchmyhead - May-27-2020

I get the following error code: NameError: name 'tempfile' is not defined


printText =Label(root, text="hello")
print(printText)
filename = tempfile.mktemp(".txt")
open(filename, "w").write(printText)
# Bellow is call to print text from your_widget_name textbox
win32api.ShellExecute(0,"printto", filename, '"%s"' % win32print.GetDefaultPrinter(), ".", 0)



RE: How to print data to the printer - menator01 - May-27-2020

Might be worth a look.
https://pypi.org/project/python-printer-escpos/


RE: How to print data to the printer - scratchmyhead - May-28-2020

I'm getting closer but still not working. I went to that link and installed all components. But when I try running the following code, I get this error: ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it


printer = getNetworkPrinter()(host='192.168.0.1', port=8080)

printer.text("Hello World")
printer.lf()



RE: How to print data to the printer - menator01 - May-28-2020

Are you going through a router? If so you may have to open a port to the printer.


RE: How to print data to the printer - scratchmyhead - May-28-2020

Yes the printer goes through a router.