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.
CodePudding user response:
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 .