首页 > 解决方案 > 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()

标签: pythontkinter

解决方案


推荐阅读