Python Tkinter教程之Text(1)篇

''' Tkinter教程之Text篇(1) '''
''' 1.创建第一个Text '''
from  Tkinter  import   *
root 
=  Tk()
=  Text(root)
t.pack()
root.mainloop()
# root中含有一Text控件,可以在这个控件内输入文本,可以使用Ctrl+C/V向Text内添加剪切板上的内容(文本),不接受Ctrl+Z执行操作
''' 2.向Text中添加文本 '''
# insert方法添加文本内容
from  Tkinter  import   *
root 
=  Tk()
=  Text(root)
# 向第一行,第一列添加文本0123456789
t.insert( 1.0 , ' 0123456789 ' )
# 向第一行第一列添加文本ABCDEFGHIJ
t.insert( 1.0 , ' ABCDEFGHIJ ' )
t.pack()
root.mainloop()
# insert的第一个参数为索引;第二个为添加的内容
''' 3.使用line.col索引添加内容 '''
# 使用indexes来添加Text的内容
#
 -*- coding: cp936 -*-
from  Tkinter  import   *
root 
=  Tk()
=  Text(root)
#  向第一行,第一列添加文本0123456789
t.insert( 1.0 , ' 0123456789 ' )
t.insert(
' 2.end ' , '   ' )
#  向第一行第一列添加文本ABCDEFGHIJ
t.insert( 2.5 , ' ABCDEFGHIJ ' )
t.pack()
root.mainloop()
#  可以看到使用indexes时,如果其值超过了Text的buffer值,程序不会抛出异常,它会使用向给定值靠近。
''' mark是用来表示在Text中位置的一类符号 '''
''' 4.使用内置的mark控制添加位置 '''
# 演示了内置的mark:INSERT/CURRENT/END/SEL_FIRST/SEL_LAST的用法
#
 -*- coding: cp936 -*-
from  Tkinter  import   *
root 
=  Tk()
=  Text(root)
# 向Text中添加10行文本
for  i  in  range( 1 , 10 ):
    t.insert(
1.0 , ' 0123456789  ' )
# 定义各个Button的回调函数,这些函数使用了内置的mark:INSERT/CURRENT/END/SEL_FIRST/SEL_LAST
def  insertText():
    t.insert(INSERT,
' jcodeer ' )
def  currentText():
    t.insert(CURRENT,
' jcodeer ' )
def  endText():
    t.insert(END,
' jcodeer ' )
def  selFirstText():
    t.insert(SEL_FIRST,
' jcodeer ' )
def  selLastText():
    t.insert(SEL_LAST,
' jcodeer ' )
# INSERT    
Button(root,
       text 
=   ' insert jcodeer at INSERT ' ,
       command 
=  insertText
       ).pack(fill 
=  X)
# CURRENT
Button(root,
       text 
=   ' insert jcodeer at CURRENT ' ,
       command 
=  insertText
       ).pack(fill 
=  X)
# END
Button(root,
       text 
=   ' insert jcodeer at END ' ,
       command 
=  endText
       ).pack(fill 
=  X)
# SEL_FIRST
Button(root,
       text 
=   ' insert jcodeer at SEL_FIRST ' ,
       command 
=  selFirstText
       ).pack(fill 
=  X)
# SEL_LAST
Button(root,
       text 
=   ' insert jcodeer at SEL_LAST ' ,
       command 
=  selLastText
       ).pack(fill 
=  X)

t.pack()
root.mainloop()
# 几个内置的mark:
#
INSERT:光标的插入点
#
CURRENT:鼠标的当前位置所对应的字符位置
#
END:这个Text buffer的最后一个字符
#
SEL_FIRST:选中文本域的第一个字符,如果没有选中区域则会引发异常
#
SEL_LAST:选中文本域的最后一个字符,如果没有选中区域则会引发 异常

''' 5.使用表达式来增强mark '''
# 表达式(expression)可以个性任何的Indexes,如下:
'''
+ count chars :前移count字符
- count chars :后移count字符
+ count lines :前移count行
- count lines :后移count行
linestart:移动到行的开始
linesend:移动到行的结束
wordstart:移动到字的开始
wordend:移动到字的结束
'''
#  演示修饰符表达式的使用方法,如何与当前可用的indexes一起使用
#
 -*- coding: cp936 -*-
from  Tkinter  import   *
root 
=  Tk()
=  Text()
#  向第一行,第一列添加文本0123456789
for  i  in  range( 1 , 10 ):
    t.insert(
1.0 , ' 0123456789  ' )
=   ' test_mark '
def  forwardChars():
    
#  直接连接字符串
     #  t.mark_set(a,CURRENT + '+ 5 chars')
    t.mark_set(a,CURRENT  +   ' +5c ' )
def  backwardChars():
    
#  t.mark_set(a,CURRENT + '- 5 chars')
    t.mark_set(a,CURRENT  +   ' -5c ' )
def  forwardLines():
    
#  t.mark_set(a,CURRENT + '+ 5 lines)
    t.mark_set(a,CURRENT  +   ' +5l ' )
def  backwardLines():
    
#  t.mark_set(a,CURRENT + '- 5 lines)
    t.mark_set(a,CURRENT  +   ' -5l ' )
def  lineStart():
    
#  注意linestart前面的那个空格不可省略
    t.mark_set(a,CURRENT  +   '  linestart ' )
def  lineEnd():
    
#  注意lineend前面的那个空格不可省略
    t.mark_set(a,CURRENT  +    '  lineend ' )
def  wordStart():
    
#  移动到当前字的开始。
    t.mark_set(a,CURRENT  +   '  wordstart ' )
def  wordend():
    
#  移动到当前字的结束
    t.mark_set(a,CURRENT  +   '  wordend ' )
#  mark:test_mark默认值为CURRENT
t.mark_set(a,CURRENT)    
Button(root,text 
=   ' forward 5 chars ' ,command  =  forwardChars).pack(fill  =  X)
Button(root,text 
=   ' backward 5 chars ' ,command  =  backwardChars).pack(fill  =  X)
Button(root,text 
=   ' forward 5 lines ' ,command  =  forwardLines).pack(fill  =  X)
Button(root,text 
=   ' backward 5 lines ' ,command  =  backwardLines).pack(fill  =  X)
Button(root,text 
=   ' line start ' ,command  =  lineStart).pack(fill  =  X)
Button(root,text 
=   ' line end ' ,command  =  lineEnd).pack(fill  =  X)
Button(root,text 
=   ' word start ' ,command  =  lineEnd).pack(fill  =  X)
Button(root,text 
=   ' word end ' ,command  =  lineEnd).pack(fill  =  X)
#  测试三个位置的不同,CURRENT可以得知是当前光标的位置;mark就表示mark的位置了,INSERT好像一植都在1.0处没有改变。
def  insertText():
    t.insert(INSERT,
' insert ' )
def  currentText():
    t.insert(CURRENT,
' current ' )
def  markText():
    t.insert(a,
' mark ' )
Button(root,text 
=   ' insert jcodeer.cublog.cn ' ,command  =  insertText).pack(fill  =  X)
Button(root,text 
=   ' current jcodeer.cublog.cn ' ,command  =  currentText).pack(fill  =  X)
Button(root,text 
=   ' mark jcodeer.cublog.cn ' ,command  =  markText).pack(fill  =  X)
t.pack()
root.mainloop()


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值