原文地址: http://blog.csdn.net/bemorequiet/article/details/54743889
這篇博客主要是簡單的說一下Tkinter中的Text控件的相關知識。
Text文本組件用於顯示和處理多行文本。在Tkinter的所有組件中,Text組件顯得異常強大和靈活,它適用於處理多任務,雖然該組件的主要目的是顯示多行文本,但它常常被用於作為簡單的文本編輯器和網頁瀏覽器使用。
當創建一個Text組件的時候里面是沒有內容的。為了給其插入內容,可以使用insert()以及INSERT或END索引號。
1.普通的Text組件
from tkinter import *
root = Tk()
text1 = Text(root,width=30,height=4)
#INSERT索引表示在光標處插入 text1.insert(INSERT,'I Love') #END索引號表示在最后插入 text1.insert(END,' you') text1.pack() mainloop()
2.插入Button之后的Text組件
from tkinter import * root = Tk() text1 = Text(root,width=30,height=2) text1.pack() text1.insert(INSERT,'I love you') def show(): print('吆喝,我被點了一下') #text還可以插入按鈕 圖片等 b1 = Button(text1,text='點我點我',command=show) #在text創建組件的命令 text1.window_create(INSERT,window=b1) mainloop()
3.插入圖片之后的Text組件
from tkinter import * root = Tk() text1 = Text(root,width=100,height=30) text1.pack() photo = PhotoImage(file='text.gif') def show(): #添加圖片用image_create text1.image_create(END,image=photo) b1 = Button(text1,text='點我點我',command=show) #添加插件用window_create text1.window_create(INSERT,window=b1) mainloop()
4.Text中的Indexes
Indexes(索引)是用來指向Text組件中文本的位置,跟python的序列索引一樣,Text的組件索引也是對應實際字符之間的位置。值得注意的是: 行號以1開始 列號以0開始
from tkinter import * root = Tk() text1=Text(root,width=30,height=3) text1.insert(INSERT,'index的練習') #1.2到1.5的范圍之間 print(text1.get(1.2,1.5))
5.Text中的Marks
Marks(標記)通常是嵌入到Text組件文本中的不可見的對象。事實上,Marks是指定字符間的位置,並跟隨相應的字符一起移動。Marks有INSERT,CURRENT,和user-defined marks(用戶自定義的Marks),
其中,INSERT和CURRENT是Tkinter預定義的特殊Marks,它們是不可能被刪除的
INSERT(或insert)用於指定當前插入光標的位置,Tkinter會在該位置繪制一個閃爍的光標(因此並不是所有的Marks都不可見)
CURRENT用於指定與鼠標坐標坐標最近最接近的位置,不過,如果你按緊鼠標任何一個按鈕,它會直到你松開它才響應
使用mark_set()方法創建和移動Marks
使用mark_unset()方法刪除Marks
Mark事實上就是索引,用於表示位置
from tkinter import * root = Tk() text1 =Text(root,width=30,height=4) text1.insert(INSERT,'I Love FishC.com') text1.mark_set('here',1.2) #插入是指在前面插入 text1.insert('here','插') text1.pack() mainloop()
6.Text中的Tags
Tags通常用於改變Text組件中內容的樣式和功能,你可以修改文本的字體,尺寸和顏色,另外Tags還允許你將文本、嵌入的組件和圖片與鍵盤相關聯,除了user-defined tags(用戶自定義的Tags),還有
一個預定義的特殊Tag:SEL
from tkinter import *
root = Tk()
text1 = Text(root,width=30,height=5) text1.pack() text1.insert(INSERT,'I Love FishC.com!') #第一個參數為自定義標簽的名字 #第二個參數為設置的起始位置,第三個參數為結束位置 #第四個參數為另一個位置 text1.tag_add('tag1','1.7','1.12','1.14') #用tag_config函數來設置標簽的屬性 text1.tag_config('tag1',background='yellow',foreground='red') #新的tag會覆蓋舊的tag mainloop()
7.Tags的事件綁定
Tags還支持事件的綁定,綁定事件使用的是tag_bind()方法,下面代碼實現了將文本與鼠標事件進行綁定,當鼠標進入該文本時,鼠標樣式切換為‘arrow’形態,離開文本時切換回‘xterm’形態,當觸發鼠標‘左鍵單擊操作’事件的時候,使用默認瀏覽器打開百度。
from tkinter import * import webbrowser root = Tk() text = Text(root,width=30,height=5) text.pack() text.insert(INSERT,"I Love FishC.com!") text.tag_add('link','1.7','1.16') text.tag_config('link',foreground='blue',underline=True) def show_arrow_cursor(event): text.config(cursor='arrow') def show_xterm_cursor(event): text.config(cursor='xterm') def click(event): webbrowser.open('http://baidu.com') text.tag_bind('link','<Enter>',show_arrow_cursor) text.tag_bind('link','<Leave>',show_xterm_cursor) text.tag_bind('link','<Button-1>',click) mainloop()
8.使用Text組件中文本的MD5摘要來判斷內容是否發生改變
from tkinter import * import hashlib root = Tk() text1 = Text(root,width=30,height=5) text1.pack() text1.insert(INSERT,'I Love FishC.com!') contents = text1.get('1.0',END) def getSig(contents): m=hashlib.md5(contents.encode()) return m.digest() sig=getSig(contents) def check(): contents = text1.get('1.0',END) if sig !=getSig(contents): print('警報,內容發生改變') else: print('風平浪靜') Button(root,text="檢查",command=check).pack() mainloop()
關於Text的組件的相關內容就先介紹到這里,之后會再進行補充。
