問題描述
如何將占位符添加到 tkinter
中的條目?例如,我不相信它具有像 HTML 這樣的占位符功能.我發(fā)現(xiàn)要使文本在單擊時(shí)消失,您必須添加一個(gè) onclick
事件,但是如何創(chuàng)建 onclick
事件以及如何制作文字出現(xiàn)在第一位?這是我正在使用的代碼.
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.
我想說"在此處輸入您的整數(shù)"
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()
推薦答案
據(jù)我所知,你不能像 HTML 一樣添加占位符,但你可以做出類似的行為.當(dāng)您進(jìn)入時(shí),您可以執(zhí)行以下操作>
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:這是我自己寫的,還沒有測(cè)試過
P.S: I've wrote this from my head , haven't tested it
這篇關(guān)于如何在 Tkinter 中添加占位符的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!