我的滚动条应该是Canvas_2的子级,并控制Canvas_3的y值。但是,它没有按预期工作。滚动条不会向上或向下移动,并且还有一个不可见的蓝色区域。关于我在这里缺少什么的任何想法?非常感谢您的宝贵时间。
import tkinter as tk
from PIL import ImageTk, Image
# To initialize tkinter, we have to create a Tk root widget,
# which is a window with a title bar and other decoration
# provided by the window manager.
# The root widget has to be created before any other widgets
# and there can only be one root widget.
root = tk.Tk()
# The weight of a row or column determines how much of the
# available space a row or column should occupy relative to
# the other rows or columns. For example, a column with a
# weight of 2 will be twice as wide as a column with a
# weight of 1, assuming there's space for the widgets to fit.
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
load1 = Image.open("example.jpg")
render1 = ImageTk.PhotoImage(load1)
# Creating a class for filling each row
def makeRow(top, img, row):
r = row
if row == 0:
c1 = "#75dce1"
c2 = "#75dce1"
e7 = tk.Entry(top, bg=c2).grid(row=r, column=6, sticky="news")
e8 = tk.Entry(top, bg=c2).grid(row=r, column=7, sticky="news")
else:
c1 = "#a9d08e"
c2 = "#8dd1bf"
img = tk.Label(top, image=render1, bg="green").grid(row=r, column=6, sticky="news")
e1 = tk.Entry(top, bg=c1).grid(row=r, column=0, sticky="news")
e2 = tk.Entry(top, bg=c1).grid(row=r, column=1, sticky="news")
e3 = tk.Entry(top, bg=c1).grid(row=r, column=2, sticky="news")
e4 = tk.Entry(top, bg=c1).grid(row=r, column=3, sticky="news")
e5 = tk.Entry(top, bg=c2).grid(row=r, column=4, sticky="news")
e6 = tk.Entry(top, bg=c2).grid(row=r, column=5, sticky="news")
# load1 = Image.open(img)
# render1 = ImageTk.PhotoImage(load1)
# The canv_1 is a child of the parent "root"
# canv_1 contains: canv_2 (frozen top row) and canv_3 (bottom rows with a vertical scroll)
canv_1 = tk.Canvas(root, bg="blue")
canv_1.grid_rowconfigure(0, weight=1)
canv_1.grid_rowconfigure(1, weight=10)
canv_1.grid_columnconfigure(0, weight=1)
canv_1.grid(row=0, column=0, sticky = "news")
canv_1.grid(row=1, column=0, sticky = "news")
# The canv_2 is a child of the parent "canv_1"
canv_2 = tk.Canvas(canv_1, bg="blue")
canv_2.grid_rowconfigure(0, weight=1)
canv_2.grid_rowconfigure(1, weight=1)
canv_2.grid_columnconfigure(0, weight=1)
canv_2.grid(row=0, column=0, sticky = "news")
canv_2.grid(row=1, column=0, sticky = "news")
# The canv_3 is a child of the parent "canv_2"
canv_3 = tk.Canvas(canv_2, bg="blue")
canv_3.grid(row=1, column=0, sticky="news")
# canv_3.grid_rowconfigure((1,2,3,4,5), weight=1)
# canv_3.grid_columnconfigure((1,2,3,4,5), weight=1)
slides = []
for i in range(10):
slides.append(i)
if i==0:
slides[i] = makeRow(canv_3,"", 0)
else:
slides[i] = makeRow(canv_3, "example.jpg", i)
# Create Scrollbar
vsb = tk.Scrollbar(canv_2, orient="vertical", command=canv_3.yview)
vsb.grid(row=1, column=1, sticky='ns')
canv_2.configure(yscrollcommand=vsb.set)
canv_2.config(scrollregion=canv_3.bbox("all"))
canv_2.configure(scrollregion=(0, 0, 5000, 5000))
root.mainloop()
这是实际输出:
这是所需的设计和输出:
答案 0 :(得分:1)
该代码至少存在三个基本问题。最大的问题是您正在使用canv_3
将小部件添加到grid
。画布无法滚动使用grid
添加到画布的项目。它只会滚动使用画布的“创建”方法添加的项目(create_window
,create_text
等)。
第二个问题是,您永远不会为scrollregion
定义适当的canv_3
,因此即使您添加了带有create_window
的项目,tkinter也不知道可滚动区域是什么。
第三个问题是滚动条需要双向配置。 scrollbar命令需要调用小部件的yview
方法,而小部件的yscrollcommand
选项需要调用滚动条的set
方法。