<bdo id='RmUE0'></bdo><ul id='RmUE0'></ul>

<small id='RmUE0'></small><noframes id='RmUE0'>

<tfoot id='RmUE0'></tfoot>
    1. <legend id='RmUE0'><style id='RmUE0'><dir id='RmUE0'><q id='RmUE0'></q></dir></style></legend>
        <i id='RmUE0'><tr id='RmUE0'><dt id='RmUE0'><q id='RmUE0'><span id='RmUE0'><b id='RmUE0'><form id='RmUE0'><ins id='RmUE0'></ins><ul id='RmUE0'></ul><sub id='RmUE0'></sub></form><legend id='RmUE0'></legend><bdo id='RmUE0'><pre id='RmUE0'><center id='RmUE0'></center></pre></bdo></b><th id='RmUE0'></th></span></q></dt></tr></i><div id='RmUE0'><tfoot id='RmUE0'></tfoot><dl id='RmUE0'><fieldset id='RmUE0'></fieldset></dl></div>

        如何使用 asksaveasfile 将文本从 Text-widget Tkinter 保存到 .doc?

        How to save text from a Text-widget Tkinter to a .doc using asksaveasfile?(如何使用 asksaveasfile 将文本从 Text-widget Tkinter 保存到 .doc?)
        <legend id='i1iyf'><style id='i1iyf'><dir id='i1iyf'><q id='i1iyf'></q></dir></style></legend>

          <i id='i1iyf'><tr id='i1iyf'><dt id='i1iyf'><q id='i1iyf'><span id='i1iyf'><b id='i1iyf'><form id='i1iyf'><ins id='i1iyf'></ins><ul id='i1iyf'></ul><sub id='i1iyf'></sub></form><legend id='i1iyf'></legend><bdo id='i1iyf'><pre id='i1iyf'><center id='i1iyf'></center></pre></bdo></b><th id='i1iyf'></th></span></q></dt></tr></i><div id='i1iyf'><tfoot id='i1iyf'></tfoot><dl id='i1iyf'><fieldset id='i1iyf'></fieldset></dl></div>
          • <bdo id='i1iyf'></bdo><ul id='i1iyf'></ul>
            <tfoot id='i1iyf'></tfoot>

                <tbody id='i1iyf'></tbody>

              <small id='i1iyf'></small><noframes id='i1iyf'>

                • 本文介绍了如何使用 asksaveasfile 将文本从 Text-widget Tkinter 保存到 .doc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想使用 asksaveasfilename 对话框将我在 Text 小部件中输入的一些内容保存为 .txt.doc 格式.在此之后,我希望它分别打开.记事本或 MS Word.

                  I want to save some stuff I entered in a Text widget to a .txt or .doc format, using an asksaveasfilename dialog box. After this I want it to open in resp. Notepad or MS Word.

                  from tkFileDialog import asksaveasfilename
                  import os.path
                  
                  name = asksaveasfilename(
                             initialdir="dir",
                             title="Save as",
                             filetypes=[("Text files",".txt"),("Word files",".doc")])
                  
                  data = open(name,"w")
                  data.write("text from text widget")
                  
                  os.startfile(name)
                  

                  它会创建文件,但不会在 MS Word 或记事本中打开它.相反,它询问我想如何打开这个文件.如果我选择程序,它将正确打开,但我希望它直接打开.(不选择打开文件的程序).当我直接在文件名:"框中提供扩展名时,它会按照我想要的方式工作.

                  It creates the file but it won't open it in MS Word or Notepad. Instead it asks how I want to open this file. If I choose the programm it will open correctly, but I want it to open directly. (without choosing a program to open the file with). When I give the extension directly in the "File name:" box it works the way I want though.

                  这有效:文件名:something.doc保存类型:Word 文件 (*.doc)---> 创建 something.doc 并在 MS Word 中打开它.

                  This works: File name: something.doc Save as type: Word file (*.doc) ---> creates something.doc and opens it in MS Word.

                  但这并不文件名称:某事保存类型:Word 文件 (*.doc)---> 创建一些东西(没有扩展名)并询问我希望它在什么程序中打开.

                  But this doesn't File name: something Save as type: Word file (*.doc) ---> creates something (no extension) and ask in what program I want it to open.

                  我使用 Python 2.7.8、Windows 8、Office 2010.

                  I use Python 2.7.8, Windows 8, Office 2010.

                  推荐答案

                  加上print name就可以看到问题,例如

                  You can see the problem if you add print name, e.g.

                  C:/Users/jsharpe/Downloads/testing
                  

                  请注意,没有添加任何扩展 - 我只输入了 "testing".filetypes 参数对于限制用户选择现有文件更有用,如果用户不提供扩展名,它将不会添加适当的扩展名.

                  note that no extension has been added - I only entered "testing". The filetypes argument is more useful for limiting the user's selection of existing files, it won't add the appropriate extension if the user doesn't supply one.

                  您可以为用户不输入扩展名的情况设置 defaultextension,但这不会反映在下拉列表中选择的类型(例如,如果您设置了 defaultextension=".txt" 即使用户从 filetypes 中选择该选项,它也不会是 .doc).

                  You can set a defaultextension for the cases where the user doesn't enter an extension, but this won't reflect the type selected in the drop-down (e.g. if you set defaultextension=".txt" it won't be .doc even if the user chooses that option from filetypes).

                  name = asksaveasfilename(defaultextension=".txt",
                                           filetypes=[("Text files",".txt"),
                                                      ("Word files",".doc")],
                                           initialdir="dir",
                                           title="Save as")
                  

                  (请注意,随着您添加越来越多的选项,按字母顺序排列的参数顺序会让生活变得更轻松)

                  附带说明,您(仍然!)不要 close 文件,这可能会导致问题 - 我建议对文件使用 with 上下文管理器处理:

                  On a side note, you (still!) don't close the file, which could cause issues - I'd suggest using the with context manager for file handling:

                  with open(name, "w") as data:
                      data.write("text from text widget")
                  

                  这篇关于如何使用 asksaveasfile 将文本从 Text-widget Tkinter 保存到 .doc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Kivy 1.9.0 Windows package KeyError: #39;rthooks#39;(Kivy 1.9.0 Windows 包 KeyError: rthooks)
                  Python Kivy: how to call a function on button click?(Python Kivy:如何在按钮单击时调用函数?)
                  How to disable a widget in Kivy?(如何禁用 Kivy 中的小部件?)
                  Centering an object in Kivy(在 Kivy 中将对象居中)
                  How to downgrade to Python 3.4 from 3.5(如何从 Python 3.5 降级到 Python 3.4)
                  Change button or label text color in kivy(在kivy中更改按钮或标签文本颜色)

                      <tfoot id='zJRjL'></tfoot>

                      <i id='zJRjL'><tr id='zJRjL'><dt id='zJRjL'><q id='zJRjL'><span id='zJRjL'><b id='zJRjL'><form id='zJRjL'><ins id='zJRjL'></ins><ul id='zJRjL'></ul><sub id='zJRjL'></sub></form><legend id='zJRjL'></legend><bdo id='zJRjL'><pre id='zJRjL'><center id='zJRjL'></center></pre></bdo></b><th id='zJRjL'></th></span></q></dt></tr></i><div id='zJRjL'><tfoot id='zJRjL'></tfoot><dl id='zJRjL'><fieldset id='zJRjL'></fieldset></dl></div>
                    1. <small id='zJRjL'></small><noframes id='zJRjL'>

                      1. <legend id='zJRjL'><style id='zJRjL'><dir id='zJRjL'><q id='zJRjL'></q></dir></style></legend>
                            <tbody id='zJRjL'></tbody>
                            <bdo id='zJRjL'></bdo><ul id='zJRjL'></ul>