Tkinter Text加上滚动条Scrollbar设计

Tkinter Text加上滚动条Scrollbar设计

将Scrollbar与Listbox进行过结合,我们可以参考该节思想将Scrollbar应用在Text控件中。

示例1

将原先只显示3行文字改成显示5行文字,另外主要是将Scrollbar应用在Text控件中,让整个Text文字区域增加y轴的滚动条。

from tkinter import * 

root = Tk()
root.title("apidemos.com")

yscrollbar = Scrollbar(root)
text = Text(root,height=5,width=30)
yscrollbar.pack(side=RIGHT,fill=Y)
text.pack() 
yscrollbar.config(command=text.yview)
text.config(yscrollcommand=yscrollbar.set)

myString = \
"""apidemos.com provides tutorials on the basics of programming, introducing the basics of various programming languages such as HTML, CSS, Javascript, Python, Java, Ruby, C, PHP , MySQL and more. The site also provides a large number of online examples, through the examples, you can better learn programming..."""
text.insert(END,myString)

root.mainloop()

输出:

Tkinter Text加上滚动条Scrollbar设计

我们可以拖动垂直滚动条,向下拖动查看更多内容了。

Tkinter Text加上滚动条Scrollbar设计

示例2

增加x轴的滚动条。请留意第8行,若是想显示x轴的滚动条必须设置wrap="none"。

from tkinter import * 

root = Tk()
root.title("apidemos.com")

xscrollbar = Scrollbar(root,orient=HORIZONTAL)
yscrollbar = Scrollbar(root)
text = Text(root,height=5,width=30,wrap="none")
xscrollbar.pack(side=BOTTOM,fill=X)
yscrollbar.pack(side=RIGHT,fill=Y)
text.pack() 
xscrollbar.config(command=text.xview)
yscrollbar.config(command=text.yview)
text.config(xscrollcommand=xscrollbar.set)
text.config(yscrollcommand=yscrollbar.set)

myString = \
"""apidemos.com provides tutorials on the basics of programming, introducing the basics of various programming languages such as HTML, CSS, Javascript, Python, Java, Ruby, C, PHP , MySQL and more. The site also provides a large number of online examples, through the examples, you can better learn programming..."""
text.insert(END,myString)
# text.insert(INSERT,myString)
root.mainloop()

输出:

Tkinter Text加上滚动条Scrollbar设计

可以拖动水平滚动条左右移动,查看完整的内容。

Tkinter Text加上滚动条Scrollbar设计

如果我们将窗口变大,仍然可以看到所设置的Text文字区域,由于我们没有使用fill或expand参数做更进一步的设置,所以Text文字区域将保持第9行height和width的参数设置,不会更改。

设计Text文字区域时,如果想让此区域随着窗口更改大小,在使用pack( )时,可适度地使用fill和expand参数。

示例3

让Text文字区域随着窗口扩充而扩充。为了让文字区域明显,将此区域的背景设为黄色,可参考第9行的设置。第9行则是让窗口扩充时,Text文字区域也同步扩充。

from tkinter import * 

root = Tk()
root.title("apidemos.com")

xscrollbar = Scrollbar(root,orient=HORIZONTAL)
yscrollbar = Scrollbar(root)
text = Text(root,height=5,width=30,wrap="none",bg="lightyellow")
xscrollbar.pack(side=BOTTOM,fill=X)
yscrollbar.pack(side=RIGHT,fill=Y)
text.pack(fill=BOTH,expand=True) # text.pack(fill=X,expand=True) 
xscrollbar.config(command=text.xview)
yscrollbar.config(command=text.yview)
text.config(xscrollcommand=xscrollbar.set)
text.config(yscrollcommand=yscrollbar.set)

myString = \
"""apidemos.com provides tutorials on the basics of programming, introducing the basics of various programming languages such as HTML, CSS, Javascript, Python, Java, Ruby, C, PHP , MySQL and more. The site also provides a large number of online examples, through the examples, you can better learn programming..."""
text.insert(END,myString)
# text.insert(INSERT,myString)
root.mainloop()

输出:

Tkinter Text加上滚动条Scrollbar设计

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程