如何在 tkinter Entry 小部件中插入临时文本? [英] How to insert a temporary text in a tkinter Entry widget?

查看:88
本文介绍了如何在 tkinter Entry 小部件中插入临时文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 tkinter Entry 小部件中插入临时文本?

How to insert a temporary text in a tkinter Entry widget?

例如,我有一个标签 User,在它旁边我有一个 Entry 小部件,它的开头应该有一些文本 "Enter your username..."应用程序,将光标放在 Entry 小部件上时,它应该删除 "Enter your username..." 并允许用户输入数据.

For example, I have a Label User and next to it I have an Entry widget which should have some text "Enter your username..." at the beginning of the application, and while putting cursor on the Entry widget, it should remove "Enter your username..." and allow user to enter data.

这是我当前的代码:

import tkinter as tk

root = tk.Tk()

label = tk.Label(root, text="User:")
label.pack()
entry = tk.Entry(root, bd=1, show="Enter your user name...")
entry.pack()

root.mainloop()

我该怎么做?

我没有找到任何选项或方法来删除将光标放在 Entry 小部件上的数据.

I didn't find any option or method to delete data on putting cursor on the Entry widget.

推荐答案

我想在回复中添加一些尚未提及的内容.正如 9jera 所说,只有在看到默认文本而不是使用firstclick"方法时,我们才能使 Entry 小部件清晰Leo Tenenbaum.

I would like to add something to the responses that has not been said. As 9jera said, we can make the Entry widget clear only when it sees the default text instead of the "firstclick" method used by Leo Tenenbaum.

如果用户在没有输入任何内容的情况下将焦点转向另一个小部件,我们还可以添加第二个函数来重新填充 Entry 小部件.

We could also add a second function to refill the Entry widget if the user has turned the focus to another widget without typing in anything.

这可以通过以下方式实现:

This can be achieved as follows:

import Tkinter as tk


def on_entry_click(event):
    """function that gets called whenever entry is clicked"""
    if entry.get() == 'Enter your user name...':
       entry.delete(0, "end") # delete all the text in the entry
       entry.insert(0, '') #Insert blank for user input
       entry.config(fg = 'black')
def on_focusout(event):
    if entry.get() == '':
        entry.insert(0, 'Enter your username...')
        entry.config(fg = 'grey')


root = tk.Tk()

label = tk.Label(root, text="User: ")
label.pack(side="left")

entry = tk.Entry(root, bd=1)
entry.insert(0, 'Enter your user name...')
entry.bind('<FocusIn>', on_entry_click)
entry.bind('<FocusOut>', on_focusout)
entry.config(fg = 'grey')
entry.pack(side="left")

root.mainloop()

最后,我还为默认文本添加了灰色,为用户编写的文本添加了黑色,仅供对此感到疑惑的任何人使用.我在这里看到的唯一问题是,如果用户实际上手动输入输入您的用户名...,聚焦并再次输入,即使文本是由用户编写的,也会被删除他自己.我想到的一种解决方案是更改 if 语句,以便在删除任何内容之前获取颜色而不是默认文本.如果颜色是灰色,它可以继续并删除它.否则,它不会.但是,我还没有找到获取文本颜色的方法.如果有人知道这件事,请告诉我!

Finally, I also added grey color to the default text and black to the one written by the user, just for anyone that was wondering about that. The only issue I see here is, if the user actually manually types in there Enter your user name..., focuses out and in again, the text will be deleted even though it was written by the user himself. One solution I thought of is to change the if statement so it gets the color instead of the default text before deleting anything. If the color is grey, it can go ahead and delete it. Else, it will not. Yet, I have not found a way to get the text color. If anyone knows about this, please let me know!

显然,正如 Olivier Samson 指出的那样,可以获取条目的颜色通过使用 entry.cget('fg').我想,两年后,我终于想出了如何做到这一点.

Apparently, as Olivier Samson has pointed out, it is possible to get the color of the entry by using entry.cget('fg'). I guess, after 2 years, I finally figure out how to do this.

因此,我们现在可以将 if entry.get() == 'Enter your user name...': 更改为 if entry.cget('fg') == '灰色':.这样,每当第一次单击条目并在里面输入任何内容时,颜色都会更改为黑色,因此下次用户聚焦时,它不会删除任何文本(即使该文本是 Enter your用户名...).

Therefore, we can now change the line if entry.get() == 'Enter your user name...': to if entry.cget('fg') == 'grey':. That way, whenever the entry is clicked for the first time and anything is typed inside, the color is changed to black, so next time the user focuses in, it will not delete any text (even if that text is Enter your user name...).

这篇关于如何在 tkinter Entry 小部件中插入临时文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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