上節(jié)課我們介紹了Text組件的Indexs 索引和 Marks 標(biāo)記,它們主要是用于定位,Marks 可以看做是特殊的 Indexs,但是它們又不是完全相同的,比如在默認(rèn)情況下,你在Marks指定的位置中插入數(shù)據(jù),Marks 的位置會(huì)自動(dòng)發(fā)生改變, 因?yàn)镸arks 認(rèn)它后面的“那個(gè)家伙”,當(dāng) Marks 前面的數(shù)據(jù)被刪除時(shí),Marks 并不會(huì)被刪除,它的位置只是相應(yīng)的向前移動(dòng)了,只有?mark_unset() 方法 才能夠刪除Marks,這節(jié)課我們接著來(lái)介紹 Tags 的用法。
Tags(標(biāo)簽)通常用于改變 Text 組件中內(nèi)容的樣式和功能。你可以修改文本的字體、尺寸和顏色。另外,Tags 還允許你將文本、嵌入的組件和圖片與鍵盤(pán)和鼠標(biāo)等事件相關(guān)聯(lián)。除了 user-defined tags(用戶(hù)自定義的 Tags),還有一個(gè)預(yù)定義的特殊 Tag:SEL。
SEL(或 "sel")用于表示對(duì)應(yīng)的選中內(nèi)容(如果有的話(huà))。
你可以自定義任意數(shù)量的 Tags,Tags 的名字是由普通字符串組成,可以是除了空白字符外的任何字符。另外,任何文本內(nèi)容都支持多個(gè) Tags 描述,任何 Tag 也可以用于描述多個(gè)不同的文本內(nèi)容。
我們來(lái)舉個(gè)例子:
-
import tkinter as tk
-
root = tk.Tk()
-
text = tk.Text(root, width=40, height=5,)
-
text.pack()
-
text.insert("insert", 'I love Python.com')
-
text.tag_add("tag1", "1.7", "1.13", "1.15")
-
text.tag_config("tag1", background = "yellow", foreground = "red")
-
root.mainloop()
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-615249.html
還有一點(diǎn)需要注意的是:如果你對(duì)同一個(gè)范圍內(nèi)的文本加上多個(gè) Tags,并且設(shè)置相同的選項(xiàng),那么新創(chuàng)建的 Tag 樣式會(huì)覆蓋比較舊的 Tag:
-
import tkinter as tk
-
root = tk.Tk()
-
text = tk.Text(root, width=40, height=5)
-
text.pack()
-
text.insert("insert", 'I love Python.com')
-
text.tag_add("tag1", "1.7", "1.13", "1.15")
-
text.tag_add("tag2", "1.7", "1.13", "1.15")
-
text.tag_config("tag2", foreground = "green")
-
text.tag_config("tag1", background = "yellow", foreground = "red")
-
root.mainloop()
那么新創(chuàng)建的 Tag2 會(huì)覆蓋比較舊的 Tag1 的相同選項(xiàng),?注意,與下邊的調(diào)用順序沒(méi)有關(guān)系
你或許想控制 Tags 間的優(yōu)先級(jí),這可以實(shí)現(xiàn)嗎?完全沒(méi)有問(wèn)題!你可以使用 tag_raise() 和 tag_lower() 方法來(lái)提高和降低某個(gè) Tag 的優(yōu)先級(jí)。
-
import tkinter as tk
-
root = tk.Tk()
-
text = tk.Text(root, width=40, height=5)
-
text.pack()
-
text.tag_config("tag1", background="yellow", foreground="red")
-
text.tag_config("tag2", foreground="green")
-
text.tag_lower("tag2")
-
text.insert("insert", "I love Python!", ("tag2", "tag1"))
-
root.mainloop()
另外 Tags 還支持事件綁定,使用的是 tag_bind() 的方法。
下邊例子中我們將文本("Python.com")與鼠標(biāo)事件進(jìn)行綁定,當(dāng)鼠標(biāo)進(jìn)入該文本段的時(shí)候,鼠標(biāo)樣式切換為 "arrow" 形態(tài),離開(kāi)文本段的時(shí)候切換回 "xterm" 形態(tài)。當(dāng)觸發(fā)鼠標(biāo)“左鍵點(diǎn)擊操作”事件的時(shí)候,使用默認(rèn)瀏覽器打開(kāi)Python的首頁(yè)(Welcome to Python.org):
-
import tkinter as tk
-
import webbrowser
-
root = tk.Tk()
-
text = tk.Text(root, width=40, height=5)
-
text.pack()
-
text.insert("insert", "I love Python.com!")
-
text.tag_add("link", "1.7", "1.17")
-
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("https://www.python.org/")
-
text.tag_bind("link", "<Enter>", show_arrow_cursor)
-
text.tag_bind("link", "<Leave>", show_xterm_cursor)
-
text.tag_bind("link", "<Button-1>", click)
-
root.mainloop()
接下來(lái)給大家介紹幾個(gè) Tags 使用上的技巧:
(一)判斷內(nèi)容是否發(fā)生變化
通過(guò)校檢 Text 組件中文本的 MD5 摘要來(lái)判斷內(nèi)容是否發(fā)生改變
-
import tkinter as tk
-
import hashlib
-
root = tk.Tk()
-
text = tk.Text(root, width=40, height=5)
-
text.pack()
-
text.insert("insert", "I love Python.com!")
-
contents = text.get("1.0", "end")
-
def getSig(contents):
-
m = hashlib.md5(contents.encode())
-
return m.digest()
-
sig = getSig(contents)
-
def check():
-
contents = text.get("1.0", "end")
-
if sig != getSig(contents):
-
print("警報(bào):內(nèi)容發(fā)生改變!")
-
else:
-
print("風(fēng)平浪靜")
-
tk.Button(root, text = "檢查", command = check).pack()
-
root.mainloop()
(二)查找操作
使用 search() 方法可以搜索 Text 組件中的內(nèi)容。但是傳統(tǒng)的 search() 方法只查找到一個(gè),就返回,我們可以加入一個(gè)循環(huán),查找所有的。
-
import tkinter as tk
-
import hashlib
-
root = tk.Tk()
-
text = tk.Text(root, width=40, height=5)
-
text.pack()
-
text.insert("insert", "I love Python.com!")
-
def getIndex(text, index):
-
return tuple(map(int, str.split(text.index(index), ".")))
-
start = "1.0"
-
while True:
-
pos = text.search("o", start, stopindex = "end")
-
if not pos:
-
break
-
print("找到啦,位置是:", getIndex(text, pos))
-
start = pos + "+1c"
-
root.mainloop()
(三)恢復(fù)、撤銷(xiāo)操作
Text 組件還支持“恢復(fù)”和“撤銷(xiāo)”操作,這使得 Text 組件顯得相當(dāng)高大上。
通過(guò)設(shè)置 undo 選項(xiàng)為 True 可以開(kāi)啟 Text 組件的“撤銷(xiāo)”功能。然后用 edit_undo() 方法實(shí)現(xiàn)“撤銷(xiāo)”操作,用 edit_redo() 方法實(shí)現(xiàn)“恢復(fù)”操作。
-
import tkinter as tk
-
import hashlib
-
root = tk.Tk()
-
text = tk.Text(root, width=40, height=5, undo = True)
-
text.pack()
-
text.insert("insert", "I love Python.com!")
-
def show():
-
text.edit_undo()
-
tk.Button(root, text = "撤銷(xiāo)", command = show).pack()
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-615249.html -
root.mainloop()
?Text 組件內(nèi)部有一個(gè)棧專(zhuān)門(mén)用于記錄內(nèi)容的每次變動(dòng),所以每次“撤銷(xiāo)”操作就是一次彈棧操作,“恢復(fù)”就是再次壓棧。
默認(rèn)情況下,每一次完整的操作將會(huì)放入棧中。但怎么樣算是一次完整的操作呢?Tkinter 覺(jué)得每次焦點(diǎn)切換、用戶(hù)按下 Enter 鍵、刪除\插入操作的轉(zhuǎn)換等之前的操作算是一次完整的操作。也就是說(shuō)你連續(xù)輸入“I love Python” 的話(huà),一次的“撤銷(xiāo)”操作就會(huì)將所有的內(nèi)容刪除。
那我們能不能自定義呢?比如我希望插入一個(gè)字符就算一次完整的操作,然后每次點(diǎn)擊“撤銷(xiāo)”就去掉一個(gè)字符。
當(dāng)然可以!做法就是先將 autoseparators 選項(xiàng)設(shè)置為 False(因?yàn)檫@個(gè)選項(xiàng)是讓 Tkinter 在認(rèn)為一次完整的操作結(jié)束后自動(dòng)插入“分隔符”),然后綁定鍵盤(pán)事件,每次有輸入就用 edit_separator() 方法人為地插入一個(gè)“分隔符”:
-
import tkinter as tk
-
import hashlib
-
root = tk.Tk()
-
text = tk.Text(root, width=40, height=5, undo = True, autoseparators = False)
-
text.pack()
-
text.insert("insert", "I love Python.com!")
-
def callback(event):
-
text.edit_separator() #人為插入分隔符
-
text.bind('<Key>', callback)
-
def show():
-
text.edit_undo()
-
tk.Button(root, text = "撤銷(xiāo)", command = show).pack()
-
root.mainloop()
到了這里,關(guān)于《零基礎(chǔ)入門(mén)學(xué)習(xí)Python》第070講:GUI的終極選擇:Tkinter7的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!