0

I have a bunch of checkboxes instantiated in Tkinter, and I would like to scroll through the window because all of them cannot be fit in one frame. I see the scrollbar, but there is no effect on using it, and the window stays stationary. How can I fix it? This is my code -

root = Tk() scroll = Scrollbar(root) scroll.pack(fill=Y,side=RIGHT) l = [] for checkBoxName in all_files: var = IntVar() c = Checkbutton(root, text=checkBoxName, variable=var, onvalue=1, offvalue=0) c.pack() l.append(var) root.mainloop() 
0

1 Answer 1

0

After some searches from the internet, I found what you need.

Note: Everything below is not mine, all the source I will put below.


So, this is what you need:

# Create A Main frame main_frame = Frame(root) main_frame.pack(fill=BOTH, expand=1) # Create Frame for X Scrollbar sec = Frame(main_frame) sec.pack(fill=X, side=BOTTOM) # Create A Canvas my_canvas = Canvas(main_frame) my_canvas.pack(side=LEFT, fill=BOTH, expand=1) # Add A Scrollbars to Canvas scrollbar = ttk.Scrollbar(main_frame, orient=VERTICAL, command=my_canvas.yview) scrollbar.pack(side=RIGHT, fill=Y) # Configure the canvas my_canvas.configure(yscrollcommand=scrollbar.set) my_canvas.bind("<Configure>", lambda e: my_canvas.config(scrollregion=my_canvas.bbox(ALL))) # Create Another Frame INSIDE the Canvas second_frame = Frame(my_canvas) # Add that New Frame a Window In The Canvas my_canvas.create_window((0, 0), window=second_frame, anchor="nw") 

and the full code is:

from tkinter import * from tkinter import ttk all_files = [str(i) for i in range(100)] # your list root = Tk() # Create A Main frame main_frame = Frame(root) main_frame.pack(fill=BOTH, expand=1) # Create Frame for X Scrollbar sec = Frame(main_frame) sec.pack(fill=X, side=BOTTOM) # Create A Canvas my_canvas = Canvas(main_frame) my_canvas.pack(side=LEFT, fill=BOTH, expand=1) # Add A Scrollbars to Canvas scrollbar = ttk.Scrollbar(main_frame, orient=VERTICAL, command=my_canvas.yview) scrollbar.pack(side=RIGHT, fill=Y) # Configure the canvas my_canvas.configure(yscrollcommand=scrollbar.set) my_canvas.bind("<Configure>", lambda e: my_canvas.config(scrollregion=my_canvas.bbox(ALL))) # Create Another Frame INSIDE the Canvas second_frame = Frame(my_canvas) # Add that New Frame a Window In The Canvas my_canvas.create_window((0, 0), window=second_frame, anchor="nw") #========== l = [] for checkBoxName in all_files: var = IntVar() c = Checkbutton(second_frame, text=checkBoxName, variable=var, onvalue=1, offvalue=0) c.pack() l.append(var) #========== root.mainloop() 

There is also an another shorter way:

from tkinter import * from tkinter.tix import * all_files = [str(i) for i in range(100)] # your list root = Tk() frame = Frame(width="500",height="500") frame.pack() swin = ScrolledWindow(frame, width=500, height=500) swin.pack() win = swin.window #========== l = [] for checkBoxName in all_files: var = IntVar() c = Checkbutton(win, text=checkBoxName, variable=var, onvalue=1, offvalue=0) c.pack() l.append(var) #========== root.mainloop() 

To be honest, I'm not too good at tkinter, and I just have a little experience about it, so I don't really understand how they could make this code.

Sources:

  1. First Way
  2. Video Tutorial (1st way)
  3. Another Way
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.