为什么itemconfig显示数字而不是变量?

问题描述 投票:0回答:1

我试图创建一个输入字段并将其保存在变量中以将其放入Canvas中,但是当我输入文本并提交它时,会出现很多数字。我不明白为什么。

Before I type and submit text

After I type and submit a text

那是我的代码:

from tkinter import *

fen=Tk()
can=Canvas(fen, width=600, height=400, background="light pink")
def rep():
 reponse.get()
 can.itemconfig(banque,text= reponse)
banque=can.create_text(300,200, text="Nothing")
reponse=Entry(fen)
reponse.pack()
b=Button(fen, text="Submit",command=rep)
b.pack()
can.pack()
fen.mainloop()
python canvas tkinter
1个回答
1
投票

更换:

def rep():
 reponse.get()
 can.itemconfig(banque,text= reponse)

有:

def rep():
    can.itemconfig(banque, text=reponse.get())

由于reponse仍然是对Entry的引用,str具有有效的entry_widget.get()表示,可以用作要显示的字符串值。

另外,entry_widget返回一个字符串。它本身不会修改任何东西,它只是返回在Tcl中写入的字符串。

您看到的数字是qazxswpoi解释器中对条目对象的引用。

© www.soinside.com 2019 - 2024. All rights reserved.