import tkinter as tk
import customtkinter as ui
ui.set_appearance_mode("dark")
ui.set_default_color_theme("dark-blue")
root = ui.CTk()
root.geometry("500x350")
frame = ui.CTkFrame(master=root)
frame.pack(pady=20, padx=60, fill="both", expand=True)
label = ui.CTkLabel(master=frame, text="Login System", font=("Roboto", 24))
label.pack(pady=12, padx=10)
username = tk.StringVar()
entry1 = ui.CTkEntry(master=frame, textvariable=username, placeholder_text="Username")
entry1.pack(pady=12, padx=10)
password = tk.StringVar()
entry2 = ui.CTkEntry(master=frame, textvariable=password, placeholder_text="Password", show="*")
entry2.pack(pady=12, padx=10)
def login():
print(username.get())
print(password.get())
button = ui.CTkButton(master=frame, text="Login", command=login)
button.pack(pady=12, padx=10)
checkbox = ui.CTkCheckBox(master=frame, text="Remember Me")
checkbox.pack(pady=12, padx=10)
root.mainloop()
Here is my code. The problem is that the placeholder text doesn't show up when I pass a text variable argument.
entry1 = ui.CTkEntry(master=frame, placeholder_text="Username")
shows the placeholder but doesn't pass the textvariable
entry1 = ui.CTkEntry(master=frame, textvariable=username, placeholder_text="Username")
passes the textvariable but doesn't show the placeholder
I've seen similar questions but they don't work for the custom tkinter widget and they don't use the placeholder argument.
CodePudding user response:
The placeholder text is only activated when, the placeholder
attribute is set, and the value of the textvariable
attribute is None
or ""
. Since in your example the value of textvariable
is StrinVar()
, the second condition fails and the placeholder is never shown.
However you don't really need both anyway. You can simply delete the textvariable
and the username
and password
StringVar
s and just call get()
on the entry
widgets directly in order to get the values in the same manner.
import tkinter as tk
import customtkinter as ui
ui.set_appearance_mode("dark")
ui.set_default_color_theme("dark-blue")
root = ui.CTk()
root.geometry("500x350")
frame = ui.CTkFrame(master=root)
frame.pack(pady=20, padx=60, fill="both", expand=True)
label = ui.CTkLabel(master=frame, text="Login System", font=("Roboto", 24))
label.pack(pady=12, padx=10)
entry1 = ui.CTkEntry(master=frame, placeholder_text="Username")
entry1.pack(pady=12, padx=10)
entry2 = ui.CTkEntry(master=frame, placeholder_text="Password", show="*")
entry2.pack(pady=12, padx=10)
def login():
print(entry1.get())
print(entry2.get()) # call get() on the entry widgets
button = ui.CTkButton(master=frame, text="Login", command=login)
button.pack(pady=12, padx=10)
checkbox = ui.CTkCheckBox(master=frame, text="Remember Me")
checkbox.pack(pady=12, padx=10)
root.mainloop()
CodePudding user response:
You must use customtkinter.StringVar()
.
Snippet:
username = customtkinter.StringVar(value="test")
entry_1 = customtkinter.CTkEntry(app,
placeholder_text="placeholder",
textvariable=username)
entry_1.pack(pady=20)
Btw, you will do same as password too.