Chodzi Tobie o tzw. placeholder jak np. w html-u.
Przykład (w oparciu o kod z Ttk Styles)
import tkinter as tk
from tkinter import ttk
class PlaceholderEntry(ttk.Entry):
def __init__(self, master=None, placeholder="", **kwargs):
super().__init__(master, **kwargs)
self.placeholder = placeholder
self.bind("<FocusIn>", self.on_entry_click)
self.bind("<FocusOut>", self.on_focus_out)
self.after(100, self.show_placeholder)
def on_entry_click(self, event):
if self.get() == self.placeholder:
self.delete(0, tk.END)
self.config(foreground='black')
def on_focus_out(self, event):
if not self.get():
self.show_placeholder()
def show_placeholder(self):
self.delete(0, tk.END)
self.insert(0, self.placeholder)
self.config(foreground='grey')
class App(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('200x150')
self.resizable(0, 0)
self.title('Logowanie')
placeholder_username = "Adres e-mail"
placeholder_password = "Hasło"
# UI options
paddings = {'padx': 15, 'pady': 5}
entry_font = {'font': ('Helvetica', 11)}
username = tk.StringVar()
password = tk.StringVar()
# heading
heading = ttk.Label(self, text='Logowanie ...', style='Heading.TLabel')
heading.grid(column=0, row=0, pady=5, sticky=tk.N)
# username
username_entry = PlaceholderEntry(self, placeholder=placeholder_username, textvariable=username, **entry_font)
username_entry.grid(column=0, row=1, sticky=tk.E, **paddings)
# password
password_entry = PlaceholderEntry(self, placeholder=placeholder_password, textvariable=password, **entry_font)
password_entry.grid(column=0, row=2, sticky=tk.E, **paddings)
# login button
login_button = ttk.Button(self, text="Zaloguj się", style='LoginButton.TButton')
login_button.grid(column=0, row=3, sticky=tk.EW, **paddings)
# configure style
self.style = ttk.Style(self)
self.style.configure('TLabel', font=('Helvetica', 11))
self.style.configure('TButton', font=('Helvetica', 11))
self.style.configure('TEntry', foreground='grey')
# heading style
self.style.configure('Heading.TLabel', font=('Helvetica', 12))
# login button style
self.style.configure('LoginButton.TButton', foreground='#6BB8E6', background='#6BB8E6', font=('Helvetica', 10))
if __name__ == "__main__":
app = App()
app.mainloop()