file - NoneType Buffer interface error in windows 8 -


i making text editor, can imagine there lot of manipulating text files. when double click on .py file run in python.exe in windows, throws error of 'nonetype not support buffer interface' have ever heard "buffer" programming term in context of text files, believe problem somewhere in there.

here code:

from tkinter import * tkinter.filedialog import * 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 file?")         prompt=label(name_it, text="enter name 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 want 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 want 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 invalid width. expected 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 know why happening , how avoid it?

at least part of problem these 2 statements:

insert=file.read() backup=file.read() 

because read() reads whole file, backup isn't going think is.

the first thing step through code pdb, or add print statements validate data think is. you're relying heavily on global variables can changed in order different expect.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -