macOS Sierra:Python分段错误11

2024-04-26 06:17:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个Python文本编辑器。尝试运行此代码时:

from Tkinter import *
from tkFileDialog import *

filename = None

def newFile():
    global filename
    filename = "Untitled"
    text.delete(0.0, END)

def saveFile():
    global filename
    t = text.get(0.0, end)
    f = open(filename, 'w')
    f.write(t)
    f.close()

def saveAs():
    f = asksaveasfile(mode='w', defaultextension='.txt')
    t = text.get(0.0, END)
    try:
        f.write(t.rstrip())
    except:
        showerror(title="Oops!", message="Unable to save file...")

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

root = Tk()
root.title("TextEditor")
root.minsize(width=400, height=400)
root.maxsize(width=400, height=400)

text = Text(root, width=400, height=400)
text.pack()

menubar = Menu(root)
filemenu = Menu(menubar)
filemenu.add_command(label="New", command=newFile)
filemenu.add_command(label="Open", command=openFile)
filemenu.add_command(label="Save", command=saveFile)
filemenu.add_command(label="Save As", command=saveAs)
filemenu.add_separator()
filemenu.add_command(label="Quit", command=root.quit)
menubar.add_cascade(label="File", menu=menubar)

root.config(menu=menubar)
root.mainloop()

Python退出了,我得到了这个错误

Segmentation fault: 11

我使用的是Python2.7.10,它对Python3.5.2也做了同样的事情

我不认为我的代码是问题,但我还是把它放进去了,因为这是一种可能性。在


Tags: 代码textfromimportadddefrootfilename