1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Resize CustomTkinter canvas when inside frame get resized

Discussão em 'Python' iniciado por Stack, Setembro 12, 2024.

  1. Stack

    Stack Membro Participativo

    I have a customtkinter app where the widgets are sorted in n columns (column_frames[n]). I have created some classes of scrollable frames to obtain the following behavior:

    1. Horizontal scrolling is available on the whole window.
    2. All columns are vertically frozen, except a column which can be scrolled column (freeze = "left" or freeze = "right").

    A schematic of the setting with the column_frames[n] in red and freeze = "left": [​IMG]

    Note that I am not using CTkScrollableFrame because I need the vertical scrollbar to be out of the horizontal scrollable frame. Otherwise, the vertical scrollbar is not visible until the horizontal scrollbar is pushed completely to the right.

    I have a function set_columns_scrolling to insert the column_frames[n] either in ScrollableFrameH (horizontal) or ScrollableFrameV (vertical) based on the freeze setting.

    When the window get resized, I manage to get the canvas to resize.

    Problem: When the frames in column_frames[n] get resized (by adding widgets inside), the vertical canvas does not resize accordingly.

    Class for vertical scrollable frame:

    def __init__(self, master, master_scrollbar, main_window):
    style.Style.__init__(self)

    self.main_window = main_window

    # Create scrollable frame
    self.frame_out = CTkFrame(master)
    self.frame_out.grid(column=1, row=0, sticky="nswe")

    # Create scrollbar
    self.scrollbar = CTkScrollbar(master_scrollbar, orientation="vertical")
    self.scrollbar.configure(command=self.scroll_y)
    self.scrollbar.pack(side=RIGHT, fill="y", expand=False)

    # Create canvas with internal frame
    self.canvas = CTkCanvas(self.frame_out)
    self.canvas.pack(side=LEFT, fill="both", expand=True)
    self.frame_in_canvas = CTkFrame(self.canvas)
    self.canvas_window = self.canvas.create_window(0, 0, window=self.frame_in_canvas,
    anchor="nw")

    # Define scrollbar motion and adjust canvas size
    self.canvas.config(yscrollcommand=self.scrollbar.set, highlightthickness=0)
    self.frame_in_canvas.bind("<Configure>", self.on_frame_configure)
    self.canvas.bind('<Configure>', self.update_size_canvas_window_change)

    def scroll_y(self, *args):
    """Scroll vertically for scrollbar movements."""
    self.canvas.yview(*args)

    def on_frame_configure(self, *args):
    """Reset the scroll region to encompass the inner frame."""
    self.canvas.update_idletasks()
    self.canvas.configure(scrollregion=self.canvas.bbox("all"))

    def update_size_canvas_window_change(self, event):
    self.main_window.update_idletasks()
    self.canvas.itemconfig(self.canvas_window, width=event.width)

    Continue reading...

Compartilhe esta Página