zoukankan      html  css  js  c++  java
  • Tkinter教程之Text篇(3)

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1811348

    '''Tkinter教程之Text篇(3)'''
    '''14.自定义tag的两个内置属性'''''
    #tag.first:tag之前插入文本,此文本不包含在这个tag中
    #tag.last:tag之后插入文本,此文本包含在这个tag中
    # -*- coding: cp936 -*-
    # 使用tag的内置属性来插入文本
    from Tkinter import *
    root = Tk()
    t = Text(root)
    # 创建一个TAG,其前景色为蓝色
    t.tag_config('b',foreground = 'blue')
    for i in range(10):
        t.insert(1.0,'0123456789 ')
    # 自定义两个mark,并使用它们来指定添加tag的文本块
    t.mark_set('ab','3.1')
    t.mark_set('cd',END)
    t.tag_add('b','ab','cd')
    # 删除tag 'b',注意这个操作是在tag_add之后进行的。
    # 在tag('b')之前插入'first'
    t.insert('b.first','first')
    # 在tag('b')之后插入'last'
    t.insert('b.last','last')
    t.pack()
    root.mainloop()
    # 注意:first没有使用tag('b')属性,last使用了tag('b')属性
    '''15.在Text中创建按钮'''
    # -*- coding: cp936 -*-
    # 使用window_create在Text内创建一widget
    from Tkinter import *
    root = Tk()
    t = Text(root)
    for i in range(10):
        t.insert(1.0,'0123456789 ')
    def printText():
        print 'buttin in text'
    bt = Button(t,text = 'button',command = printText)
    # 在Text内创建一个按钮
    t.window_create('2.0',window = bt)
    # 没有调用pack()
    # bt.pack()
    t.pack()
    root.mainloop()
    # 注意:使用window_create,而不是使用insert('2.0',bt);pack()也不用调用;
    # 点击这个按钮,打印出'button in text',证明这个按钮是可以正常工作的。
    '''16.在Text中创建一个图像(未实现)'''
    # -*- coding: cp936 -*-
    # 使用window_create在Text内创建一widget
    from Tkinter import *
    root = Tk()
    t = Text(root)
    for i in range(10):
        t.insert(1.0,'0123456789 ')
    # 分别使用BitmapImage和PhotoImage进行测试,均没有显示出图像???
    #bm = BitmapImage('gray75')
    bm = PhotoImage('c:/python.gif')
    # 在Text内创建一个图像
    t.image_create('2.0',image = bm)
    print t.image_names()
    # 打印的图像名称都是正确的
    t.pack()
    root.mainloop()
    # 按照手册中的说明未实现这种效果,原因不知。
    '''17.绑定tag与事件'''
    # -*- coding: cp936 -*-
    # 使用tag_bind方法
    from Tkinter import *
    root = Tk()
    t = Text(root)
    for i in range(10):
        t.insert(1.0,'0123456789 ')
    # 创建一个tag
    t.tag_config('a',foreground = 'blue',underline = 1)
    # Enter的回调函数
    def enterTag(event):
        print 'Enter event'
    # 绑定tag('a')与事件('<Enter>')
    t.tag_bind('a','<Enter>',enterTag)
    t.insert(2.0,'Enter event ','a')
    t.pack()
    root.mainloop()
    # 注意:使用tag_bind绑定tag与事件,当此事件在tag上发生时便就会调用这个tag的回调函数
    # 因为使用了Enter事件,此事件含有一个参数,故将enterTag加了一个参数,程序中不使用此参数
    '''18.使用edit_xxx实现编辑常用功能(未实现)'''
    # -*- coding: cp936 -*-
    # 使用edit_xxx函数实现编辑常用功能
    from Tkinter import *
    root = Tk()
    t = Text(root)
    for i in range(10):
        t.insert(1.0,'0123456789 ')
    t.pack()
    # 定义回调函数
    # 撤消回调函数
    def undoText():
        t.edit_undo()
    # 插入文本函数
    def insertText():
        t.insert(1.0,'insert text')
    Button(root,text = 'undo',command = undoText).pack(fill = X)
    Button(root,text = 'insert text',command = insertText).pack(fill = X)

    root.mainloop()
    # 这个edit_undo方法也是不起作用,不知为何???

  • 相关阅读:
    Python3---内建函数---all()
    (dp)Codeforces Round #418 (Div. 2) C. An impassioned circulation of affection
    (状压dp)codevs2800 送外卖
    (dp)CF 813 Educational Codeforces Round 22 D. Two Melodies
    (线段树)CF813 Educational Codeforces Round 22 E
    (trie)HDU1251 统计难题
    (最大流)CodeForces
    (高斯消元)HDU2827 The Evaluation of Determinant
    (三分)HDU5531 Rebuild
    (并查集)Codeforces 325 D-Reclamation
  • 原文地址:https://www.cnblogs.com/LeeZz/p/3984336.html
Copyright © 2011-2022 走看看