如何在 Tkinter 中添加占位符

How to add a placeholder in Tkinter(如何在 Tkinter 中添加占位符)
本文介绍了如何在 Tkinter 中添加占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如何将占位符添加到 tkinter 中的条目?例如,我不相信它具有像 HTML 这样的占位符功能.我发现要使文本在单击时消失,您必须添加一个 onclick 事件,但是如何创建 onclick 事件以及如何制作文字出现在第一位?这是我正在使用的代码.

How would I add a placeholder to an entry in tkinter? I don't believe it has a placeholder function like HTML for example. I figured out that to make the text disappear when you click it, you will have to add an onclick event, but how do I create the onclick event and how do you make the text appear in the first place? Here is the code I'm working with.

我想说"在此处输入您的整数"

I would like it to say " Enter your integer here"

    vcmd = (self.register(self.onValidate), '%S')
    self.text = Entry(self, validate='key', vcmd=vcmd)
    self.text.pack()
       
def onValidate(self, S):
    if S in '0123456789.':
        return True
    return False

def clear_entry(self):
    self.text.delete(0, END)
    self.text.pack()

推荐答案

据我所知,你不能像 HTML 一样添加占位符,但你可以做出类似的行为.当您进入时,您可以执行以下操作>

U can't add placeholder like HTML as far as i know, but u can make similar behavior. When you make entry you can do something like>

from tkinter import *   

def clear_entry(event, entry):
    entry.delete(0, END)

root = Tk()

entry = Entry(root)

entry.pack()
placeholder_text = 'some text'
entry.insert(0, placeholder_text)

entry.bind("<Button-1>", lambda event: clear_entry(event, entry))

root.mainloop()

P.S:这是我自己写的,还没有测试过

P.S: I've wrote this from my head , haven't tested it

这篇关于如何在 Tkinter 中添加占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reading *.mhd/*.raw format in python(在 python 中读取 *.mhd/*.raw 格式)
Count number of cells in the image(计算图像中的单元格数)
How to detect paragraphs in a text document image for a non-consistent text structure in Python OpenCV(如何在 Python OpenCV 中检测文本文档图像中的段落是否存在不一致的文本结构)
How to get the coordinates of the bounding box in YOLO object detection?(YOLO物体检测中如何获取边界框的坐标?)
Divide an image into 5x5 blocks in python and compute histogram for each block(在 python 中将图像划分为 5x5 块并计算每个块的直方图)
Extract cow number from image(从图像中提取奶牛编号)