Python Tkinter 输入小部件不接受输入 [英] Python Tkinter entry widget won´t accept input

查看:44
本文介绍了Python Tkinter 输入小部件不接受输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个关于 Tkinter 条目小部件的非常奇怪的问题.当我尝试向他们输入内容时,他们不接受我的输入.

I have run into a very weird Problem with Tkinter entry widgets. When I try to enter something into them they don´t accept my input.

在一些 PC 重新启动和 Python 重新安装后,我想出了为什么会发生这种情况:我在代码中的 root.mainloop() 之前有一个消息框.代码如下所示:

After some PC restarting and Python reinstalling I figured out why this happens: I had a messagebox just before the root.mainloop() in the code. The code looks something like this:

def xyz():
    if not messagebox.askyesno("Title","Some text"):
        exit()
xyz()
root.mainloop()

我发现,要解决此问题,您只需手动将焦点放在不同的窗口上,然后再返回即可.我想知道是否有更好的方法来做到这一点?我想保留我的消息框,并且不想要手动更改窗口焦点的不雅解决方案.

I found, to resolve the issue you can just manually focus on a different window and then back again. I would like to know if there is some better way to do this? I would like to keep my messagebox, AND dont´t want the unelegant solution of manually changing window focus.

推荐答案

您可以像这样修复代码:

You can fix the code like this:

def xyz():
    if not messagebox.askyesno("Title","Some text"):
        exit()
root.after(10,xyz) #show the messagebox after root.mainloop()
root.mainloop()

这篇关于Python Tkinter 输入小部件不接受输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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