![](/img/trans.png)
[英]How To Bold, Italic And Underline The Same Selected Text in Python Tkinter?
[英]How to bold selected text in tkinter?
我在 python 中制作了一個文本編輯器,但遇到了一些問題。 它有一個粗體按鈕,可以將文本編輯器的整個文本加粗,我想將其應用於所選文本,但我無法正確定義功能。
請幫我選擇文本和粗體。
這是我的代碼:
#bold
bold_icon = tk.PhotoImage(file='C:\\Users\\soham\\OneDrive\\Desktop\\TEXT_EDITOR\\ICONS\\icons2\\bold.png')
bold_btn = ttk.Button(tool_bar, image= bold_icon)
bold_btn.grid(row=0, column=2, padx=5)
def change_bold():
text_property=tk.font.Font(font=text_editor['font'])
if text_property.actual()['weight'] =='normal':
text_editor.configure(font=(current_font_family, current_font_size, 'bold'))
if text_property.actual()['weight'] == 'bold':
text_editor.configure(font=(current_font_family, current_font_size, 'normal'))
bold_btn.configure(command=change_bold)
將您的功能更改為:
def change_bold():
textwidget.tag_configure("boldtext",font=textwidget.cget("font")+" bold")
textwidget.tag_add("boldtext","sel.first","sel.last")
要使文本再次正常,您應該將函數更改為:
textwidget.tag_configure("boldtext",font=textwidget.cget("font")+" bold")
def change_bold():
if "boldtext" in textwidget.tag_names("sel.first"):
textwidget.tag_remove("boldtext","sel.first","sel.last")
else:
textwidget.tag_add("boldtext","sel.first","sel.last")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.