简体   繁体   中英

Tkinter: How do I create an entry box that highlights as a user hovers their mouse over it?

In Tkinter, I would like to create an Entry box that highlights when the user hovers their mouse over it. I have tried the code below but unfortunately it hasn't worked; the entry box only highlights when I click on it.

import tkinter as tk
import tkinter.ttk as ttk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        entry_style = ttk.Style()
        entry_style.map('TEntry', highlightcolor=[("active", "green")])
        entry = ttk.Entry(self)
        entry.pack()


app = App()
app.mainloop()

I am running Python 3.8.2 on iOS Catalina.

Thanks in advance.

If you are not set into using ttk , the plain tk approach is to bind the events '<Enter>' and '<Leave>' to a configuration setting:

import tkinter as tk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        entry = tk.Entry(self)
        entry.pack()
        entry.bind('<Enter>', lambda dummy_event: entry.config(bg='green'))
        entry.bind('<Leave>', lambda dummy_event: entry.config(bg='white'))

app = App()
app.mainloop()

The aqua theme is very rigid and does not allow changes to its theme.

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