你想要的是不是这样from tkinter import *
class newEntry(Entry):
def __init__(self, master=None, placeholder="PLACEHOLDER", color="grey"):
super().__init__(master)
self.placeholder = placeholder
self.placeholder_color = color
self.default_fg_color = self["fg"]
self.bind("<FocusIn>", self.foc_in)
self.bind("<FocusOut>", self.foc_out)
self.put_placeholder()
def put_placeholder(self):
self.insert(0, self.placeholder)
self["fg"] = self.placeholder_color
def foc_in(self, *args):
if self["fg"] == self.placeholder_color:
self.delete("0", "end")
self["fg"] = self.default_fg_color
def foc_out(self, *args):
if not self.get():
self.put_placeholder()
root = Tk()
Label(root, text="用户名密码登录").pack()
username = newEntry(root, "手机/邮箱/用户名")
username.pack()
password = newEntry(root, "密码")
password.pack()
Button(root, text="登录").pack()
root.mainloop()
|