user interface - How to create a search box with 'magnifying glass' image and some text within it -
i want create search box in python 3. aware of entry widget , buttons, want more elegant this. possible create closer 1 in image? if yes, kindly throw light on topic. tia
you can using ttk if create new element using image search icon embed text widget using following code. in case add theme provided 'pin' icon element replaced. demo looks original entry on top , new style below:
the vsapi element engine available on windows using image element engine define custom element work on tk platforms.
import tkinter tk import tkinter.ttk ttk class searchentry(ttk.widget): """ customized version of ttk entry widget element included in text field. custom elements can created using either vsapi engine obtain system theme provided elements (like pin used here) or using "image" element engine create element using tk images. note: class needs registered tk interpreter before gets used calling "register" static method. """ def __init__(self, master, **kw): kw["style"] = "search.entry" ttk.widget.__init__(self, master, 'ttk::entry', kw) def get(self): return self.tk.call(self._w, 'get') def set(self, value): self.tk.call(self._w, 'set', value) @staticmethod def register(root): style = ttk.style() # there seems argument parsing bug in tkinter.ttk cheat , eval # raw tcl code add vsapi element pin. root.eval('''ttk::style element create pin vsapi explorerbar 3 { {pressed !selected} 3 {active !selected} 2 {pressed selected} 6 {active selected} 5 {selected} 4 {} 1 }''') #style.element_create("pin", "vsapi", "explorerbar", "3", [(["selected"], 4),([], 1)]) style.layout("search.entry", [ ("search.entry.field", {'sticky': 'nswe', 'children': [ ("search.entry.background", {'sticky':'nswe', 'children': [ ("search.entry.padding", {'sticky':'nswe', 'children': [ ("search.entry.textarea", {'sticky':'nswe'}) ]}) ]}), ("search.entry.pin", {'sticky': 'e'}) ]}) ]) style.configure("search.entry", padding=(1, 1, 14, 1)) style.map("search.entry", **style.map("tentry")) if __name__ == '__main__': root = tk.tk() text = tk.stringvar() searchentry.register(root) frame = ttk.frame(root) text.set("some example text ...") e1 = ttk.entry(frame, textvariable=text) e2 = searchentry(frame, textvariable=text) e1.grid(sticky="news", padx=2, pady=2) e2.grid(sticky="news", padx=2, pady=2) frame.grid(sticky = "news", padx=2, pady=2) root.grid_columnconfigure(0, weight = "1") root.grid_rowconfigure(0, weight = "1") root.mainloop()
Comments
Post a Comment