Canvasvg模块错误
问题描述:
我在写代码来保存和合并用乌龟模块制作的图像,但是当我去保存图像时,一个错误不断出现;我认为关于Canvasvg模块本身?它可能是安装不正确,如果是的话,我该怎么做才能正确?Canvasvg模块错误
错误代码:
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\Python Program\lib\idlelib\run.py", line 137, in main
seq, request = rpc.request_queue.get(block=True, timeout=0.05)
File "D:\Python Program\lib\queue.py", line 172, in get
raise Empty
queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Python Program\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "D:\Python Program\lib\turtle.py', line 686, in eventfun
fun()
File "C:\Users\garla\Desktop\tst.py", line 75, in saveImg
canvasvg.saveall(ts , namesav)
File "D:\Python Program\lib\canvasvg\canvasvg.py', line 337, in saveall
for element in convert(doc, canvas, items, tounicode):
File "D:\Python Program\lib\canvasvg\canvasvg.py", line 84, in convert
tk = canvas.tk
AttributeError: 'str' object has no attribute 'tk'
这里是它采用canvasvg代码:
def saveImg() :
print("Done.")
save = input("Would you like to save this ? Y/N \n")
if save.upper() == "Y" :
Red.hideturtle()
Blue.hideturtle()
name = input("File Name :")
namesav = name + " .jpg"
ts = turtle.getscreen() .getcanvas()
canvasvg.saveall(ts , namesav)
elif save.upper() == "N" :
def runChk() :
runAgain = input("Would you like to run again? Y?N (N will Exit)")
if runAgain.upper() == "Y" :
print("Running")
main()
elif runAgain.upper() == "N" :
print ("Exiting...")
exit()
else :
print("Invalid response.")
runChk()
runChk()
else :
print("Invalid Response.")
saveImg()
所有帮助表示赞赏。
答
我的猜测是你的问题来自于这些行:
namesav = name + ".jpg"
ts = turtle.getscreen().getcanvas()
canvasvg.saveall(ts, namesav)
我看到两个问题。第一个问题是saveall()
方法接受它的参数以相反的顺序比你给了他们:
saveall(filename, canvas, items=None, margin=10, tounicode=None)
的第二个问题是,你所使用的扩展".jpg"
当这个代码将会给你回".svg"
文件。该canvasvg模块用于Save Tkinter Canvas in SVG file
此外,我相信您的电话runChk()
缩进不正确。返工代码:
def saveImg():
print("Done.")
save = input("Would you like to save this? Y/N: ")
if save.upper() == "Y":
Red.hideturtle()
Blue.hideturtle()
name = input("File Name: ")
namesav = name + ".svg"
ts = turtle.getscreen().getcanvas()
canvasvg.saveall(namesav, ts)
elif save.upper() == "N":
def runChk():
runAgain = input("Would you like to run again? Y/N (N will Exit): ")
if runAgain.upper() == "Y":
print("Running")
main()
elif runAgain.upper() == "N":
print("Exiting...")
exit()
else:
print("Invalid response.")
runChk()
runChk()
else:
print("Invalid Response.")
saveImg()
我也有一个问题,你的递归错误处理,但是这是编程风格和不影响你的代码的行为的东西。
避免将图像用于错误和代码,因为它很难复制您的问题! –
@AP。,我做了一个错误消息和代码的在线OCR,并手工清理它 - 任何错字都可能是我的。大多数情况下,我很好奇在线OCR的工作情况。 – cdlane