python加粗_python – 设置为使用标记加粗选定的文本
我一直在努力制作一個簡單的文本編輯器,并一直在試驗標簽.我已經能夠使用標簽創建證明.現在我添加一個粗體選項.
我的問題是我找不到很多使用“sel”標簽的例子,這是當前選擇中使用的標簽.
每當我使用SEL標簽時,只要文本突出顯示,文本就是粗體,當它變為未突出顯示時,它會恢復為舊的瘦字體.
這只是我代碼的一小部分:
def Bold(self, body, Just, Line, selected font):
bold font = tkFont.Font(family=selectedfont, weight="bold")
selected font = boldfont
body.tag_config("sel",font=selectedfont)
body.tag_add("sel", 1.0,END)
按下Bold按鈕時,將調用上一個功能.
現在,我將body.tag_add(“sel”,1.0,END)設置為從1.0到END,因為我不知道如何獲取所選域.
我已經嘗試了<< Selection>>,但經過長時間的實驗,它沒有幫助我.
解決方法:
您只需要在函數內部使用tag_add():
import Tkinter as tk
def make_bold():
aText.tag_add("bt", "sel.first", "sel.last")
lord = tk.Tk()
aText = tk.Text(lord, font=("Georgia", "12"))
aText.grid()
aButton = tk.Button(lord, text="bold", command=make_bold)
aButton.grid()
aText.tag_config("bt", font=("Georgia", "12", "bold"))
lord.mainloop()
我剛剛遇到了一個相當教育的example,而不是Bryan Oakley,
在一個完全不相關的搜索!
以下是更具動態替代方案的快速示例:
import Tkinter as tk
import tkFont
def make_bold():
current_tags = aText.tag_names("sel.first")
if "bt" in current_tags:
aText.tag_remove("bt", "sel.first", "sel.last")
else:
aText.tag_add("bt", "sel.first", "sel.last")
lord = tk.Tk()
aText = tk.Text(lord, font=("Georgia", "12"))
aText.grid()
aButton = tk.Button(lord, text="bold", command=make_bold)
aButton.grid()
bold_font = tkFont.Font(aText, aText.cget("font"))
bold_font.configure(weight="bold")
aText.tag_configure("bt", font=bold_font)
lord.mainloop()
標簽:python,text,tkinter
來源: https://codeday.me/bug/20190613/1233762.html
總結
以上是生活随笔為你收集整理的python加粗_python – 设置为使用标记加粗选定的文本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BIOS中的分区工具
- 下一篇: python递归面试题_python面试