【发布时间】:2020-08-20 17:46:39
【问题描述】:
我正在制作一个程序来帮助我在学校讲座期间快速截取屏幕截图。我告诉 pyautogui 将屏幕截图保存到一个目录,但它没有将它保存在那里,而是将它保存在脚本所在的文件夹中。它也没有'不要给出任何错误。只是没有将它保存到我想要的路径。 我的代码:-
import tkinter as tk
import pyautogui
import keyboard
from datetime import date
from tkinter import filedialog
r = tk.Tk()
r.geometry("600x300")
r.configure(background="white")
def startss():
while True:
if keyboard.is_pressed("insert"):
with open("chemistry.txt","r") as a:
b = a.read()
with open("chemistry.txt","w") as c:
newval = int(b) + 1
c.write(str(newval))
todaydate = date.today()
name = "Chemistry-" + str(b) + "-" + str(todaydate) + ".jpeg"
myScreenshot = pyautogui.screenshot(name)
myScreenshot.save(r"C:\Users\Hp\Desktop\Maheer\Study\Chemistry")
heading = tk.Label(r,text="Study-Screenshotter",fg="grey",bg="white",font="Arial 20 bold")
heading.place(relx=0.5,rely=0,anchor="n")
ctext = tk.Label(r,text="Chemistry=F1",fg="black",bg="white",font="Arial 15 bold")
ctext.place(relx=0.11,rely=0.25,anchor="n")
btext = tk.Label(r,text="Biology=F2",fg="black",bg="white",font="Arial 15 bold")
btext.place(relx=0.9,rely=0.25,anchor="n")
ptext = tk.Label(r,text="Physics=F3",fg="black",bg="white",font="Arial 15 bold")
ptext.place(relx=0.1,rely=0.45,anchor="n")
mtext = tk.Label(r,text="Maths=F4",fg="black",bg="white",font="Arial 15 bold")
mtext.place(relx=0.9,rely=0.45,anchor="n")
etext = tk.Label(r,text="English=F4",fg="black",bg="white",font="Arial 15 bold")
etext.place(relx=0.5,rely=0.65,anchor="n")
startbutton = tk.Button(r,text="Start",fg="black",bg="white",font="Arial 15 bold",command=startss)
startbutton.place(relx=0.5,rely=0.8,anchor="n")
r.mainloop()
Tkinter 回调异常 回溯(最近一次通话最后): 文件“C:\Users\Hp\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PIL\Image.py”,第 2138 行,保存 格式=扩展[分机] 关键错误:''
上述异常是以下异常的直接原因:
Traceback (most recent call last):
File "C:\Users\Hp\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "c:\Users\Hp\Desktop\Maheer\Study\Study-Screenshotter.py", line 25, in startss
myScreenshot.save(rpath)
File "C:\Users\Hp\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PIL\Image.py", line 2140, in save
raise ValueError("unknown file extension: {}".format(ext)) from e
ValueError: unknown file extension:
如果我尝试使用双斜杠或其他模块,例如 os.path,我会收到此错误
【问题讨论】:
-
试试说
myScreenshot.save(r"C://Users//Hp//Desktop//Maheer//Study//Chemistry")?并尝试删除r -
试过了,但是报错了
-
os模块可能有一些选择路径的方法?我相信你进入道路的方式是错误的。尝试使用filedialog询问目录? -
我之前做过filedialog,它工作正常,但这个程序的重点是截取屏幕截图并立即保存,而无需我进入带有文件对话框的文件夹并保存
-
那你输入路径的方式有问题,我猜