文本小部件上的Python滚动条

时间:2013-10-28 23:10:53

标签: python tkinter

我正在尝试让一个简单的滚动条显示在文本小部件上...我已经导出了以下代码,它添加了滚动条但似乎完全消除了文本小部件。

eula = Tkinter.Text(screen1_2)
eula.insert("1.0", "text")
eula.pack(side="left",fill="both",expand=True)
scroll = Tkinter.Scrollbar(eula)
scroll.pack(side="right",fill="y",expand=False)

1 个答案:

答案 0 :(得分:2)

请注意,您必须在“文本”小部件中定义yscrollcommand=scroll.set,并使用以下行配置滚动条:scroll.config(command=eula.yview) 这是你的代码,用一个滚动条重写:

# Import Tkinter
from Tkinter import *

# define master
master = Tk()

# Vertical (y) Scroll Bar
scroll = Scrollbar(master)
scroll.pack(side=RIGHT, fill=Y)

# Text Widget
eula = Text(master, wrap=NONE, yscrollcommand=scroll.set)
eula.insert("1.0", "text")
eula.pack(side="left")

# Configure the scrollbars
scroll.config(command=eula.yview)
mainloop()

这是一个带有两个滚动条的Tkinter示例:

# Import Tkinter
from Tkinter import *

# define master
master = Tk()

# Horizontal (x) Scroll bar
xscrollbar = Scrollbar(master, orient=HORIZONTAL)
xscrollbar.pack(side=BOTTOM, fill=X)

# Vertical (y) Scroll Bar
yscrollbar = Scrollbar(master)
yscrollbar.pack(side=RIGHT, fill=Y)

# Text Widget
text = Text(master, wrap=NONE,
            xscrollcommand=xscrollbar.set,
            yscrollcommand=yscrollbar.set)
text.pack()

# Configure the scrollbars
xscrollbar.config(command=text.xview)
yscrollbar.config(command=text.yview)

# Run tkinter main loop
mainloop()