如何将滚动条添加到Tkinter

时间:2016-12-18 17:36:13

标签: python tkinter scrollbar

Python 3.5.2

这是我的代码的基础:

from tkinter import *
w = Tk()
w.geometry("650x600")

#A very large amount of labels here. Too many to be able to see them all.

w.mainloop()

如何向此处添加滚动条以便能够转到窗口底部? 我没有在网上找到任何特定于此案例的答案。它们都处理列表框,框架或画布。这只是Tkinter窗口。不,我无法编辑我的代码以使用画布或任何东西。

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以以此为例尝试一下:

from tkinter import *

root = Tk()
scrollbar = Scrollbar(root)
scrollbar.pack( side = RIGHT, fill=Y )

w = Label(root, text="Label: ")#some label

mylist = Listbox(root, yscrollcommand = scrollbar.set )
for line in range(100):
   mylist.insert(END, w.cget("text") + str(line))

mylist.pack( side = LEFT, fill = BOTH )
scrollbar.config( command = mylist.yview )

mainloop()

输出:

enter image description here