Welcome, guest | Sign In | My Account | Store | Cart
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 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):
        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 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).pack(pady=6, padx=3)

    root.mainloop()

Diff to Previous Revision

--- revision 3 2017-04-04 05:01:53
+++ revision 4 2017-04-04 05:05:52
@@ -91,6 +91,6 @@
         showinfo("search command", "searching:%s"%text)
 
     root = Tk()
-    SearchBox(root, command=command).pack(pady=6)
+    SearchBox(root, command=command).pack(pady=6, padx=3)
 
     root.mainloop()

History