Entry box text clear when pressed Tkinter

前端 未结 2 1808
小蘑菇
小蘑菇 2020-12-07 04:43

I\'m not sure whether this has been asked already or not, but I have multiple entry boxes in which contain a default piece of text. I am not trying to set a default piece of

相关标签:
2条回答
  • 2020-12-07 05:16

    Assuming you've got your default text sorted out, you want to create an Event binding somewhere, with the general format of the comment above, not sure why it's not an answer, because it's right:

    import tkinter as tk
    
    root = tk.Tk()
    e = tk.Entry(root)
    e.insert(0, "some text")
    
    def some_callback(event): # note that you must include the event as an arg, even if you don't use it.
        e.delete(0, "end")
        return None
    
    e.bind("<Button-1>", some_callback)
    
    e.pack()
    

    Finally, http://effbot.org is your friend: http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

    EDIT: Additional info for OP from comment. If you have multiple entries and you need to clear each one individually, you can simply refer to the widget that called the bound method using

    event.widget
    

    Your callback could then work as follows:

    def some_callback(event):
        event.widget.delete(0, "end")
        return None
    
    0 讨论(0)
  • 2020-12-07 05:28

    The method called by the event binding is given an event object. This object has a reference to the widget that triggered the event. You can use that to clear the widget:

    def removeValue(event):
        event.widget.delete(0, 'end')
    
    0 讨论(0)
提交回复
热议问题