【问题标题】:Python: is there a way to have default for Entry field?Python:有没有办法为 Entry 字段设置默认值?
【发布时间】:2013-07-25 13:44:43
【问题描述】:

有没有办法让 GUI 中的文本框具有默认文本显示?

我希望文本框有“请设置你想要的文件的路径...” 但是,当我运行它时 - 它是空白的......

我的代码如下:

  path=StringVar()
  textEntry=Entry(master,textvariable=path,text='Please set the path of the file you want...')
  textEntry.pack()

【问题讨论】:

    标签: python tkinter tkinter-entry


    【解决方案1】:

    这应该演示如何做你想做的事:

    import Tkinter as tk
    
    root = tk.Tk()
    
    entry = tk.Entry(root, width=40)
    entry.pack()
    # Put text in the entrybox with the insert method.
    # The 0 means "at the begining".
    entry.insert(0, 'Please set the path of the file you want...')
    
    text = tk.Text(root, width=45, height=5)
    text.pack()
    # Textboxes also have an insert.
    # However, since they have a height and a width, you need to
    # put 0.0 to spcify the beginning.  That is basically the same as
    # x=0, y=0.
    text.insert(0.0, 'Please set the path of the file you want...')
    
    root.mainloop()
    

    【讨论】:

      【解决方案2】:
      path.set("Please set the path of the file you want...")
      

      -或-

      textEntry.insert(0, "Please set the path of the file you want...")
      

      有用的文档:

      【讨论】:

        猜你喜欢
        • 2010-12-20
        • 1970-01-01
        • 2020-03-08
        • 2016-07-16
        • 2012-08-07
        • 1970-01-01
        • 1970-01-01
        • 2017-12-05
        • 2018-07-23
        相关资源
        最近更新 更多