Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.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 为savecommand添加.txt、.png和其他选项_Python_Python 3.x_Tkinter_Save - Fatal编程技术网

Python 为savecommand添加.txt、.png和其他选项

Python 为savecommand添加.txt、.png和其他选项,python,python-3.x,tkinter,save,Python,Python 3.x,Tkinter,Save,因此,我在Python3中启动了一个小型文本编辑器项目,并成功地添加了一个带有保存文档功能的保存按钮,但我想为save命令添加在.txt、.png和其他选项之间进行选择的选项。我怎么能这么做 from tkinter import * # Imports everything from tkinter import tkinter from tkinter import filedialog class Window(Frame): # Frame is a class in tk

因此,我在Python3中启动了一个小型文本编辑器项目,并成功地添加了一个带有保存文档功能的保存按钮,但我想为save命令添加在.txt、.png和其他选项之间进行选择的选项。我怎么能这么做

from tkinter import *   # Imports everything from tkinter
import tkinter
from tkinter import filedialog


class Window(Frame):    # Frame is a class in tkineter and we are creating a 
frame or a window
    def __init__(self, master=None):    
        Frame.__init__(self, master)

        self.master = master    #Mainframe or main window

        self.init_window()

    def init_window(self):  # 

        self.master.title("GUI")    #windows title is GUI

        self.pack(fill=BOTH, expand=1)  # Adjust window as we want, but also 
fill up the window as a defult


        menu =Menu(self.master)
        self.master.config(menu=menu)

        file = Menu(menu)
        file.add_command(label='Open File')
        file.add_command(label='New File')
        file.add_command(label='Save as')
        file.add_command(label='Save', command=self.file_save)
        file.add_command(label='Exit', command=self.client_exit)
        menu.add_cascade(label='File', menu=file)

        edit = Menu(menu)
        edit.add_command(label='Undo')
        edit.add_command(label='Redo')
        menu.add_cascade(label='Edit', menu=edit)


    def file_save(self):
        f = filedialog.asksaveasfile(mode='w', defaultextension=".txt")
        if f is None:
            return
        text2save = str(text.get(1.0, END))
        f.write(text2save)
        f.close()

    def client_exit(self):
        exit()


root = Tk()     # 
root.geometry("400x300")    #Specify the dimention of the window as 400 by 
300
app = Window(root)
root.mainloop()     # Generates our window for us
如果查看,可以看到有一个
filetypes
参数。只需用您想要的文件类型列表来填充它

filetypes_choices = [('Text file', '*.txt'), ('PNG Image File', '*.png')]
f = filedialog.asksaveasfile(mode='w', defaultextension=".txt", filetypes=filetypes_choices)
如果查看,可以看到有一个
filetypes
参数。只需用您想要的文件类型列表来填充它

filetypes_choices = [('Text file', '*.txt'), ('PNG Image File', '*.png')]
f = filedialog.asksaveasfile(mode='w', defaultextension=".txt", filetypes=filetypes_choices)

这就是我要找的!非常感谢。但我也很好奇,当我点击save时,命令提示符在
text2save=str(text.get(1.0,END))
中返回“name'text'未定义”,但文件仍然保存为文本文档@恐怕我帮不了你解决那个问题。您得到一个错误,但代码似乎产生了一个结果,这看起来确实很奇怪。你确定该文件不存在吗?除非我保存了我要找的文件,否则该文件不存在!非常感谢。但我也很好奇,当我点击save时,命令提示符在
text2save=str(text.get(1.0,END))
中返回“name'text'未定义”,但文件仍然保存为文本文档@恐怕我帮不了你解决那个问题。您得到一个错误,但代码似乎产生了一个结果,这看起来确实很奇怪。你确定该文件不存在吗?除非我保存该文件,否则该文件不存在