0

I use code from this question for printing to thermal EPSON TM-T82 printer but when I modified it with tkinter and mainloop() python not direcly printout my data until I close my layout GUI. I want this script can print out my data without I close layout GUI.

This is my code:

import subprocess
from Tkinter import *

class tes_print:
   def __init__(self):
       self.printData()

   def printData(self):
       data = " MY TEXT "
       lpr = subprocess.Popen("/usr/bin/lpr", stdin=subprocess.PIPE, shell=True)
       lpr.stdin.write(data)

root = Tk()
app2 = tes_print()
root.mainloop()
Community
  • 1
  • 1
eM. Key
  • 21
  • 4
  • The `shell=True` is not doing anything useful there. You don't need a shell to run a single command (though you'll need to put `lpr` in a list: `subprocess.Popen(['lpr'], stdin=subprocess.PIPE)` – tripleee Sep 26 '16 at 06:05
  • Your code does not call `printData`, I would not expect it to write anything. – Stop harming Monica Sep 26 '16 at 06:58

1 Answers1

0

You are experiencing buffering. The write will write to a buffer which is not actually passed on to lpr until it fills up or you explicitly flush it.

Running lpr.communicate() will flush it and allow lpr to run to completion.

lpr = subprocess.Popen(['lpr'], stdin=subprocess.PIPE)
stdout, stderr = lpr.communicate(input=data)
tripleee
  • 175,061
  • 34
  • 275
  • 318