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

已获原作者授权. 原系列地址: Python Tkinter

简介及简例

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

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()

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

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

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 对象.

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行文字, 但所有行都可以通过拖动滚动条看到:

加入图片

下面的例子中, 我们在一个 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.
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值