-1

I'm writing code to save and combine images made with the turtle module, however when I go to save the image an error keeps appearing; I think in relation to the Canvasvg module itself? Could it have been installed incorrectly, and if so how can I do it correctly?

Error code:

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'

Here's the code which uses 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()

All help is appreciated.

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • Avoid using images for Errors and code, as it makes it very hard to replicate your issue! – AP. May 24 '17 at 20:44
  • 1
    @AP., I did an online OCR of the error messages and code and hand cleaned it up -- any typos are likely mine. Mostly I was curious how well online OCR worked. – cdlane May 24 '17 at 22:35

1 Answers1

0

My guess is your issues come from these lines:

namesav = name + ".jpg"

ts = turtle.getscreen().getcanvas()

canvasvg.saveall(ts, namesav)

I see two issues. The first issue is the saveall() methods takes its arguments in the opposite order than you gave them:

saveall(filename, canvas, items=None, margin=10, tounicode=None)

The second issue is that you used the extension ".jpg" when this code will be giving you back a ".svg" file. The canvasvg module is used to Save Tkinter Canvas in SVG file

Additionally, I believe one of your calls to runChk() is indented incorrectly. The reworked code:

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

I also have an issue with your recursive error handling but that's programming style and not something affecting your code's behavior.

cdlane
  • 40,441
  • 5
  • 32
  • 81