Python - Text input within a Tkinter canvas

ThatsNotMyName picture ThatsNotMyName · Jul 2, 2013 · Viewed 11.4k times · Source

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.

Answer

Bryan Oakley picture Bryan Oakley · Jul 2, 2013

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

e1 = Entry(canvas)
canvas.create_window(400,10,window=e1)