python列表appendtext_python-默认文本以及列表textvariable Entry小部...

为了将默认文本放在Entry小部件中,可以使用here所述的insert()方法.

box.insert(0, "Value 1") # Set default text at cursor position 0.

现在,为了在用户在框内单击鼠标后更改框的内容,您需要将事件绑定到Entry对象.例如,以下代码在单击该框时将其删除. (您可以阅读有关事件和绑定here的信息.)在下面,我显示了一个完整的示例.

请注意,删除框中的文本可能仅对第一次单击有效(即删除默认内容时),因此我创建了一个单击的全局标志以跟踪是否已单击它.

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值