python程序可以在Thonny Python IDE的打印机上打印整个窗口,但不能在LXTerminal

时间:2020-02-24 08:15:30

标签: python tkinter

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

0 个答案:

没有答案