如何使用 Tkinter 在更大的 window 中为 Canvas 创建滚动条?

How do you create a scrollbar for a Canvas inside a bigger window using Tkinter?

我正在尝试在 canvas 对象中添加一个滚动条,我将在其中导入我的表格。我在 Stack Overflow 上发现了以下内容:tkinter: using scrollbars on a canvas

但问题是,如果不使用*全部导入,我不知道要导入哪些子模块。我还希望滚动条位于 Canvas 内,而不是整个 window 本身。

这是我的基本代码,展示了边栏的外观:

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()

编辑:

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()

提前致谢。

您无需导入任何内容。执行以下操作:

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

并且您必须删除这些行:

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)

您还需要在表格面板上进行一些更改,例如:

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

有关详细信息,请参阅滚动条的文档。