因为还要设置Canvas的滑动范围:
通过canvas的对象方法scrollregion设置
如,设置为全部,则为canvas1.configure(scrollregion=c.bbox("all"))
示例代码(ChatGPT写的):
from tkinter import * def on_canvas_configure(event): canvas.configure(scrollregion=canvas.bbox("all")) root = Tk() # 创建滚动条 scrollbar_y = Scrollbar(root, orient="vertical") # 创建Canvas,并绑定滚动条 canvas = Canvas(root, yscrollcommand=scrollbar_y.set) scrollbar_y.config(command=canvas.yview) # 将滚动条与Canvas进行布局 scrollbar_y.pack(side="right", fill="y") # scrollbar_x.pack(side="bottom", fill="x") canvas.pack(side="left", fill="both", expand=True) # 添加内容到Canvas frame = Frame(canvas) canvas.create_window((0, 0), window=frame, anchor="nw") # 配置Canvas滚动区域 canvas.bind("<Configure>", on_canvas_configure) # 往Canvas中添加一些示例内容 for i in range(100): label = Label(frame, text="Label {}".format(i)) label.pack() root.mainloop()
标签:Canvas,tkinter,configure,canvas,scrollbar,变灰,root,pack From: https://www.cnblogs.com/roundfish/p/17623483.html