0

I'm currently using Customtkinter and encountering an issue with horizontally centering text within a CTkTextbox. Here is a snippet of my code:

textbox = customtkinter.CTkTextbox(master=self.createLoginFrame.loginFrame, width=loginFrame_width/1.2, height=loginFrame_height/6)
textbox.configure(padx=textbox.winfo_reqwidth() / 2)
textbox.grid(row=0, column=0)

With this setup, the text initially starts typing from the center of the textbox, which is my goal. However, the issue occurs when typing subsequent characters; after typing one character, the text moves to a new line instead of continuing horizontally centered.

I believe the padx setting is affecting both the left and right sides of the textbox, causing this issue. Is there a way to dynamically keep the text horizontally centered after each character is typed, rather than it moving to a new line?

Any suggestions or alternative methods to achieve this in Customtkinter would be highly appreciated. Thank you!

Here is a screenshot showing the current issue, with text jumping lines after each character:

3
  • 1
    You're configuring the textbox to have padding on each side, equal to half the width of the widget. You should be seeing ANY text at all, since the entire widget is padding! (I guess the programmer of this widget didn't bother to implement, or at least didn't bother to test, this absurd edge-case). Set padx to something more reasonable. Commented Jul 14, 2024 at 16:25
  • 1
    You've told the entire text widget to be padding. You can reduce the padding, but that's not really how you do this, and it won't really work, because you won't be typing from the center, you'll be typing from right of left pad.. You should be adding a tag to the text with justify="center" set as one of the options. Commented Jul 14, 2024 at 17:05
  • 1
    To be clearer: You need to create a tag with center justification and apply it to the entire text box. You then need to bind all key presses to a custom function. in that function you check if event.char exists. If it does, insert the new character, use tag_remove to remove your tag, and then use tag_add to apply the tag to the beginning up to the NEW end. Follow up by returning "break" to suppress normal keypress behavior. I would have given you a real answer but I'm not typing up a code example on my phone, which is all I have ATM. Commented Jul 14, 2024 at 17:48

1 Answer 1

0

Try adding ew to grid

textbox.grid(row=0, column=0, sticky = 'ew')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.