Tkinter教程之Text篇(3)

來源:互聯網
上載者:User

標籤:blog   http   使用   ar   for   art   sp   log   on   

本文轉載自: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方法也是不起作用,不知為何???

Tkinter教程之Text篇(3)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.