1

I am currently building a tkinter text editing application with python. I'd love to have a function that enables text to be printed out on paper like one will do one Microsoftword and other text editors, if a printer is connected, but I do not know how to go about solving this.

  • You could research for some things first. Also your problem is not summared in the title. It's not clear from your question if you want to create a button or a function to print a file. – Jakub Bláha Apr 25 '18 at 13:01
  • Thankyou Jakub.I think is a function I want to create – Delalie Rawllings Apr 25 '18 at 14:07
  • Take a look at here http://timgolden.me.uk/python/win32_how_do_i/print.html – Jakub Bláha Apr 25 '18 at 14:11
  • I have edited your question. Honestly, it was not clear what you wanted to print. Please, re-edit the question if my changes do not reflect your needs. – nbro Apr 25 '18 at 14:11

1 Answers1

2

Part of your problem:

Using this answer should work.

So create a function with a variable to store your text in (or read from a file and then pass it as a variable) and just call your function with a simple button:

def to_printer(text):
    import subprocess
    lpr =  subprocess.Popen("/usr/bin/lpr", stdin=subprocess.PIPE)
    lpr.stdin.write(text)

print_button = tk.Button(text="print", command=to_printer)
print_button.pack()
Xantium
  • 11,201
  • 10
  • 62
  • 89