-1

When I run this code(part of a larger project):

def Initialize (event,tk):
    event.delete("all")
    parch = tk.PhotoImage(file = "Parchment.png" )
    parchment = event.create_image(175, 25, image = parch, anchor = "nw")
    parchment.place(175, 25)`

Which gives the error:

parchment.place(175, 25)
AttributeError: 'int' object has no attribute 'place'

Technically, it all works fine and does its job; but having error messages is not really good either way.

Nae
  • 14,209
  • 7
  • 52
  • 79
Salad
  • 9
  • 2
  • 4
    Apparently `parchment` is a number, which doesn't have a `place` method. Check the docs to see what `create_image` returns. Are you sure the method doesn't just mutate `parch`? In that case, change it to `parch.place(175, 25)`. – Carcigenicate Dec 29 '17 at 13:36
  • 2
    `create_image` and other `create_*` don't create widgets but objects on canvas which don't have methods `place()`/`grid()`/ `pack()` because they don't need them (because they are not widgets). – furas Dec 29 '17 at 13:49
  • I understand they don't need those methods, nonetheless without those methods the desired image just does not show up – Salad Dec 29 '17 at 17:05
  • @Salad Your image probably doesn't show up because it lacks a global reference. See [this post](https://stackoverflow.com/q/3359717/7032856). Or you can simply make `parchment.image = parch`. – Nae Dec 29 '17 at 17:11
  • Thank you for the effort, but it's no good, I guess I'll just find a workaround, or solve it a later date. – Salad Dec 29 '17 at 17:37
  • I'd try `event.parch_img = parch`. – Nae Dec 29 '17 at 17:40
  • @nae you are a genius sir, although the solution is much simpler, I figured it out thanks to you. I merely had to globalise the variable which declared which file would be used for the image. – Salad Dec 29 '17 at 18:44
  • 1
    It's very strange that you have a parameter named `event` which is a canvas rather than an event object. It makes your code hard to understand. – Bryan Oakley Dec 29 '17 at 19:36
  • yes, i know. its a habit to use it. And here I mainly wanted something easy to remember for me so I could use it without having to think too hard about it – Salad Dec 29 '17 at 20:05
  • 2
    @Salad - please DO NOT include answer into the question - feel free to post solution as an actual answer if you want to (I rolled back your edit that adds solution). Side note: this question is [discussed on meta](https://meta.stackoverflow.com/questions/361267/why-are-complete-question-deviation-making-answers-effectively-meaningless-is) which may bring additional attention to the post. – Alexei Levenkov Jan 01 '18 at 00:24

1 Answers1

2

As you can see here, the create_image method returns the integer ID number of the image object it creates. All the canvas object creation methods return such an ID.

You can use canvas methods to interact with the objects given their ID (e.g. move them using the .move method).

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • 1
    I think the question above this needs a roll-back edit, for this answer to be more related. – Nae Dec 31 '17 at 12:59
  • The stuff after the edit on how the image is "lost" is a completely different question, really. – Roland Smith Dec 31 '17 at 15:12
  • Yes, I agree. I [tried suggesting](https://meta.stackoverflow.com/q/361267/7032856) the previous edit. But apparently, it's not exactly the same as roll-back and was judged as if I was the one changing the question. – Nae Dec 31 '17 at 15:16