python列表appendtext_python-默认文本以及列表textvariable Entry小部...
為了將默認文本放在Entry小部件中,可以使用here所述的insert()方法.
box.insert(0, "Value 1") # Set default text at cursor position 0.
現(xiàn)在,為了在用戶在框內(nèi)單擊鼠標(biāo)后更改框的內(nèi)容,您需要將事件綁定到Entry對象.例如,以下代碼在單擊該框時將其刪除. (您可以閱讀有關(guān)事件和綁定here的信息.)在下面,我顯示了一個完整的示例.
請注意,刪除框中的文本可能僅對第一次單擊有效(即刪除默認內(nèi)容時),因此我創(chuàng)建了一個單擊的全局標(biāo)志以跟蹤是否已單擊它.
from tkinter import Tk, Entry, END # Python3. For Python2.x, import Tkinter.
# Use this as a flag to indicate if the box was clicked.
global clicked
clicked = False
# Delete the contents of the Entry widget. Use the flag
# so that this only happens the first time.
def callback(event):
global clicked
if (clicked == False):
box[0].delete(0, END) #
box[0].config(fg = "black") # Change the colour of the text here.
clicked = True
root = Tk()
box = [] # Declare a list for the Entry widgets.
box.append(Entry(fg = "gray")) # Create an Entry box with gray text.
box[0].bind("", callback) # Bind a mouse-click to the callback function.
box[0].insert(0, "Value 1") # Set default text at cursor position 0.
box.append(Entry(fg = "gray")) # Make a 2nd Entry; store a reference to it in box.
box[1].insert(0, "Value 2")
box[0].pack() #
box[1].pack()
if __name__ == "__main__":
root.mainloop()
總結(jié)
以上是生活随笔為你收集整理的python列表appendtext_python-默认文本以及列表textvariable Entry小部...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 下列哪项属于正确的锁定计算机桌面,【20
- 下一篇: 怎样切换git账号密码错误_git中多账