Canvasvg模块错误

时间:2017-05-24 20:39:04

标签: python turtle-graphics attributeerror traceback 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()

感谢所有帮助。

1 个答案:

答案 0 :(得分:0)

我的猜测是你的问题来自这些方面:

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

我的递归错误处理也存在问题,但编程样式并不影响代码的行为。