How print to printer with Popen and GUI tkinter
<p>I use code from <a href="http://stackoverflow.com/questions/12723818/print-to-standard-printer-from-python">this question</a> for printing to thermal EPSON TM-T82 printer but when I modified it with <code>tkinter</code> and <code>mainloop()</code> 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.</p>

<p>This is my code:</p>

<pre><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()
</code></pre>

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

<p>Running <code>lpr.communicate()</code> will flush it and allow <code>lpr</code> to run to completion.</p>

<pre><code>lpr = subprocess.Popen(['lpr'], stdin=subprocess.PIPE)
stdout, stderr = lpr.communicate(input=data)
</code></pre>

python printing tkinter