mercredi 29 avril 2015

NoneType Buffer interface error in windows 8

I am making a text editor, so you can imagine that there is a lot of manipulating text files. When I double click on the .py file to run it in python.exe in windows, it throws an error of 'NoneType does not support the Buffer interface'

Here is the code:

from tkinter import *
from tkinter.filedialog import *
from tkinter.messagebox import *
import os

os.chdir(os.getenv('HOME'))

current=None
backup=''

def newfile():

    def create_file(entry):
        global current
        global root
        nonlocal create_in
        nonlocal name_it
        current = open(entry.get(),'w')
        root.title(create_in+'/'+current.name+' - Aspus')
        name_it.destroy()

    create_in=askdirectory()
    if create_in!='':
        global root
        os.chdir(create_in)
        name_it=Tk()
        name_it.title("Name the File?")
        prompt=Label(name_it, text="Enter name for new file:")
        prompt.grid(row=0)
        entry=Entry(name_it)
        entry.grid(row=1)
        entry.insert(0, "Untitled.txt")
        create=Button(name_it, text="Create", command = lambda: create_file(entry))
        create.grid(row=1, column=3)
        name_it.mainloop()

def openfile(master):
    global current
    global backup
    opening=askopenfilename()
    file=open(opening, 'r')
    insert=file.read()
    backup=file.read()
    file.close()
    file=open(opening, 'w')
    current=file
    master.title(current.name+' - Aspus')
    return insert

def savefile(entry):
    global current
    if current!=None:
        current.write(entry.get('1.0', END))

    elif current==None:
        newfile()
        current.write(entry.get('1.0', END))

def ask_save():
    global root
    global current
    global main
    if current!=None:
        save_exit=askyesnocancel("Save Before Exit?", "Do you want to save before exiting?")
        if save_exit==True:
            a=current.name
            current.close()
            current=open(a, 'w')
            savefile(main)
            current.close()
            root.destroy()
            exit()
        elif save_exit==False:
            a=current.name
            current.close()
            current=open(a, 'w')
            current.write(backup)
            current.close()
            root.destroy()
            exit()
    elif current==None:
        if main.get('0.1', END).strip()!='':
            save_exit=askyesnocancel("Save Before Exit?", "Do you want to save before exiting?")
            if save_exit==True:
                newfile()
                savefile()
                current.close()
                root.destroy()
            elif save_exit==False:
                root.destroy()
        else:
            root.destroy()

def setpgwidth():

    def adjust(entry):
        global main
        new_width=entry.get()
        try:
            main.config(width=int(entry.get()))
        except:
            showerror("Invalid width", "You entered an invalid width. Expected an integer.")
            entry.delete(0, END)
        else:
            main.pack(expand=Y, fill=Y, side=LEFT)
            entry.master.destroy()

    width=Tk()
    width.title("Set Page Width")
    prompt=Label(width, text="Enter new page width:")
    prompt.grid(row=0, column=0, columnspan=2)
    new=Entry(width)
    new.grid(row=1, column=0)
    submit=Button(width, text="Submit", command=lambda: adjust(new))
    submit.grid(row=1, column=1)
    width.mainloop()

root=Tk()
root.title("Aspus Text Editor")

#create main text widget
main=Text(root, wrap=WORD)
main.pack(expand=True, fill=BOTH, side=LEFT)

#create scrollbar
scroll=Scrollbar(root)
scroll.pack(side=RIGHT, fill=Y)

#configure scrollbar
scroll.config(command=main.yview)
main.config(yscrollcommand=scroll.set)

#Creating menus
menu=Menu(root)
root.config(menu=menu)
menu.add_command(label="New File", command=newfile)
menu.add_command(label="Open File", command=lambda: main.insert(END, openfile(root)))
menu.add_command(label="Save File", command=lambda: savefile(main))
formatmenu=Menu(menu)
menu.add_cascade(label="Format", menu=formatmenu)
formatmenu.add_command(label="Set Page Width", command=setpgwidth)
menu.add_command(label="Quit", command=ask_save)
root.protocol("WM_DELETE_WINDOW", ask_save)

root.mainloop()

Does anyone know why this is happening and how to avoid it?

Aucun commentaire:

Enregistrer un commentaire