CustomTkinter icon indicating copy to clipboard operation
CustomTkinter copied to clipboard

Associating a tk.StringVar with CTkEntry erases placeholder text

Open avalon60 opened this issue 2 years ago • 1 comments

Hello.

I have added StringVar to CTkEntry widget, using the textvariable parameter. I am then binding the key release event to a function. The idea being that as the user types, the characters are converted to uppercase:

        self.tk_new_db_account_name = tk.StringVar(master=self.frm_new_entry)
        self.ent_new_db_account_name = ctk.CTkEntry(master=self.frm_new_entry,
                                                    placeholder_text='Database username',
                                                    textvariable=self.tk_new_db_account_name)
        self.ent_new_db_account_name.grid(row=row, column=0, padx=default_padx, pady=default_pady)
        self.ent_new_db_account_name.bind("<KeyRelease>", self.db_account_caps)

In case it is relevant (unlikely), db_account_caps method looks like this:

    def db_account_caps(self, event):
        self.tk_new_db_account_name .set(self.tk_new_db_account_name .get().upper())

The capitalisation works, however this causes the 'Database username', placeholder text, to be ignored (or erased). If I remove the StringVar, the placeholder text reappears.

This is happening with CustomTkinter 4.6.3.

Is this a bug or am I missing something?

Thanks.

avalon60 avatar Nov 23 '22 09:11 avalon60

That's intended, because I couldn't get it to work properly, it was too much work so I just disabled the placeholder for variables. Maybe I will try to do it again n the future.

TomSchimansky avatar Nov 24 '22 11:11 TomSchimansky

@avalon60 It is not possible to use the textvariable and placeholder_text at the same time because the textvariable will replace the placeholder_text.

However, there is a workaround like this

entry = CTkEntry(frame,placeholder_text="Enter Somthing..")
entry.pack()

submitButton = CTkButton(
    frame, 
    font=("",32), 
    text="Submit", 
    command=lambda: print(entry.get())
    )

Instead of using StringVar() you can get the value directly from the entry it self

Krishna-Noutiyal avatar Aug 12 '23 18:08 Krishna-Noutiyal