【问题标题】:How to create a search box with 'magnifying glass' image and some text within it如何创建一个带有“放大镜”图像和一些文本的搜索框
【发布时间】:2016-01-08 02:00:03
【问题描述】:

我想在 Python 3 中创建一个搜索框。我知道条目小部件和按钮,但我只想要更优雅的东西,比如 this。甚至有可能创造出更接近图像中的东西吗?如果是,请对这个话题有所了解。 TIA

【问题讨论】:

  • 我相信以下内容会有点接近:带边框的框架(如果允许,则为蓝色,不确定。在下拉列表、输入框、按钮、按钮内,所有这些都没有边框或填充。按钮获取图像标签而不是文本。

标签: user-interface python-3.x tkinter uisearchbar ttk


【解决方案1】:

如果您使用搜索图标的图像创建新元素,则可以使用 ttk 执行此操作,您可以使用以下代码将其嵌入到文本小部件中。在这种情况下,我们添加了一个主题提供的“pin”图标,但这个元素可以很容易地替换。演示看起来像这样,顶部是原始条目,下面是新样式:

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-01
    • 2012-04-15
    • 2013-08-06
    • 2013-08-20
    • 2019-04-09
    • 1970-01-01
    • 2018-04-16
    相关资源
    最近更新 更多