try:
from Tkinter import Entry, Frame, Label, StringVar
from Tkconstants import *
except ImportError:
from tkinter import Entry, Frame, Label, StringVar
from tkinter.constants import *
def hex2rgb(str_rgb):
try:
rgb = str_rgb[1:]
if len(rgb) == 6:
r, g, b = rgb[0:2], rgb[2:4], rgb[4:6]
elif len(rgb) == 3:
r, g, b = rgb[0] * 2, rgb[1] * 2, rgb[2] * 2
else:
raise ValueError()
except:
raise ValueError("Invalid value %r provided for rgb color."% str_rgb)
return tuple(int(v, 16) for v in (r, g, b))
class Placeholder_State(object):
__slots__ = 'normal_color', 'normal_font', 'placeholder_text', 'placeholder_color', 'placeholder_font', 'with_placeholder'
def add_placeholder_to(entry, placeholder, color="grey", font=None):
normal_color = entry.cget("fg")
normal_font = entry.cget("font")
if font is None:
font = normal_font
state = Placeholder_State()
state.normal_color=normal_color
state.normal_font=normal_font
state.placeholder_color=color
state.placeholder_font=font
state.placeholder_text = placeholder
state.with_placeholder=True
def on_focusin(event, entry=entry, state=state):
if state.with_placeholder:
entry.delete(0, "end")
entry.config(fg = state.normal_color, font=state.normal_font)
state.with_placeholder = False
def on_focusout(event, entry=entry, state=state):
if entry.get() == '':
entry.insert(0, state.placeholder_text)
entry.config(fg = state.placeholder_color, font=state.placeholder_font)
state.with_placeholder = True
entry.insert(0, placeholder)
entry.config(fg = color, font=font)
entry.bind('<FocusIn>', on_focusin, add="+")
entry.bind('<FocusOut>', on_focusout, add="+")
entry.placeholder_state = state
return state
class SearchBox(Frame):
def __init__(self, master, entry_width=30, entry_font=None, entry_background="white", entry_highlightthickness=1, button_text="Search", button_ipadx=10, button_background="#009688", button_foreground="white", button_font=None, opacity=0.8, placeholder=None, placeholder_font=None, placeholder_color="grey", command=None):
Frame.__init__(self, master)
self._command = command
self.entry_var = StringVar()
self.entry = Entry(self, textvariable=self.entry_var, width=entry_width, background=entry_background, highlightcolor=button_background, highlightthickness=entry_highlightthickness)
self.entry.pack(side=LEFT, fill=BOTH, expand=True, ipady=1)
if entry_font:
self.entry.configure(font=entry_font)
if placeholder:
add_placeholder_to(self.entry, placeholder, color=placeholder_color, font=placeholder_font)
if command is not None:
self.entry.bind("<Return>",self._execute_command)
r,g,b = hex2rgb(button_background)
r = int(opacity*r)
g = int(opacity*g)
b = int(opacity*b)
self._button_activebackground = '#%02x%02x%02x' % (r,g,b)
self._button_background = button_background
self.button_label = Label(self, text=button_text, background=button_background, foreground=button_foreground)
if entry_font:
self.button_label.configure(font=button_font)
self.button_label.pack(side=LEFT, fill=Y, ipadx=button_ipadx)
self.button_label.bind("<Enter>", self._state_active)
self.button_label.bind("<Leave>", self._state_normal)
self.button_label.bind("<ButtonRelease-1>", self._execute_command)
def get(self):
return self.entry_var.get()
def set(self, text):
self.entry_var.set(text)
def clear(self):
self.entry_var.set("")
def focus(self):
self.entry.focus()
def _execute_command(self, event):
text = self.entry_var.get()
self._command(text)
def _state_normal(self, event):
self.button_label.configure(background=self._button_background)
def _state_active(self, event):
self.button_label.configure(background=self._button_activebackground)
if __name__ == "__main__":
try:
from Tkinter import Tk
from tkMessageBox import showinfo
except ImportError:
from tkinter import Tk
from tkinter.messagebox import showinfo
def command(text):
showinfo("search command", "searching:%s"%text)
root = Tk()
SearchBox(root, command=command, placeholder="Type and press enter", entry_highlightthickness=0).pack(pady=6, padx=3)
root.mainloop()
Diff to Previous Revision
--- revision 4 2017-04-04 05:05:52
+++ revision 5 2017-04-04 05:17:21
@@ -20,8 +20,50 @@
return tuple(int(v, 16) for v in (r, g, b))
+class Placeholder_State(object):
+ __slots__ = 'normal_color', 'normal_font', 'placeholder_text', 'placeholder_color', 'placeholder_font', 'with_placeholder'
+
+def add_placeholder_to(entry, placeholder, color="grey", font=None):
+ normal_color = entry.cget("fg")
+ normal_font = entry.cget("font")
+
+ if font is None:
+ font = normal_font
+
+ state = Placeholder_State()
+ state.normal_color=normal_color
+ state.normal_font=normal_font
+ state.placeholder_color=color
+ state.placeholder_font=font
+ state.placeholder_text = placeholder
+ state.with_placeholder=True
+
+ def on_focusin(event, entry=entry, state=state):
+ if state.with_placeholder:
+ entry.delete(0, "end")
+ entry.config(fg = state.normal_color, font=state.normal_font)
+
+ state.with_placeholder = False
+
+ def on_focusout(event, entry=entry, state=state):
+ if entry.get() == '':
+ entry.insert(0, state.placeholder_text)
+ entry.config(fg = state.placeholder_color, font=state.placeholder_font)
+
+ state.with_placeholder = True
+
+ entry.insert(0, placeholder)
+ entry.config(fg = color, font=font)
+
+ entry.bind('<FocusIn>', on_focusin, add="+")
+ entry.bind('<FocusOut>', on_focusout, add="+")
+
+ entry.placeholder_state = state
+
+ return state
+
class SearchBox(Frame):
- def __init__(self, master, entry_width=30, entry_font=None, entry_background="white", entry_highlightthickness=1, button_text="Search", button_ipadx=10, button_background="#009688", button_foreground="white", button_font=None, opacity=0.8, command=None):
+ def __init__(self, master, entry_width=30, entry_font=None, entry_background="white", entry_highlightthickness=1, button_text="Search", button_ipadx=10, button_background="#009688", button_foreground="white", button_font=None, opacity=0.8, placeholder=None, placeholder_font=None, placeholder_color="grey", command=None):
Frame.__init__(self, master)
self._command = command
@@ -33,6 +75,9 @@
if entry_font:
self.entry.configure(font=entry_font)
+
+ if placeholder:
+ add_placeholder_to(self.entry, placeholder, color=placeholder_color, font=placeholder_font)
if command is not None:
self.entry.bind("<Return>",self._execute_command)
@@ -91,6 +136,6 @@
showinfo("search command", "searching:%s"%text)
root = Tk()
- SearchBox(root, command=command).pack(pady=6, padx=3)
+ SearchBox(root, command=command, placeholder="Type and press enter", entry_highlightthickness=0).pack(pady=6, padx=3)
root.mainloop()