[译][Tkinter 教程10] Text 控件

已获原做者受权. 原系列地址: Python Tkinter

简介及简例

Text 控件用来显示多行文本. Tkinter 的 Text 控件很强大, 很灵活, 能够实现不少功能. 虽然这个控件的主要用途是显示多行文本, 但其还能够被用做简单的文本编辑器, 甚至是网页浏览器.
Text 控件能够显示网页连接, 图片, HTML页面, 甚至 CSS 样式表.
在其余的各类教程中, 很难找到一个关于 Text 控件的简单例子. 这也是咱们写这一章教程的主要目的:
咱们使用构造方法建立了一个 Text 控件, 设置其高度为 2 (不是像素高度, 而是两行字符的高度), 设置其宽度为 30 (不是像素宽度, 是30个字符的宽度), 而后使用 insert() 方法插入两行文本.php

from Tkinter import *

root = Tk()
T = Text(root, height=2, width=30)
T.pack()
T.insert(END, "Just a text Widget\nin two lines\n")
mainloop()

运行后窗口的样子很可爱:
python

让咱们对上面的例子作一点小小的改动. 咱们加入了另外一段文字, 哈姆雷特那段著名的开场白:segmentfault

from Tkinter import *

root = Tk()
T = Text(root, height=2, width=30)
T.pack()
quote = """HAMLET: To be, or not to be--that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune
Or to take arms against a sea of troubles
And by opposing end them. To die, to sleep--
No more--and by a sleep to say we end
The heartache, and the thousand natural shocks
That flesh is heir to. 'Tis a consummation
Devoutly to be wished."""
T.insert(END, quote)
mainloop()

运行上面的例子后, 产生的窗口并很差看. 在窗口中咱们只能看到这段独白的第一行, 而且还被断为两行. 窗口只显示两行文字, 是由于咱们将 Text 控件高度设置为 2 行文字. 文本自动断行, 是由于咱们将 Text 控件宽度设置为 30 个字符.
浏览器

这个问题的一个解决办法是, 将 Text 控件的高度设置为这段文本的行数, 将 Text 控件的宽度设置为这段文本中最长的那行的字符数.
但更好的解决办法是设置滚动, 就像咱们经常使用的浏览器等应用中那样.编辑器

滚动条

如今让咱们来为咱们的应用加入一个滚动条. Tkinter 提供了 Scrollbar() 方法来实现这一目的, 其所接受的惟一参数为当前窗口应用的 Tkinter root 对象.ide

from Tkinter import *

root = Tk()
S = Scrollbar(root)
T = Text(root, height=4, width=50)
S.pack(side=RIGHT, fill=Y)
T.pack(side=LEFT, fill=Y)
S.config(command=T.yview)
T.config(yscrollcommand=S.set)
quote = """HAMLET: To be, or not to be--that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune
Or to take arms against a sea of troubles
And by opposing end them. To die, to sleep--
No more--and by a sleep to say we end
The heartache, and the thousand natural shocks
That flesh is heir to. 'Tis a consummation
Devoutly to be wished."""
T.insert(END, quote)
mainloop(  )

如今这个窗口看起来顺眼多了, 视口中老是显示4行文字, 但全部行均可以经过拖动滚动条看到:
oop

加入图片

下面的例子中, 咱们在一个 Text 控件中显示了一张图片, 并为另外一个单行的 Text 控件绑定了一个点击事件:布局

from Tkinter import *

root = Tk()

text1 = Text(root, height=20, width=30)
photo=PhotoImage(file='./William_Shakespeare.gif')
text1.insert(END,'\n')
text1.image_create(END, image=photo)

text1.pack(side=LEFT)

text2 = Text(root, height=20, width=50)
scroll = Scrollbar(root, command=text2.yview)
text2.configure(yscrollcommand=scroll.set)
text2.tag_configure('bold_italics', font=('Arial', 12, 'bold', 'italic'))
text2.tag_configure('big', font=('Verdana', 20, 'bold'))
text2.tag_configure('color', foreground='#476042', 
                        font=('Tempus Sans ITC', 12, 'bold'))
text2.tag_bind('follow', '<1>', lambda e, t=text2: t.insert(END, "Not now, maybe later!"))
text2.insert(END,'\nWilliam Shakespeare\n', 'big')
quote = """
To be, or not to be that is the question:
Whether 'tis Nobler in the mind to suffer
The Slings and Arrows of outrageous Fortune,
Or to take Arms against a Sea of troubles,
"""
text2.insert(END, quote, 'color')
text2.insert(END, 'follow-up\n', 'follow')
text2.pack(side=LEFT)
scroll.pack(side=RIGHT, fill=Y)

root.mainloop()


全系列:
[译][Tkinter 教程01] 入门: Label 控件
[译][Tkinter 教程02] Message 控件
[译][Tkinter 教程03] Button 控件
[译][Tkinter 教程04] Variable 类
[译][Tinkter 教程05] Radiobutton 控件
[译][Tkinter 教程06] Checkbox 控件
[译][Tkinter 教程07] Entry 控件
[译][Tkinter 教程08] Canvas 图形绘制
[译][Tkinter 教程09] Scale 控件
[译][Tkinter 教程10] Text 控件
[译][Tkinter 教程11] 对话框和消息框
[译][Tkinter 教程12] 布局管理 (Pack Place Grid)
[译][Tkinter 教程13] Mastermind 游戏
[译][Tkinter 教程14] menu 菜单
[译][Tkinter 教程15] event 事件绑定
译者水平有限, 若有疏漏, 欢迎指正.
已得到原做者受权. 原文地址: Text Widget.
相关文章
相关标签/搜索