[英]How to bold selected text in tkinter?
I had made a text editor in python where I am facing some problems.我在 python 中制作了一个文本编辑器,但遇到了一些问题。 It has a bold button which enbolds whole text of the text editor, where I want to on apply it to selected text, but I cannot define the functionality properly.它有一个粗体按钮,可以将文本编辑器的整个文本加粗,我想将其应用于所选文本,但我无法正确定义功能。
Please help me to select the text and bold .请帮我选择文本和粗体。
Here is my code:这是我的代码:
#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)
Change your function to:将您的功能更改为:
def change_bold():
textwidget.tag_configure("boldtext",font=textwidget.cget("font")+" bold")
textwidget.tag_add("boldtext","sel.first","sel.last")
To make the text normal again you should change the function to this:要使文本再次正常,您应该将函数更改为:
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.