.get() not working correctly with entry boxes in tkinter

吃可爱长大的小学妹 提交于 2021-02-02 09:59:09

问题


game_entry = Entry(gs, width = 10, bg = 'white')
game_entry.grid(row=4, column = 0, sticky=W)
ip = game_entry.get()
part1 = partial(click2, ip)
Button(gs, text = "Submit", width = 6, command = part1, bg='white').grid(row=5, column = 0, sticky=W)
output = Text(gs, width = 25, height = 1, wrap=WORD,bg = 'white')
output.grid(row = 7,column=0,sticky=W)

def click2(a):
    if a == 'a':
        print('hello')

If I click the button and the entry box says 'a' (without the quote marks), nothing gets printed and if i define click2 as:

def click2(a):
    if a != 'a':
        print('hello')

and type 'a' (again, without quote marks), it does print it even thought it shouldn't.

Does anyone know why?

Thanks


回答1:


The problem is because you get the value of the entry widget about a millisecond after you create the entry widget. You need to wait until the user clicks the button before you call the .get() method.



来源:https://stackoverflow.com/questions/46654408/get-not-working-correctly-with-entry-boxes-in-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!