S.McWhorter
S.McWhorter

Reputation: 151

Tkinter: How to stop typing in an Entry widget?

Basically I want to be able to type something in an entry widget and when I am done typing I want to be able to click anywhere on the application to stop typing. As of right now it wants me to constantly type something into the entry box. Anyone know of a way to stop this?

import tkinter as tk

class window2:
    def __init__(self, master1):
        self.panel2 = tk.Frame(master1)
        self.panel2.grid()
        self.button1 = tk.Button(self.panel2,text="Button")
        self.button1.grid()
        self.text1 = tk.Entry(self.panel2)
        self.text1.grid()
        self.text1.focus()

root1 = tk.Tk()
root1.geometry("750x500")
window2(root1)
root1.mainloop()

Upvotes: 0

Views: 743

Answers (1)

Mike - SMT
Mike - SMT

Reputation: 15236

I would build this as an inherited class for Tk and then bind mouse button 1 to change focus to whatever widget was clicked.

import tkinter as tk

class window2(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry("750x500")
        panel2 = tk.Frame(self)
        panel2.grid()
        tk.Button(panel2,text="Button").grid()
        text1 = tk.Entry(panel2)
        text1.grid()
        text1.focus()
        self.bind("<1>", self.set_focus)

    def set_focus(self, event=None):
        x, y = self.winfo_pointerxy()
        self.winfo_containing(x, y).focus()


window2().mainloop()

Upvotes: 1

Related Questions