To change the foreground
and background
when the widget is inactive (out in focus) then with the help of and binds we can configure the widgets in such a way that they will change their foreground
and background
when they lose focus and when gain focus back.
Practically we can first save original foreground
and background
values of that widget and then use it and callbacks.
Here I've made a class Entry which does exactly you want. I added inactivebackground
and inactiveforeground
configure options.
class Entry(tk.Entry):
def __init__(self, master=None, **kw):
self.inactivebackground = kw.pop('inactivebackground', 'white')
self.inactiveforeground = kw.pop('inactiveforeground', 'black')
super().__init__(master=master, **kw)
self.org_bg = self['background']
self.org_fg = self['foreground']
self.bind('<FocusIn>', self._focusin, '+')
self.bind('<FocusOut>', self._focusout, '+')
self._focusout()
def _focusout(self, evt=None):
self['background'] = self.inactivebackground
self['foreground'] = self.inactiveforeground
def _focusin(self, evt=None):
self['background'] = self.org_bg
self['foreground'] = self.org_fg
Have a look at this example:-
import tkinter as tk
root = tk.Tk()
var = tk.StringVar(value="Hello! How are you doing! :)")
Entry(root, textvariable=var, inactivebackground='pink',
inactiveforeground='blue').pack()
Entry(root, textvariable=var, inactivebackground='orange',
inactiveforeground='red').pack()
root.mainloop()
Similarly, you can modify a Spinbox
to do the same thing. Also just by replacing the inherited class tk.Entry
with ttk.Entry
will work with ttk
style widgets as well but remember not everything is configurable directly with ttk
style widgets.
Power of inheritance
There is one trick you can do to save time and space, by creating a support class that can be inherited along with the desired widget to have the same functionality.
class supportinactive(object):
def __init__(self, inactivebackground, inactiveforeground):
self.inactivebackground = inactivebackground
self.inactiveforeground = inactiveforeground
self.org_bg = self['background']
self.org_fg = self['foreground']
self.bind('<FocusIn>', self._focusin, '+')
self.bind('<FocusOut>', self._focusout, '+')
self._focusout()
def _focusout(self, evt=None):
self['background'] = self.inactivebackground
self['foreground'] = self.inactiveforeground
def _focusin(self, evt=None):
self['background'] = self.org_bg
self['foreground'] = self.org_fg
How to use it?
From the above supportinactive
class we can add this functionality to widget like so
class Entry(tk.Entry, supportinactive):
def __init__(self, master=None, **kw):
inactivebg = kw.pop('inactivebackground', 'white')
inactivefg = kw.pop('inactiveforeground', 'black')
tk.Entry.__init__(self, master=master, **kw)
supportinactive.__init__(self, inactivebg, inactivefg)
# Spinbox will have the same functionality too.
class Spinbox(tk.Spinbox, supportinactive):
def __init__(self, master=None, **kw):
inactivebg = kw.pop('inactivebackground', 'white')
inactivefg = kw.pop('inactiveforeground', 'black')
tk.Spinbox.__init__(self, master=master, **kw)
supportinactive.__init__(self, inactivebg, inactivefg)
If you want to understand how this inheritance is working check out these answers:-