-16

Creating a scrollbar for canvas window is really hard to achieve with Tkinter.I somehow managed to create a vertical scrollbar but now I have no idea to create a horizontal scrollbar. If possible please share sample code for creating a double frame for canvas window.

Shabbir Hussain
  • 2,600
  • 2
  • 17
  • 25

1 Answers1

4

Normally I don't give answers to questions that show no signs of research or experimentation, but it's clear you're struggling. Here's an example of a 1000x1000 canvas in a 500x500 window, with some random rectangles to illustrate that the scrolling works.

import tkinter as tk
import random

root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=500, scrollregion=(0,0,999,999), bg="black")
vsb = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
hsb = tk.Scrollbar(root, orient="horizontal", command=canvas.xview)
canvas.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set)

root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

vsb.grid(row=0, column=1, sticky="ns")
hsb.grid(row=1, column=0, sticky="ew")
canvas.grid(row=0, column=0, sticky="nsew")

for i in range(100):
    color = random.choice(("red", "orange", "yellow", "green", "blue", "violet"))
    width = random.randint(20, 100)
    height = random.randint(20, 100)
    x0 = random.randint(10, 990-width)
    y0 = random.randint(10, 990-height)
    canvas.create_rectangle(x0, y0, x0+width, y0+height, outline='white', fill=color)

root.mainloop()

screenshot

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685