Reputation: 11
I am trying to get a vertical scrollbar working with tkinter. I am using the code below. The scroll bar shows up until I uncomment the line
#canvas.configure(yscrollcommand=yscrollbar.set)
What am I doing wrong?
thanks in advance!
import tkinter as tk
from tkinter import ttk
root =tk.Tk()
frame = tk.Frame(root)
frame.pack()
canvas = tk.Canvas(frame, height=200, width=200, background="blue")
yscrollbar = tk.Scrollbar(frame, orient=tk.VERTICAL, command=canvas.yview)
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)
yscrollbar.grid(row=0, column=1, sticky='ns')
canvas.grid(row=0, column=0, sticky='ewns')
canvas.configure(yscrollcommand=yscrollbar.set)
canvas.config(scrollregion=canvas.bbox("all"))
root.mainloop()
Upvotes: 0
Views: 434
Reputation: 11
Thanks, the scrollregion was smaller than the canvas size...
got it working
import tkinter as tk
from tkinter import ttk
root =tk.Tk()
frame = tk.Frame(root, height=100, width=100)
frame.pack()
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)
canvas = tk.Canvas(frame, height=200, width=200, background="blue")
canvas.grid(row=0, column=0, sticky='ewns')
yscrollbar = tk.Scrollbar(frame, orient=tk.VERTICAL, command=canvas.yview)
yscrollbar.grid(row=0, column=1, sticky='ns')
canvas.configure(yscrollcommand=yscrollbar.set)
frame_buttons = tk.Frame(canvas, bg="green")
canvas.create_window((0, 0), window=frame_buttons, anchor="nw")
rows = 9
columns = 5
buttons = [[tk.Button() for j in range(columns)] for i in range(rows)]
for i in range(0, rows):
for j in range(0, columns):
buttons[i][j] = tk.Button(frame_buttons, text=("%d,%d" % (i+1, j+1)))
buttons[i][j].grid(row=i, column=j, sticky='news')
frame_buttons.update_idletasks()
canvas.configure(scrollregion=canvas.bbox("all"))
root.mainloop()
Upvotes: 1