如何将滚动条移动到文本小部件内部

时间:2018-06-02 17:29:14

标签: python-3.x tkinter

我有这段代码

from tkinter import *

   root = Tk()
   root.geometry("1200x1000+30+30")
   # width x height + x_offset + y_offset:
   T = Text(root, height=10, width=100)
   T.place(x=20, y=30)
   for i in range(40):
      T.insert(END, "This is line %d\n" % i)


   yscroll = Scrollbar(command=T.yview, orient=VERTICAL)
   T.configure(yscrollcommand=yscroll.set)
   yscroll.pack(side="right", fill="y", expand=False)

   root.mainloop()

滚动条位于窗口框架上,有没有办法将其移动到文本区域的内部和右边缘?

1 个答案:

答案 0 :(得分:1)

可能有另一种方式,但我会使用ScrolledText中包含的tkinter.scrolledtext小部件。

from tkinter import *
import tkinter.scrolledText

root = Tk()
root.geometry("1200x1000+30+30")
# width x height + x_offset + y_offset:
T = ScrolledText(root, height=10, width=100)
T.place(x=20, y=30)
for i in range(40):
    T.insert(END, "This is line %d\n" % i)

root.mainloop()

这会自动将滚动条放在文本框中。

来自this answer我还得知您可以使用place代替pack来定位滚动条,然后使用in_=T

from tkinter import *

root = Tk()
root.geometry("1200x1000+30+30")
# width x height + x_offset + y_offset:
T = Text(root, height=10, width=100)
T.place(x=20, y=30)
for i in range(40):
    T.insert(END, "This is line %d\n" % i)


yscroll = Scrollbar(command=T.yview, orient=VERTICAL)
yscroll.place(in_=T, relx=1.0, relheight=1.0, bordermode="outside")
T.configure(yscrollcommand=yscroll.set)

root.mainloop()
相关问题