0

I'm trying to add a scrollbar inside a canvas object where I will be importing my tables. I found the following on Stack Overflow: tkinter: using scrollbars on a canvas

But the problem is that I do not know which sub-modules to import without using * to import all. I also wants the scroll bar to be inside a Canvas not the whole window itself.

This is my base code showcasing what the side bar will look like:

import tkinter as tk

##Tab Settings:
root=tk.Tk()
root.title("Virtual Desktop")
root.resizable(False, False)

#Tab Resolution
canvas = tk.Canvas(root, width=1080, height=720, bg="LightBlue")
canvas.pack()

#Generic Canvas Where An Table Will Be Imported Into
tablePanel = tk.Canvas(root, width=580, height=250, bg="Blue")
canvas.create_window(540, 360, window=tablePanel)


###Desired Horizontal Sidebar Location (Actual Scrollbar Haven't Added)

#White Sidebar Background
backBar = sideBar = tk.Canvas(root, width=25, height=248, bg="LightGrey")
canvas.create_window(816, 360, window=sideBar)

#Sidebar
sideBar = tk.Canvas(root, width=18, height=150, bg="Grey")
canvas.create_window(816, 316, window=sideBar)


root.mainloop()

Edit:

sideBar=tk.Scrollbar(root, orient='vertical',command = tablePanel.yview())
sideBar["command"]=tablePanel.yview
canvas.create_window(816, 360, window=sideBar)

contents = tk.Canvas(root, width=100, height=100, bg="Green")
tablePanel.create_window(300, 200, window=contents)

contents2 = tk.Canvas(root, width=100, height=100, bg="Green")
tablePanel.create_window(300, 90, window=contents2)

root.mainloop()

Thanks in advance.

1 Answers1

1

You don't have to import anything . Do the following :

    sideBar=tk.Scrollbar(root, orient='vertical',command = tablePanel.yview())
    sideBar["command"]=tablePanel.yview
    canvas.create_window(816, 360, window=sideBar)

and you have to delete those lines :

backBar = sideBar = tk.Canvas(root, width=25, height=248, bg="LightGrey")
canvas.create_window(816, 360, window=sideBar)

#Sidebar
sideBar = tk.Canvas(root, width=18, height=150, bg="Grey")
canvas.create_window(816, 316, window=sideBar)

you need also to make some changes on the tablepanel like :

tablePanel = tk.Canvas(root, width=580, height=250, bg="Blue",scrollregion=(0,0,0,1200)) 

for more information see the documentation for the scrollbar .

Ayyoub ESSADEQ
  • 776
  • 2
  • 14
  • Okay thanks. I'll test it with the code now and come back with the result. – BartSimpson Sep 19 '21 at 11:06
  • 1
    If the answer was helpful . please assign it as helpful . thanks – Ayyoub ESSADEQ Sep 19 '21 at 11:07
  • Thanks, the code does seems to work without importing anything else other than tkinter. However, I still haven't been able to figure out how to use it for my 'tablePanel' canvas; I want to increase the height of the panel but not having it to show up unless using the sidebar. – BartSimpson Sep 19 '21 at 11:16
  • 1
    what do you mean by ''increase the height of the panel" ? (do you mean to make the Panel bigger ) – Ayyoub ESSADEQ Sep 19 '21 at 11:21
  • Kind of. I want to increase the height of the 'tablePanel' without it displaying the whole full panel size; I want to use the sidebar to navigate its height. – BartSimpson Sep 19 '21 at 11:47
  • 1
    my script does the job . To demonstrate that you have to create something on the tablepanel to see the effect . – Ayyoub ESSADEQ Sep 19 '21 at 13:20
  • 1
    I make some edits on my answer . take a look at it . – Ayyoub ESSADEQ Sep 19 '21 at 13:30
  • Ah, I see thank you. The scrollbar does seems to function now, but the content inside the tablePanel seems to only disappear when the entire content/box is out of the panel area (I have edited post) – BartSimpson Sep 19 '21 at 14:10
  • Thank you, I managed to solve it (kind of) by placing another canvas that is same colour to the root background and have it cover the items when it scrolls out. – BartSimpson Sep 19 '21 at 18:52