【问题标题】:User entering same placeholder text python tkinter用户输入相同的占位符文本 python tkinter
【发布时间】:2021-01-17 00:41:15
【问题描述】:

我为 tkinter 制作了一个占位符,并想将其向前推进,但如果用户输入与占位符相同的文本怎么办,占位符应该发生的所有效果都会在文本上实现。无论如何要克服这个?

占位符:

import tkinter as tk
from tkinter import ttk

class PlaceholderEntry(tk.Entry):
    '''
    Custom modern Placeholder Entry box, takes positional argument master and placeholder\n
    Use ret() for getting output from entry widget\n
    Use ins() for inserting into entry widget\n
    Use remove() for deleting from entry widget\n
    ISSUES: What if the user types exactly same text as the placeholders in the entrybox?
    '''

    def __init__(self, master, placeholder, **kwargs):
        # style for ttk widget
        self.s = ttk.Style()
        self.s.configure('my.TEntry')

        # init entry box
        ttk.Entry.__init__(self, master, style='my.TEntry', **kwargs)
        self.text = placeholder

        # add placeholder if box empty
        self.add()

        # bindings of the widget
        self.bind('<FocusIn>', self.clear)
        self.bind('<FocusOut>', self.add)
        self.bind_all('<Key>', self.normal)
        self.bind_all('<Button-1>', self.cursor)

    def clear(self, *args):
        if self.get() == self.text:  # remove placeholder when focus gain
            self.delete(0, tk.END)
            self.s.configure('my.TEntry', foreground='black',
                             font=(0, 0, 'normal'))

    def add(self, *args):
        if self.get() == '':  # if no text add placeholder
            self.s.configure('my.TEntry', foreground='grey',
                             font=(0, 0, 'bold'))
            self.insert(0, self.text)  # insert placeholder
            self.icursor(0)  # move insertion cursor to start of entrybox

    def normal(self, *args):
        self.s.configure('my.TEntry', foreground='black',
                         font=(0, 0, 'normal'))  # set normal font
        self.add()  # if empty add placeholder
        if self.get() == self.text:  # clear the placeholder if starts typing
            self.bind('<Key>', self.clear)
            self.icursor(-1)  # keep insertion cursor to the end

    def ret(self):  # custom method to get the text
        if self.get() == self.text:
            return None
        else:
            return self.get()

    def ins(self, index, string):  # custom method to insert into entry
        self.clear()
        self.insert(index, string)

    def remove(self, first, last):  # custom method to remove from entry
        if self.get() != self.text:
            self.delete(first, last)
            self.add()

    def cursor(self, *args):  # method to not allow user to move cursor when placeholder there
        if self.get() == self.text:
            self.icursor(0)

if __name__ == '__main__':
    root = tk.Tk()
    plc = PlaceholderEntry(root,placeholder='Type')
    plc.pack(padx=10,pady=10)
    root.mainloop()

您可以通过在框中键入类型来重现该问题,并且它将删除进一步的插入,因为它会将条目误认为是占位符。

提前致谢:D

【问题讨论】:

    标签: python tkinter placeholder


    【解决方案1】:

    有点老套的解决方案,但为什么不在占位符文本的开头添加空格(或不可打印字符)

    plc = PlaceholderEntry(root,placeholder=' Type')
    

    或者

    self.text = "\x00" + placeholder
    

    这将确保用户输入的文本与占位符文本不匹配

    【讨论】:

    • 哈哈,是的,好主意,我看看!
    • 当然,你还是有同样的问题。如果用户键入“Type”,它将与占位符完全相同。
    【解决方案2】:

    由于您有一个自定义类,因此可以在添加占位符文本时轻松设置标志,在删除占位符时轻松取消设置。然后就是在获取值的时候检查flag是否被设置了。

    def add(self, *args):
        if self.get() == '':  # if no text add placeholder
            ...
            self._has_placeholder = True
    
    def clear(self, *args):
        if self.get() == self.text:  # remove placeholder when focus gain
            self._has_placeholder = False
            ...
    

    【讨论】:

    • 哦,是的标志,它对我有用,改变了所有的方法。谢谢老哥!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 2017-05-11
    • 2015-12-08
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多