Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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 2.7 Python文本编辑器(非常基本)_Python 2.7_Tkinter - Fatal编程技术网

Python 2.7 Python文本编辑器(非常基本)

Python 2.7 Python文本编辑器(非常基本),python-2.7,tkinter,Python 2.7,Tkinter,我是使用Tkinter的初学者 我正在尝试制作python文本编辑器 此编辑器具有多个选项 1.新文件 2.打开文件 3.保存 4.另存为 5.退出 但我的3.save选项不起作用 它显示出…错误 Exception in Tkinter callback Traceback (most recent call last): File "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in __call__ return s

我是使用Tkinter的初学者

我正在尝试制作python文本编辑器

此编辑器具有多个选项

1.新文件

2.打开文件

3.保存

4.另存为

5.退出

但我的3.save选项不起作用

它显示出…错误

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 's 

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

下面是我的代码,我使用的是python 2.7.10
错误非常明显:

TypeError: coercing to Unicode: need string or buffer, NoneType found
更具体地说,找到的“NoneType”表示open方法需要一个unicode字符串,但您将它交给了
None


看起来您遇到的代码路径没有设置全局
filename
变量(顺便说一句,使用全局变量是个糟糕的选择)。我不打算为您调试,但这会给您一个指向正确方向的指针。这只是代码中的一个错误,不是Tkinter或解释器的问题。

因为当您单击“保存”按钮(在单击“新建”按钮之前)时,
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 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)
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()
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)