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