Python text editor (very simple)

I am starting to use Tkinter.

I am trying to make a python text editor

There are options in this editor.

1.new file

2.open file

3.save

4.save how

5.exit

But my 3.save option does not work.

it shows ... Error

Exception in Tkinter callback

Traceback (most recent call last):

File "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in __call__

return self.func(*args)

File "C:\Users\Shalom Alexander\Desktop\files\Sharon  

database\sdf_lab_by_sharon\Assignment on c and python\python assinment 

ans\mytxt2.py", line 44, in savefile

f=open(filename,'w')

TypeError: coercing to Unicode: need string or buffer, NoneType found

Below is my code and I am using python 2.7.10

from Tkinter import *
from tkFileDialog import *

filename=None

class Window(Frame):
    def __init__(self,master=None):
        Frame.__init__(self,master)

        self.master=master


        self.init_window()


    def init_window(self):

        self.master.title('Death note 2')

        self.pack(fill=BOTH,expand=1)

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

        File=Menu(menubar)
        File.add_command(label='New',command=self.newfile)
        File.add_command(label='Open',command=self.openfile)
        File.add_command(label='Save',command=self.savefile)
        File.add_command(label='Save as',command=self.saveasfile)
        File.add_separator()
        File.add_command(label='Exit',command=self.my_exit)
        menubar.add_cascade(label='File',menu=File)

    def newfile(self):

        global filename
        filename="Untitled.txt"
        text.delete(0.0,END)

    def savefile(self):
        global filename
        f=open(filename,'w')
        t=text.get(0.0,END)
        f.write(t)
        f.close()

    def saveasfile(self):

        f=asksaveasfile(mode='w')
        t=text.get(0.0,END)

        try:
            f.write(t.rstrip())
        except:
            showerror(title='Oops',message='Sorry can\'t save the file')
    def openfile(self):

        f=askopenfile(mode='r')
        t=f.read()
        text.delete(0.0,END)
        text.insert(0.0,t)

    def my_exit(self):
        exit()

root=Tk()
text=Text(root,width=500,height=500)
text.pack()
root.geometry('500x500')
My_txt_edt=Window(root)
root.mainloop()
+4
source share
2 answers

The error is pretty obvious:

TypeError: coercing to Unicode: need string or buffer, NoneType found

More specifically, the found "NoneType" indicates that the open method expected a unicode string, but you passed it None.

, , filename ( , ) . , . , Tkinter .

+2

filename=None, (, ).
:

>>> open(None, 'w')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: coercing to Unicode: need string or buffer, NoneType found
>>> 

:

def savefile(self):
    global filename

    if filename:
        f=open(filename,'w')
        t=text.get(0.0,END)
        f.write(t)
        f.close()
    else:
        f=asksaveasfile(mode='w',defaultextension=".txt")
        if f:
            text2save=text.get(0.0,END)
            f.write(text2save)
            f.close()

filename , :

def write(self, filename, text):
    f=open(filename,'w')
    f.write(text)
    f.close()

def savefile(self):
    global filename

    txt = text.get(0.0,END)
    if filename:
        self.write(filename, txt)
    else:
        ask=asksaveasfilename()
        if ask:
            filename = ask
            self.write(filename, txt)
+1

Source: https://habr.com/ru/post/1622691/


All Articles