使用 Python 在 td 中定位滚动条

Positioning the scrolbar in tk using Python

正如您在图片中看到的那样,我希望 canvas 滚动条一直向右移动...简而言之,我希望 canvas 覆盖所有红色背景frame 2 有...有什么想法吗?这是代码:(参考下面关于滚动条代码的代码,你会发现here

upFrame = Frame(window, bg='lightgray')
upFrame.grid(row=1, column=0, sticky='nesw')
midFrame = Frame(window, bg='red')
midFrame.grid(row=2, column=0, sticky='nesw')
bottomFrame = Frame(window, bg='lightgray')
bottomFrame.grid(row=3, column=0, sticky='nesw')
    
    
window.grid_rowconfigure(1, weight = 0)
window.grid_columnconfigure(0, weight = 1)
window.grid_rowconfigure(1, weight = 0)

container = Frame(midFrame)
canvas = Canvas(container)
scrollbar = Scrollbar(container, orient="vertical", command=canvas.yview)
scrollable_frame = Frame(canvas)

scrollable_frame.bind(
    "<Configure>",
     lambda e: canvas.configure(
      scrollregion=canvas.bbox("all")
     )
  )
    
canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")

canvas.configure(yscrollcommand=scrollbar.set)
    
for i in range(50):
    Label(scrollable_frame, text="Sample scrolling label").pack()

container.grid(row=2, column=0, sticky='nesw')
canvas.pack(side='left', fill='both', expand=True)
scrollbar.pack(side="right", fill="y")

此外,如果你们能告诉我如何使滚动条随鼠标滚轮移动会很有帮助

下次请添加一个完整的可重现示例,包括导入和主循环,以便更容易理解。

使您的 canvas 可滚动

(来源:tkinter: binding mousewheel to scrollbar):

# bind scrolling to mousewheel
def _scroll_canvas(event):
    canvas.yview_scroll(-1*(int(event.delta/100)), "units")
canvas.bind_all("<MouseWheel>", _scroll_canvas)

将滚动条放在右侧

  1. 你不需要另一个框架只是为了 Canvas,我删除了你的 container 框架并将 Canvas 直接放入你的 midFrame。红色背景也可以去掉。
  2. 同样适用于您的滚动条 - 直接放入您的 midFrame
  3. 对于红色部分,我创建了一个名为 red_frame 的新子框架(在底部)
  4. 如果您希望滚动条出现在屏幕的右侧,请使用带有“右”选项的打包,对于所有其他子框架,请使用“左”选项。其他一切都由您打包小部件的顺序管理。

完整代码:

from tkinter import Frame, Canvas, Scrollbar, Tk, Label

window = Tk()

upFrame = Frame(window, bg='lightgray')
upFrame.grid(row=1, column=0, sticky='nesw')
midFrame = Frame(window, bg='red')
midFrame.grid(row=2, column=0, sticky='nesw')
bottomFrame = Frame(window, bg='lightgray')
bottomFrame.grid(row=3, column=0, sticky='nesw')
    
    
window.grid_rowconfigure(1, weight = 0)
window.grid_columnconfigure(0, weight = 1)
window.grid_rowconfigure(1, weight = 0)

canvas = Canvas(midFrame)
scrollbar = Scrollbar(midFrame, orient="vertical", command=canvas.yview)
scrollable_frame = Frame(canvas)

scrollable_frame.bind(
    "<Configure>",
     lambda e: canvas.configure(
      scrollregion=canvas.bbox("all")
     )
  )
    
canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")

canvas.configure(yscrollcommand=scrollbar.set)

# bind scrolling to mousewheel
def _scroll_canvas(event):
    canvas.yview_scroll(-1*(int(event.delta/100)), "units")
canvas.bind_all("<MouseWheel>", _scroll_canvas)
    
for i in range(50):
    Label(scrollable_frame, text="Sample scrolling label").pack()

# here you decide in which order your widgets appear: canvas, red frame and on the right - your scrollbar
canvas.pack(side='left', fill='both')
red_frame = Frame(midFrame, bg="red")
red_frame.pack(side="left")
scrollbar.pack(side="right", fill="y")

window.mainloop()