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

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

      <tfoot id='W7hbf'></tfoot>

    2. 如何使用 asksaveasfile 将文本从 Text-widget Tkinter 保

      时间:2023-10-08

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

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

                <tbody id='qXyk0'></tbody>

                本文介绍了如何使用 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:python:不同包下同名的两个模块和类 下一篇:如何显示 PyQt 模式对话框并在关闭后从其控件中

                相关文章

                最新文章

              1. <legend id='JJYtX'><style id='JJYtX'><dir id='JJYtX'><q id='JJYtX'></q></dir></style></legend>

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

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