How to add a placeholder in 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 中添加占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Tkinter 中添加占位符


基础教程推荐
- 包装空间模型 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 求两个直方图的卷积 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01