python - python 程序可以在 Thonny Python IDE 上的打印机上打印整个窗口,但不能在 LXTerminal 上
问题描述
我在 Raspberry Pi 3B+ 上的 Thonny Python IDE 上开发 Python3 程序。我使用 Tkinter 作为 GUI。
现在,我想编写一个程序,通过按下窗口上的按钮在打印机上打印整个窗口。我已经在 Thonny Python IDE 上取得了成功。但是,当它在 LXTerminal 上运行时,在按下按钮后会出现一条错误消息。我希望它在 LXTerminal 上正常工作。
程序和错误信息如下。我现在该怎么办?
错误信息:
giblib error: Saving to file .screenshot2020-0224_16-01-14-611084.png failed
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "./Projects/tk.py", line 18, in printer
img = pyautogui.screenshot(region=(400,200,800,500))
File "/home/pi/.local/lib/python3.7/site-packages/pyscreeze/__init__.py", line 476, in
_screenshot_linux
im = Image.open(tmpFilename)
File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2634, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: '.screenshot2020-0224_16-01-14-611084.png'
编码:
#!/usr/bin/python3
#-*- coding: utf8 -*-
import tkinter as tk
import sys
import subprocess
from PIL import Image
import io
import pyautogui
def printer():
buf = io.BytesIO()
img = pyautogui.screenshot(region=(400,200,800,500))
img.save(buf, 'PNG')
p = subprocess.Popen('lp', stdin=subprocess.PIPE)
p.communicate(buf.getvalue())
p.stdin.close()
buf.close()
root = tk.Tk()
root.attributes('-type','splash')
root.geometry("400x300")
button = tk.Button(root,text="exit",command=sys.exit)
button.pack()
button2 = tk.Button(root,text="print",command=printer)
button2.pack()
root.mainloop()
解决方案
推荐阅读
- android - 如何在 iOS 上禁用 reCAPTCHA 验证以进行 Firebase 身份验证?
- opencv - 多个应用程序访问同一个 DeckLink 设备(openCV 和 rtmp 推送)?我有什么选择?
- elasticsearch - Elasticsearch - 在多个字段中搜索多个值
- html - 在输入字段上键入时,使用 jquery 或 js 将文本传递到另一个字段
- python - 在 Jetson Nano 终端上不使用“sudo”导入 opencv
- php - Nginx + 别名 + php == 404 文件未找到
- php - PHPExcel 错误:不推荐使用带有大括号的数组和字符串偏移访问语法
- abap - 如何在 ABAP 报告中使用 MB51 Tcode 的字段/表格
- javascript - 在 AJAX/JS 中将 ID 添加到 URL 的正确方法
- angular - 在 Angular 中记住所选页面的最佳方法