I have a CustomTkinter
app with an horizontal CTkScrollableFrame
. Inside this frame, I have two frames:
- On the left, a
CTkFrame
, non-scrollable, here in blue, containing a smaller CTkFrame` (or any widgets), here in teal. - On the right, a vertical
CTkScrollableFrame
, here in red, containing two moreCTkFrame
(or any widgets), here in orange and yellow.
The horizontal CTkScrollableFrame
is expanding in the full window (vertical and horizontal), as wanted.
The vertical CTkScrollableFrame
is not expanding in the complete space left in the window (horizontal).
How can I get the following result?
EDIT:
Based on the code from Demonstrate a CustomTkinker scrollable and clickable grid, I tried another solution, see “NEW MWE”. How could I make the vertical scrollbar scroll the canvas_v
?
The MWE has been updated and the result is:
NEW MWE
from customtkinter import CTk, CTkFrame, CTkScrollbar
from tkinter import Canvas
def scroll_x(*args):
"""Scroll horizontally for scrollbar movements."""
canvas_h.xview(*args)
def scroll_y(*args):
"""Scroll vertically for scrollbar movements."""
canvas_v.yview(*args)
def handle_mousewheel(event):
"""Scroll vertically for mouse wheel."""
canvas_v.yview_scroll(int(-1*(event/120)), "units")
def handle_mousewheel_shift(event):
"""Scroll horizontally for mouse wheel.."""
canvas_h.xview_scroll(int(-1*(event/120)), "units")
root = CTk()
frame_h = CTkFrame(root, fg_color="blue")
frame_h.grid(column=0, row=0, sticky='nsew')
# Define horizontal scrollbar
hsb = CTkScrollbar(root, orientation='horizontal', command=scroll_x)
hsb.grid(column=0, row=1, sticky='ew')
# Make grid resizeable
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
# Canvas with internal frame for horizontal scrollable frame
canvas_h = Canvas(frame_h, width=200, height=300)
frame_canvas_h = CTkFrame(canvas_h, fg_color="cyan")
canvas_h.config(xscrollcommand=hsb.set, highlightthickness=0)
canvas_h.pack(fill='both', side='left', expand=True)
canvas_h.create_window(0, 0, window=frame_canvas_h, anchor='nw')
# Get the colorful frames
column_frames = []
column_frames.append(CTkFrame(frame_canvas_h, fg_color="teal"))
column_frames[0].grid(column=0, row=0)
##### Start working on vertical scrollable frame
frame_v = CTkFrame(frame_canvas_h, fg_color="red")
frame_v.grid(column=1, row=0, sticky="nswe")
# Define vertical scrollbar
vsb = CTkScrollbar(root, orientation='vertical', command=scroll_y)
vsb.grid(column=1, row=0, rowspan=2, sticky='ew')
# Make grid resizeable
frame_h.grid_columnconfigure(0, weight=1)
frame_h.grid_rowconfigure(0, weight=1)
# Canvas with internal frame for vertical scrollable frame
canvas_v = Canvas(frame_v, width=500, height=300)
frame_canvas_v = CTkFrame(canvas_v, fg_color="pink")
canvas_v.config(yscrollcommand=vsb.set, highlightthickness=0)
canvas_v.pack(fill='both', side='left', expand=True)
canvas_v.create_window(0, 0, window=frame_canvas_v, anchor='nw')
# Get the colorful frames
column_frames.append(CTkFrame(frame_canvas_v, fg_color="orange"))
column_frames.append(CTkFrame(frame_canvas_v, fg_color="yellow"))
column_frames[1].grid(column=0, row=0)
column_frames[2].grid(column=1, row=0)
# Size canvases to size of frame with widgets
canvas_h.update_idletasks()
canvas_v.update_idletasks()
canvas_h.config(scrollregion=frame_canvas_h.bbox("all"))
canvas_v.config(scrollregion=frame_canvas_v.bbox("all"))
root.mainloop()
OLD MWE
from customtkinter import CTkFrame, CTk, CTkScrollableFrame
from tkinter import LEFT
root = CTk()
# Create a horizontal scrollable frame
h_scroll_frame = CTkScrollableFrame(root, orientation="horizontal")
h_scroll_frame.pack(fill="both", expand=True)
# Create a vertical scrollable frame
v_scroll_frame = CTkScrollableFrame(h_scroll_frame, fg_color="red")
# Create the first of the column_frames, outside of vertical scrolling
column_frames = [CTkFrame(h_scroll_frame, fg_color="blue")]
column_frames[0].pack(side=LEFT, fill="both", expand=False)
v_scroll_frame.pack(side=LEFT, fill="both", expand=True)
# Add two more column_frames, inside of vertical scrolling
for column in range(1, 3):
column_frames.append(CTkFrame(v_scroll_frame))
column_frames[column].grid(column=column, row=0, sticky="NWE", padx=0, pady=0)
# Add a boxed frame inside each of the column_frames
colors = ["teal", "orange", "yellow"]
for index_column, column in enumerate(column_frames):
frame = CTkFrame(column, fg_color=colors[index_column], border_color="black", border_width=3)
frame.grid(column=0, row=0)
root.mainloop()
7