Wes Tomer
Wes Tomer

Reputation: 329

Tkinter scroll bar will not control entry widgets

My scroll bar is supposed to be a child of Canvas_2 and controlling the y-value of Canvas_3. However it is not working as intended. The scroll bar doesn't move up or down, and there is also a blue region that shouldn't be visible. Any ideas on what I'm missing here? I really appreciate your time.

import tkinter as tk
from PIL import ImageTk, Image

# To initialize tkinter, we have to create a Tk root widget,
# which is a window with a title bar and other decoration
# provided by the window manager.
# The root widget has to be created before any other widgets
# and there can only be one root widget.
root = tk.Tk()

# The weight of a row or column determines how much of the
# available space a row or column should occupy relative to
# the other rows or columns. For example, a column with a
# weight of 2 will be twice as wide as a column with a
# weight of 1, assuming there's space for the widgets to fit.
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

load1 = Image.open("example.jpg")
render1 = ImageTk.PhotoImage(load1)

# Creating a class for filling each row
def makeRow(top, img, row):
    r = row
    if row == 0:
        c1 = "#75dce1"
        c2 = "#75dce1"
        e7 = tk.Entry(top, bg=c2).grid(row=r, column=6, sticky="news")
        e8 = tk.Entry(top, bg=c2).grid(row=r, column=7, sticky="news")
    else:
        c1 = "#a9d08e"
        c2 = "#8dd1bf"
        img = tk.Label(top, image=render1, bg="green").grid(row=r, column=6, sticky="news")
    e1 = tk.Entry(top, bg=c1).grid(row=r, column=0, sticky="news")
    e2 = tk.Entry(top, bg=c1).grid(row=r, column=1, sticky="news")
    e3 = tk.Entry(top, bg=c1).grid(row=r, column=2, sticky="news")
    e4 = tk.Entry(top, bg=c1).grid(row=r, column=3, sticky="news")
    e5 = tk.Entry(top, bg=c2).grid(row=r, column=4, sticky="news")
    e6 = tk.Entry(top, bg=c2).grid(row=r, column=5, sticky="news")
    # load1 = Image.open(img)
    # render1 = ImageTk.PhotoImage(load1)


# The canv_1 is a child of the parent "root"
# canv_1 contains: canv_2 (frozen top row) and canv_3 (bottom rows with a vertical scroll)
canv_1 = tk.Canvas(root, bg="blue")
canv_1.grid_rowconfigure(0, weight=1)
canv_1.grid_rowconfigure(1, weight=10)
canv_1.grid_columnconfigure(0, weight=1)
canv_1.grid(row=0, column=0, sticky = "news")
canv_1.grid(row=1, column=0, sticky = "news")

# The canv_2 is a child of the parent "canv_1"
canv_2 = tk.Canvas(canv_1, bg="blue")
canv_2.grid_rowconfigure(0, weight=1)
canv_2.grid_rowconfigure(1, weight=1)
canv_2.grid_columnconfigure(0, weight=1)
canv_2.grid(row=0, column=0, sticky = "news")
canv_2.grid(row=1, column=0, sticky = "news")

# The canv_3 is a child of the parent "canv_2"
canv_3 = tk.Canvas(canv_2, bg="blue")
canv_3.grid(row=1, column=0, sticky="news")
# canv_3.grid_rowconfigure((1,2,3,4,5), weight=1)
# canv_3.grid_columnconfigure((1,2,3,4,5), weight=1)

slides = []
for i in range(10):
    slides.append(i)
    if i==0:
        slides[i] = makeRow(canv_3,"", 0)
    else:
        slides[i] = makeRow(canv_3, "example.jpg", i)

# Create Scrollbar
vsb = tk.Scrollbar(canv_2, orient="vertical", command=canv_3.yview)
vsb.grid(row=1, column=1, sticky='ns')
canv_2.configure(yscrollcommand=vsb.set)
canv_2.config(scrollregion=canv_3.bbox("all"))
canv_2.configure(scrollregion=(0, 0, 5000, 5000))

root.mainloop()

This is the actual output:

enter image description here

This is the desired design & output:

enter image description here

Upvotes: 2

Views: 188

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

There are at least three fundamental problems with the code. The biggest problem is that you are adding widgets to canv_3 using grid. A canvas can't scroll items added to a canvas with grid. It will only scroll items added with the "create" methods of the canvas (create_window, create_text, etc).

The second problem is that you never define a proper scrollregion for canv_3, so even if you added the items with create_window, tkinter wouldn't know what the scrollable region is.

The third problem is that scrollbars require a two-way configuration. The scrollbar command needs to call the yview method of the widget, and the widget's yscrollcommand option needs to call the set method of the scrollbar.

Upvotes: 1

Related Questions