Python - Text input within a Tkinter canvas

13,436

Solution 1

Ok I did some more reading up on what a tuple actually is and I was able to fix the error.

Just had to remove the x= and y= so that the coordinates of the window are by themselves.

Solution 2

You can use create_window to place a widget within a canvas.

e1 = Entry(canvas)
canvas.create_window(400,10,window=e1)
Share:
13,436

Related videos on Youtube

ThatsNotMyName
Author by

ThatsNotMyName

UoA CompSci Graduate/Stats Major

Updated on June 04, 2022

Comments

  • ThatsNotMyName
    ThatsNotMyName almost 2 years

    I want to create a userinput box for integers inside a canvas I created with Tkinter. How I would go about doing this?

    def gamescreen():
        photo = PhotoImage(file="gamescreen.gif")
        canvas.bind("<Button-1>", buttonclick_gamescreen)
        canvas.pack(expand = YES, fill = BOTH)
        canvas.create_image(1, 1, image = photo, anchor = NW)
        e1 = Entry(canvas)
        e2 = Entry(canvas)
        game1 = PhotoImage(file="1.gif")
        canvas.create_image(30, 65, image = game1, anchor = NW)
        canvas.create_window(window = e1, x=10, y=10)
        canvas.create_window(window = e2 , x=400, y=10)    
        canvas.update()
        window.mainloop()
    

    This is what I currently have but a entry box doesn't appear anywhere on the canvas. I know this probably isn't the most efficient way to create a game in python but I'm not familiar with any other way.

    Thanks for your help.

    EDIT: I have updated the code with the suggestions provided. I now have an issue with

    IndexError: tuple index out of range
    

    This is occurring at the lines below

    canvas.create_window(window = e1, x=10, y=10)
    canvas.create_window(window = e2, x=400, y=10)
    

    EDIT: Okay I figured out what was wrong, I had to remove the x= and y= and just have the coordinates by themselves. The entry boxes now appear.

    • Dan Doe
      Dan Doe almost 11 years
      Just based on your code, if you're looking to make a game you should really check out Pygame (pygame.org).
  • ThatsNotMyName
    ThatsNotMyName almost 11 years
    Thanks for your answer, I was able to make the input box appear but now seems to replace the images that were placed onto the canvas, I'll keep working on it to find a solution. I have window and canvas defined in the main subroutine
  • Russell Smith
    Russell Smith almost 11 years
    pack almost certainly won't do what the OP wants -- it doesn't place the entry on the canvas. It will create the canvas to shrink to fit the entry widget, and the entry widget won't scroll along with the contents of the canvas.
  • ThatsNotMyName
    ThatsNotMyName almost 11 years
    Thanks for your answer but I seem to be running into another error. This time it displays "IndexError: tuple index out of range" I searched up the error message here and on google but didn't find anything related. :/
  • Reut Sharabani
    Reut Sharabani over 9 years
    Please fix your answer.
  • Russell Smith
    Russell Smith over 9 years
    @ReutSharabani: fixed. Thanks for letting me know about the mistake.
  • Reut Sharabani
    Reut Sharabani over 9 years
    this helped me. Thanks.