Python 将文本从tkinter文本框保存到.doc文件时,如何保持格式?
我一直在试图找到一种方法来保存我一直在使用的文本编辑器应用程序中Tkinter文本框中的格式 以下是我的粗体函数代码:Python 将文本从tkinter文本框保存到.doc文件时,如何保持格式?,python,file,tkinter,textbox,formatting,Python,File,Tkinter,Textbox,Formatting,我一直在试图找到一种方法来保存我一直在使用的文本编辑器应用程序中Tkinter文本框中的格式 以下是我的粗体函数代码: def bold(): boldFont = font.Font(textBox, textBox.cget("font")) boldFont.configure(weight = "bold") textBox.tag_configure("bold", font = boldFont)
def bold():
boldFont = font.Font(textBox, textBox.cget("font"))
boldFont.configure(weight = "bold")
textBox.tag_configure("bold", font = boldFont)
currentTags = textBox.tag_names("sel.first")
if ("bold" in currentTags):
textBox.tag_remove("bold", "sel.first", "sel.last")
else:
textBox.tag_add("bold", "sel.first", "sel.last")
我使用以下代码调用此函数:
boldButton = Button(toolbarFrame, text = "B", command = bold, font = ('Times New Roman', 11, 'bold'))
当我选择文本并按下按钮时,按钮在文本框中起作用,但当我保存文件时,格式会被删除。我正在将其保存到.doc文件中
我使用以下功能将其保存为.doc文件:
def saveNote():
name = str(nameBox.dump('1.0', END))
f = open(name + ".doc", "w")
text = str(textBox.dump('1.0', END))
f = open(name + ".doc", "a")
f.write(text)
f.close()
我尝试同时使用get()
函数和dump()
函数,但两者都不起作用
保存文件时如何保持格式?您必须将数据转换为其他格式。您必须将数据转换为其他格式。