如何将占位符添加到 tkinter 中的条目? [英] How to add placeholder to an Entry in tkinter?

查看:74
本文介绍了如何将占位符添加到 tkinter 中的条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 tkinter 中创建了一个登录窗口,它有两个输入字段,第一个是用户名,第二个是密码.
代码

I have created a login window in tkinter which has two Entry field, first one is Username and second one is Password.
code

from tkinter import *

ui = Tk()

e1 = Entry(ui)
#i need a placeholder "Username" in the above entry field
e1.pack()

ui.mainloop()

我想要一个名为用户名"的占位符;在 Entry 中,但如果您在输入框内单击,文本应该消失.

I want a placeholder called "Username" in the Entry, but if you click inside the entry box, the text should disappear.

推荐答案

您需要为此条目设置默认值.像这样:

You need to set a default value for this entry. Like this:

from tkinter import *

ui = Tk()

e1 = Entry(ui)
e1.insert(0, 'username')
e1.pack()

ui.mainloop()

那么如果你想在点击条目时删除内容,那么你必须绑定一个鼠标点击事件和一个事件处理程序方法来更新这个条目的内容.这是一个链接.

Then if you want to delete the content when you click the entry, then you have to bind a mouse click event with an event handler method to update content of this entry. Here is a link for you.

这篇关于如何将占位符添加到 tkinter 中的条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆