Tkinter Text(文本)

     Tkinter Text(文本): 文本小部件提供先进的功能,让您编辑多行文本格式,如改变其颜色和字体的方式显示.
 
文本小部件提供先进的功能,让您编辑多行文本格式,如改变其颜色和字体的方式显示,.

您还可以使用标签和商标一样优雅的结构,找到特定部分的文本,并更改应用到这些领域。此外,你可以嵌入在文本和图像的窗口,因为这个小工具的目的是要处理的平原和格式化文本.

语法:

这里是一个简单的语法来创建这个widget:

w = Text ( master, option, ... )

参数:

  • master: 这代表了父窗口.

  • options: 下面是这个小工具最常用的选项列表。这些选项可以作为键 - 值对以逗号分隔.

OptionDescription
bgThe default background color of the text widget.
bdThe width of the border around the text widget. Default is 2 pixels.
cursorThe cursor that will appear when the mouse is over the text widget.
exportselectionNormally, text selected within a text widget is exported to be the selection in the window manager. Set exportselection=0 if you don't want that behavior.
fontThe default font for text inserted into the widget.
fgThe color used for text (and bitmaps) within the widget. You can change the color for tagged regions; this option is just the default.
heightThe height of the widget in lines (not pixels!), measured according to the current font size.
highlightbackgroundThe color of the focus highlight when the text widget does not have focus.
highlightcolorThe color of the focus highlight when the text widget has the focus.
highlightthicknessThe thickness of the focus highlight. Default is 1. Set highlightthickness=0 to suppress display of the focus highlight.
insertbackgroundThe color of the insertion cursor. Default is black.
insertborderwidthSize of the 3-D border around the insertion cursor. Default is 0.
insertofftimeThe number of milliseconds the insertion cursor is off during its blink cycle. Set this option to zero to suppress blinking. Default is 300.
insertontimeThe number of milliseconds the insertion cursor is on during its blink cycle. Default is 600.
insertwidthWidth of the insertion cursor (its height is determined by the tallest item in its line). Default is 2 pixels.
padxThe size of the internal padding added to the left and right of the text area. Default is one pixel.
padyThe size of the internal padding added above and below the text area. Default is one pixel.
reliefThe 3-D appearance of the text widget. Default is relief=SUNKEN.
selectbackgroundThe background color to use displaying selected text.
selectborderwidthThe width of the border to use around selected text.
spacing1This option specifies how much extra vertical space is put above each line of text. If a line wraps, this space is added only before the first line it occupies on the display. Default is 0.
spacing2This option specifies how much extra vertical space to add between displayed lines of text when a logical line wraps. Default is 0.
spacing3This option specifies how much extra vertical space is added below each line of text. If a line wraps, this space is added only after the last line it occupies on the display. Default is 0.
stateNormally, text widgets respond to keyboard and mouse events; set state=NORMAL to get this behavior. If you set state=DISABLED, the text widget will not respond, and you won't be able to modify its contents programmatically either.
tabsThis option controls how tab characters position text.
widthThe width of the widget in characters (not pixels!), measured according to the current font size.
wrapThis option controls the display of lines that are too wide. Set wrap=WORD and it will break the line after the last word that will fit. With the default behavior, wrap=CHAR, any line that gets too long will be broken at any character.
xscrollcommandTo make the text widget horizontally scrollable, set this option to the set() method of the horizontal scrollbar.
yscrollcommandTo make the text widget vertically scrollable, set this option to the set() method of the vertical scrollbar.

方法:

文本对象,这些方法:

Methods & Description
delete(startindex [,endindex])
This method deletes a specific character or a range of text.
get(startindex [,endindex])
This method returns a specific character or a range of text.
index(index)
Returns the absolute value of an index based on the given index.
insert(index [,string]...)
This method inserts strings at the specified index location.
see(index)
This method returns true if the text located at the index position is visible.

文本组件支持三种不同的辅助结构:标记,选项卡,和索引:

标记是使用书签在一个给定的文本两个字符之间的位置。处理标记时,我们可用下列方法:

Methods & Description
index(mark)
Returns the line and column location of a specific mark.
mark_gravity(mark [,gravity])
Returns the gravity of the given mark. If the second argument is provided, the gravity is set for the given mark.
mark_names()
Returns all marks from the Text widget.
mark_set(mark, index)
Informs a new position to the given mark.
mark_unset(mark)
Removes the given mark from the Text widget.
 

标签是用来关联名称的文本,这使得容易修改特定的文本区显示设定的任务的地区。标签也可以用来绑定事件回调到特定范围的文字.

以下是可用的方法处理标签:

Methods & Description
tag_add(tagname, startindex[,endindex] ...)
This method tags either the position defined by startindex, or a range delimited by the positions startindex and endindex.
tag_config
You can use this method to configure the tag properties, which include, justify(center, left, or right), tabs(this property has the same functionality of the Text widget tabs's property), and underline(used to underline the tagged text).
tag_delete(tagname)
This method is used to delete and remove a given tag.
tag_remove(tagname [,startindex[.endindex]] ...)
After applying this method, the given tag is removed from the provided area without deleting the actual tag definition.

方法:

自行尝试下面的例子:

from Tkinter import *

def onclick():
   pass

root = Tk()
text = Text(root)
text.insert(INSERT, "Hello.....")
text.insert(END, "Bye Bye.....")
text.pack()

text.tag_add("here", "1.0", "1.4")
text.tag_add("start", "1.8", "1.13")
text.tag_config("here", background="yellow", foreground="blue")
text.tag_config("start", background="black", foreground="green")
root.mainloop()

这将产生以下结果:

 

转载于:https://www.cnblogs.com/tkinter/p/5628833.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值