How to remove focus out of a Text widget in Tkinter, Python

耗尽温柔 提交于 2021-01-07 03:41:43

问题


By placing a Text widget in my GUI

from Tkinter import Text
textBox=Text(root, height=20, width=10)
textBox.pack()

whenever I write something in that box, I cant return the focus back to any other place in the window. I have some keys bounded to event, which stop working after I wrote in the Text widget.

Is there a way of redirecting the focus to another place after writing text?


回答1:


Is there a way of redirecting the focus to another place after writing text?

Every widget has a method named focus_set which can be used to move keyboard focus to that widget.

For example, to set the focus to the root window you would do:

root.focus_set()



回答2:


Please press Return-Key to give focus back to window

import tkinter as tk


def onReturn(*event):

    root.focus_set()
    
    
root = tk.Tk()

textBox= tk.Text(root, height=20, width=10)
textBox.pack()

root.bind("<Return>", onReturn)

root.mainloop()


来源:https://stackoverflow.com/questions/55583093/how-to-remove-focus-out-of-a-text-widget-in-tkinter-python

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