简体   繁体   中英

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

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM