Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将文本从tkinter文本框保存到.doc文件时,如何保持格式?_Python_File_Tkinter_Textbox_Formatting - Fatal编程技术网

Python 将文本从tkinter文本框保存到.doc文件时,如何保持格式?

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)

我一直在试图找到一种方法来保存我一直在使用的文本编辑器应用程序中Tkinter文本框中的格式

以下是我的粗体函数代码:

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()
函数,但两者都不起作用


保存文件时如何保持格式?

您必须将数据转换为其他格式。您必须将数据转换为其他格式。