Relatively new to Tkinter and Python. So kindly bear with me.
I am trying to display the following GUI and want to have a scrollbar in Frame2 to display only 5x5 buttons at a time. Looks like Tkinter Frames don't support scrollbar and hence added a canvas (within which the frame is embedded) and a scrollbar in the parent frame 'FMas'. But for some reason the scroll bar goes to the right end of the screen and doesn't do any scrolling.
Shouldn't the canvas end at the edge of the Frame2 and the scroll bar be right next to it? Also, I tried rowspan to increase the height of the scrollbar to match the height of 5x5 buttons. That too doesn't work.
CODE (Using Python3.2):
from tkinter import *
import tkinter.ttk as ttk
mGui = Tk()
mGui.geometry("630x600")
mGui.configure(background="Gray")
mGui.columnconfigure(0, weight=1)
mGui.rowconfigure(0, weight=1)
FMas = Frame(mGui, bg="Gray")
FMas.grid(sticky=(N,E,S,W))
FMas.columnconfigure(0, weight=1)
L1 = Label(FMas, text="Frame 1 Contents")
L1.grid(row=0, column=0, pady=5, sticky=(N,W))
F1 = Frame(FMas, bg="Green", bd=2, relief=GROOVE)
F1.grid(row=1, column=0, sticky=(N,W))
ChkBox1=IntVar()
CB1 = Checkbutton(F1, text="StartCheckBox", variable=ChkBox1)
CB1.grid(row=0,column=0,padx=2)
L2 = Label(FMas, text="Frame 2 Contents")
L2.grid(row=2, column=0, pady=5, sticky=(N,W))
Can1 = Canvas(FMas, bg="Yellow")
Can1.grid(row=3, column=0, sticky=(N,W))
F2 = Frame(Can1, bg="Blue", bd=2, relief=GROOVE)
F2.grid(row=0, column=0, sticky=(N,W))
rows = 10
for i in range(1,rows):
for j in range(1,6):
button = Button(F2, padx=7, pady=7, text="[%d,%d]" % (i,j))
button.grid(row=i, column=j, sticky='news')
vsbar = Scrollbar(FMas, orient="vertical", command=Can1.yview)
vsbar.grid(row=3, column=1)
Can1.configure(yscrollcommand=vsbar.set, scrollregion=Can1.bbox("all"))
L3 = Label(FMas, text="Frame 3 Contents")
L3.grid(row=4, column=0, pady=5, sticky=(N,W))
F3 = Frame(FMas, bg="Red", bd=2, relief=GROOVE)
F3.grid(row=5, column=0, sticky=(N,W))
ChkBox2=IntVar()
CB2 = Checkbutton(F3, text="EndCheckBox", variable=ChkBox2)
CB2.grid(row=0,column=0,padx=2)
mGui.mainloop()
sys.exit()
The height of your scrollbar didn't match the buttons frame height because you did't tell it to stick North and South
.grid(..., sticky='ns')
Then, the scrolling behavior you want to achieve is described here: Adding a Scrollbar to a group of widgets
See also @martineau's answer for a more general object-oriented solution with 2D scrolling (horizontal & vertical)
Although this is a somewhat dated question, here's a different answer which doesn't use tkinter event handling, which requires unnecessary overhead.
Although the code is derived from the OP's, I've made a number of code formatting changes so it conforms better to the PEP 8 - Style Guide for Python Code which resulted in many variable names being changed. I've also modified the architecture so the application is a subclass of the root
tkinter.Tk
window widget class. I did these things with the hope that the results will be more understandable and provide a better template for writing similartkinter
-based applications.Like @Josselin's answer, it nests the
Canvas
and each of theScrollbar
widget inside anotherFrame
which allows the them to easily be positioned alongside one another both vertically and horizontally usingtkinter
'sgrid()
layout manager.The code has also been extended so the grid also a horizontal scrollbar to allow scrolling of its contents in that direction as well as up and down.
Here's what it looks like running: