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.
Asked
Active
Viewed 1,901 times
1
-
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 Answers
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
-
1Thankyou Simon for your answer. I have tried this but nothing happens please can you tell me how to solve this – Delalie Rawllings Apr 25 '18 at 16:59
-
1@FredRawllings I think this was a mistake my end. I updated my code. If this still does not work do you get any errors?, does the function actually execute. I need more information (I don't know your code)... – Xantium Apr 25 '18 at 17:06
-
1Thankyou simon your answer update works. But now the problem I have is saving the text to a variable.I first converted it to bytes, I give a print command but the printer prints nothing. – Delalie Rawllings Apr 25 '18 at 17:15
-
-
1No it is not. What I mean it the paper gets into the printer and then gets out with no text printed on it – Delalie Rawllings Apr 25 '18 at 17:25
-
Hmm odd. Is the text is actually stored in the variable before printing? – Xantium Apr 25 '18 at 17:27
-
1Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169787/discussion-between-fred-rawllings-and-simon). – Delalie Rawllings Apr 25 '18 at 17:29
-
1Thank you simon after some little research I got it to work. Your help was really awesome – Delalie Rawllings Apr 25 '18 at 18:00
-
Sorry Simon I taught I got the answer but I did not. This worked just once but refused to work the next time i run it here is my code – Delalie Rawllings Apr 25 '18 at 19:22
-
1def to_printer(): lpr = subprocess.Popen("/usr/bin/lpr", stdin=subprocess.PIPE) lpr.stdin.write(text2) to_printer() – Delalie Rawllings Apr 25 '18 at 19:22
-
@FredRawllings Please see in [Chat](https://chat.stackoverflow.com/rooms/169787/discussion-between-fred-rawllings-and-simon) – Xantium Apr 25 '18 at 19:30
-