.get() 无法与 tkinter 中的输入框一起正常工作 [英] .get() not working correctly with entry boxes in tkinter

查看:22
本文介绍了.get() 无法与 tkinter 中的输入框一起正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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')

如果我单击按钮并且输入框显示a"(不带引号),则不会打印任何内容,如果我将 click2 定义为:

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')

并输入'a'(同样,没有引号),它确实打印了它,即使认为它不应该.

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

有人知道为什么吗?

谢谢

推荐答案

问题是因为您在创建条目小部件后大约一毫秒获得了条目小部件的值.需要等到用户点击按钮后才能调用.get() 方法.

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.

这篇关于.get() 无法与 tkinter 中的输入框一起正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆