Python - Tkinter 画布中的文本输入

Tha*_*ame 3 python user-input tkinter tkinter-canvas

我想在我用 Tkinter 创建的画布内为整数创建一个用户输入框。我将如何去做这件事?

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()
Run Code Online (Sandbox Code Playgroud)

这是我目前所拥有的,但画布上的任何地方都没有出现输入框。我知道这可能不是在 python 中创建游戏的最有效方式,但我不熟悉任何其他方式。

谢谢你的帮助。

编辑:我已经使用提供的建议更新了代码。我现在有一个问题

IndexError: tuple index out of range
Run Code Online (Sandbox Code Playgroud)

这发生在以下几行

canvas.create_window(window = e1, x=10, y=10)
canvas.create_window(window = e2, x=400, y=10)
Run Code Online (Sandbox Code Playgroud)

编辑:好的,我知道出了什么问题,我不得不删除 x= 和 y= 并且只拥有自己的坐标。现在出现输入框。

Bry*_*ley 7

您可以使用create_window在画布中放置小部件。

e1 = Entry(canvas)
canvas.create_window(400,10,window=e1)
Run Code Online (Sandbox Code Playgroud)