有没有办法将tkinter Entry字段编码为utf-8



我第一次摆弄tkinter,有个问题。有没有一种方法可以对tkinter输入字段进行编码?这是我的代码:

my_username = Entry(window,width=10)
my_username.grid(column=1, row=0)
#a few lines that have nothing to do with username
username = my_username.encode('utf-8')

问题是:

Traceback (most recent call last):
File "project.py", line 34, in <module>
username = my_username.encode('utf-8')
AttributeError: 'Entry' object has no attribute 'encode'

有没有一种正确的方法来编码输入字段?谢谢

不,您不能对小部件本身进行编码,因为编码是一个字符串操作,而小部件不是字符串。但是,您可以对从小部件中获得的数据进行编码。

username = my_username.get().encode('utf-8')

最新更新