0

Hi why does this program return the error? responseImagePath contains the exact file path of a local image. When i copy the print of responseImagePath directly into the PhotoImage, there is no such error. What am i doing wrong?

class Assistant():

    def __init__(self, master):

        self.label = ttk.Label(master)
        self.label.pack()
        self.button = Button(master, command = self.getResponse)
        self.button.pack()


    def getResponse(self):

        message = assistant.message(

            '89635700-591b-4f58-8345-409e08cef531',

            session,

            input={'text': 'picture'},

            ).get_result()

        res = message["output"]["generic"][0]["text"]
        print("Answer: " + res)    

        responseImagePath =  message["output"]["generic"][1]["source"]
        print("Response Image src: " + responseImagePath)
        responseImage = PhotoImage(file = responseImagePath)
        self.label.config(image = responseImage)

The print is -> Response Image src: r'C:\Users\Nathan\Desktop\test\resImage.gif' Error is -> _tkinter.TclError: couldn't open "r'C:\Users\Nathan\Desktop\test\resImage.gif'": no such file or directory

  • You should be seeing `Response Image src: C:\Users\Nathan\Desktop\test\resImage.gif` printed, so I think the string value of `responseImagePath` is incorrect. An `r` prefix on strings generally is only used when _defining_ string literals in source code. – martineau Jun 11 '20 at 14:37
  • 1
    Read also [why-does-tkinter-image-not-show-up-if-created-in-a-function](https://stackoverflow.com/questions/16424091) – stovfl Jun 11 '20 at 14:42

1 Answers1

2

Take a close look at the error message, it is literally telling you the problem:

couldn't open "r'C:\Users\Nathan\Desktop\test\resImage.gif'": no such file or directory

It's telling you it is looking for a file where the first letter of the name is r, followed by a single quote, followed by the letter C, etc.

assistant.message(...).get_result() appears to be returning an incorrect path with extra characters in it.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thank you for your answer, i changed the assistant response to just C:\Users\Nathan\Desktop\test\resImage.gif without any quotes and it works now. – user13107709 Jun 11 '20 at 15:35