[英]margins in tkinter text widget acquire background color
在我的程序中的文本小部件中, lmargin1/lmargin2 用於根據其輪廓級別縮進文本(這些是與 tag_config 一起使用的選項,例如lmargin1(距離)用於文本塊中第一行的左邊距有這個標簽。默認為 0(沒有左邊距) )
我的問題是我定義了一個高亮文本標簽,它改變了背景。 因此,如果縮進文本使用此標記,則邊距空間中的背景也會發生變化,例如:
#!/usr/bin/env python
from Tkinter import *
root = Tk()
configtext = Text(root, width=150)
configtext.pack()
configtext.tag_configure('n', lmargin1=45, lmargin2=45)
configtext.tag_configure('nh', lmargin1=45, lmargin2=45, background="yellow", foreground="red")
con1="L'Italia è una Repubblica democratica, fondata sul lavoro. La sovranità appartiene al popolo"
con2=" che la esercita nelle forme e nei limiti della Costituzione. La Repubblica riconosce e garantisce i diritti inviolabili dell'uomo"
con3=" sia come singolo sia nelle formazioni sociali ove si svolge la sua personalità, e richiede l'adempimento dei doveri inderogabili di solidarietà politica, economica e sociale.\n\n"
configtext.insert(INSERT,con1+con2+con3)
configtext.insert(INSERT,con1,'n')
configtext.insert(INSERT,con2,'nh')
configtext.insert(INSERT,con3,'n')
mainloop()
有沒有辦法避免背景顏色污染頁邊距?
這是一個很老的問題,但考慮到我今天偶然發現它尋找答案,其他有同樣問題的人可能會喜歡這個更新。
我解決這個問題的方法是將標簽配置中的lmargincolor
選項設置為Text
小部件的背景顏色。 這將以該顏色繪制由lmargin1
/ lmargin2
添加的邊距(請參閱官方手冊)。
就我而言,這看起來像這樣:
text_widget.tag_configure(
tagName="warning",
background="#ff9800",
lmargin1=margin1
lmargin2=margin2,
lmargincolor=text_widget.cget("background")
)
或者,您當然可以使用靜態顏色,如#FFF
。
這對我來說就像一個魅力,導致了這種外觀(琥珀色背景文本是有問題的標簽)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.