【发布时间】:2016-03-27 17:56:56
【问题描述】:
我想在 Canvas 中放置例如 20 个文本框。所以我的小部件层次结构是主窗口-> 画布-> 文本框。文本框无法全部放入画布中,因此我想在其上附加一个垂直滚动条。这是我尝试过的:
from tkinter import *
root = Tk()
root_height = root.winfo_screenheight()
root_width = root.winfo_screenwidth()
root.geometry("%dx%d+0+0" % (root_width, root_height))
canvas = Canvas(root, height=root_height, width=root_width)
canvas.pack(fill=BOTH, expand=1)
scrollbar = Scrollbar(canvas)
scrollbar.pack(side=RIGHT, fill=Y)
canvas.config(yscrollcommand=scrollbar.set)
textBoxes = []
for i in range(0, 20):
textBoxes.append(Text(canvas, height=1, width=20, bd=2))
y_offset = 15
for i in range(0, 20):
textBoxes[i].place(x=10, y=y_offset)
y_offset += 60
scrollbar.config(command=canvas.yview)
mainloop()
所以基本上,我试着做我从教程和其他问题中理解的东西 -
将小部件的(画布)
yscrollcommand回调设置为滚动条的 set 方法。将滚动条的命令设置为小部件(画布)的
yview方法。
不幸的是,滚动条无法点击。我哪里错了,我怎样才能达到预期的行为?
【问题讨论】: