【问题标题】:How print to printer with Popen and GUI tkinter如何使用 Popen 和 GUI tkinter 打印到打印机
【发布时间】:2023-04-08 08:01:01
【问题描述】:

我使用来自this question 的代码打印到热EPSON TM-T82 打印机,但是当我使用tkintermainloop() 修改它时,python 不会直接打印出我的数据,直到我关闭我的布局GUI。我希望这个脚本可以在不关闭布局 GUI 的情况下打印出我的数据。

这是我的代码:

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()

【问题讨论】:

  • shell=True 在那里没有做任何有用的事情。您不需要 shell 来运行单个命令(尽管您需要将 lpr 放入列表中:subprocess.Popen(['lpr'], stdin=subprocess.PIPE)
  • 你的代码没有调用printData,我不希望它写任何东西。

标签: python printing tkinter


【解决方案1】:

您正在经历缓冲。 write 将写入一个缓冲区,该缓冲区实际上不会传递给 lpr,直到它填满或您明确刷新它。

运行lpr.communicate() 将刷新它并允许lpr 运行完成。

lpr = subprocess.Popen(['lpr'], stdin=subprocess.PIPE)
stdout, stderr = lpr.communicate(input=data)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    相关资源
    最近更新 更多