2015-10-10 48 views
0

我想在Python中創建3.我知道entry組件和按鈕的搜索框,但我想要的東西更優雅像this。是否有可能創建更接近圖像中的東西?如果是的話,請點擊這個主題。 TIA如何創建與「放大鏡」圖像的搜索框和一些文字在它

+0

我相信下面會來有點接近:有邊框(藍色鏡架如果允許的話,不知道里面一個下拉列表,輸入框,按鈕,按鍵,全部採用無邊框或留白的按鈕獲取圖像。標籤而不是文本。 –

回答

1

如果您創建使用圖像,你可以將其嵌入到使用下面的代碼的文本插件的搜索圖標的新元素你可以做到這一點使用TTK。在這種情況下,我們添加一個提供'pin'圖標的主題,但這個元素可以很容易地被替換。演示看起來像這樣與頂部的原始入口和下面的新風格:

example image

的VSAPI元素引擎僅適用於Windows,但通過使用圖像元素引擎來定義你的自定義元素這會工作在所有的Tk平臺上。

import tkinter as tk 
import tkinter.ttk as ttk 

class SearchEntry(ttk.Widget): 
    """ 
    Customized version of a ttk Entry widget with an element included in the 
    text field. Custom elements can be created using either the vsapi engine 
    to obtain system theme provided elements (like the pin used here) or by using 
    the "image" element engine to create an element using Tk images. 

    Note: this class needs to be registered with the Tk interpreter before it gets 
    used by calling the "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 to be some argument parsing bug in tkinter.ttk so cheat and eval 
     # the raw Tcl code to add the vsapi element for a 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() 
相關問題