python - Reset Tkinter Widget to its default value -


in need make generic function on event return focused-out widget default value. there way accomplish this?

example:

entry1 = tkinter.entry() entry1.grid(..) entry1.insert(0,"hello") entry1.bind("<focusin>", entryfocusedin) entry1.bind("<focusout>", entryfocusedout)  entry2 = tkinter.entry() entry2.grid(..) entry2.insert(0,"again") entry2.bind("<focusin>", entryfocusedin) entry2.bind("<focusout>", entryfocusedout)    def entryfocusedin(params):      params.widget.delete(0, tkinter.end)  def entryfocusedout(params):      # return widget default value      # in case of entry1 "hello"      # , in case of entry2 "again" 

you subclass entry widget add attribute store default value, , reference attribute in event handler. however, there's nothing stopping adding own attribute each entry widget directly, e.g. entry1.default_value = 'hello', entry1.default_value = 'again':

import tkinter  def entryfocusedin(params):     params.widget.delete(0, tkinter.end)  def entryfocusedout(params):     # restore default value     params.widget.delete(0, tkinter.end)     params.widget.insert(0, params.widget.default_value)  root = tkinter.tk()  entry1 = tkinter.entry() entry1.default_value = 'hello' entry1.pack() entry1.insert(0, entry1.default_value) entry1.bind("<focusin>", entryfocusedin) entry1.bind("<focusout>", entryfocusedout)  entry2 = tkinter.entry() entry2.default_value = 'again' entry2.pack() entry2.insert(0, entry2.default_value) entry2.bind("<focusin>", entryfocusedin) entry2.bind("<focusout>", entryfocusedout)  root.mainloop() 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -