How to set default text for a Tkinter Entry widget

后端 未结 1 620
情深已故
情深已故 2020-12-14 00:12

How do I set the default text for a Tkinter Entry widget in the constructor? I checked the documentation, but I do not see a something like a \"string=\" optio

相关标签:
1条回答
  • 2020-12-14 00:39

    Use Entry.insert. For example:

    try:
        from tkinter import *  # Python 3.x
    except Import Error:
        from Tkinter import *  # Python 2.x
    
    root = Tk()
    e = Entry(root)
    e.insert(END, 'default text')
    e.pack()
    root.mainloop()
    

    Or use textvariable option:

    try:
        from tkinter import *  # Python 3.x
    except Import Error:
        from Tkinter import *  # Python 2.x
    
    root = Tk()
    v = StringVar(root, value='default text')
    e = Entry(root, textvariable=v)
    e.pack()
    root.mainloop()
    
    0 讨论(0)
提交回复
热议问题