CustomTkinter icon indicating copy to clipboard operation
CustomTkinter copied to clipboard

[Feature Requrest] Textbox auto-resizing argument when creating

Open f1refa11 opened this issue 2 years ago • 8 comments

Like: textbox = customtkinter.CtkTextbox(app, autoresize=True)

f1refa11 avatar Apr 26 '23 18:04 f1refa11

What do you mean with autoresize, what do you want to achieve?

TomSchimansky avatar Apr 27 '23 20:04 TomSchimansky

Perhaps, rather than the textbox being stuck at a width and height, you can give a maximum width and height. From there, the textbox will resize itself to only fit the text inside of it. Basically, you would eliminate the extra, empty space below the text. If the text was too little for the textbox that was too big.

If that made any sense?? Sorry - it was confusing I know

ghost avatar Jun 20 '23 00:06 ghost

Anyone got solution for this?

its-ahad avatar Sep 10 '23 00:09 its-ahad

@ahad-naseer-innusagi - Use a customtkinter.StringVar to check the length of the string inside the textbox. Then, calculate the width of the textbox (maybe using data about the font's width per character in pixels or something?) and resize the textbox's width with customtkinter.CTkTextbox.configure(). Make sure to set the redraw parameter as True.

ghost avatar Sep 10 '23 02:09 ghost

@dishb- I have tried calculating text width and determine the textbox height parameter by multiplying lines count with height, the issue sometimes it work perfect for medium length text but when text is long it leaves empty spaces below and if text is short it will set height too short and I have to scroll to see the text.

its-ahad avatar Sep 10 '23 12:09 its-ahad

@ahad-naseer-innusagi - I don't think there is much of a solution then. I haven't used CTk in a while... Not to mention, with this current hack, your application's performance will take a hit.

ghost avatar Sep 30 '23 21:09 ghost

Temporary, I have created a solution in which i check if scrollbar is shown then 1 Px is incremented in height. It effects the performance a little bit. UI looks good so i am using it.

its-ahad avatar Sep 30 '23 22:09 its-ahad

I am looking for the same feature. What I think OP is trying to do is create a box that dynamically changes its height based on it's width. In standard Tkinter this was easy:

class ChatBubble(tk.Frame):
    def __init__(self, master, sender, message, bg_color):
        super().__init__(master)

        self.text_widget = tk.Text(self, wrap='word', bg=bg_color, font=('Helvetica', 10), borderwidth=2,
                                   relief="solid", width=50)
        self.text_widget.insert(tk.END, message)
        self.text_widget.grid(row=0, column=0, sticky="ew")

        self.columnconfigure(0, weight=1)
        self.text_widget.bind("<Configure>", self.on_configure)

    def on_configure(self, event):
        """Dynamically set the height of the text widget based on its width."""
        lines = self.text_widget.count("1.0", "end", "displaylines")[0]
        self.text_widget.config(height=lines)

    def set_state(self, state):
        self.text_widget.configure(state=state)

SmallChief avatar Apr 20 '24 00:04 SmallChief