代码导致shell重新启动

时间:2018-01-09 09:47:56

标签: python shell matplotlib

一旦我开始运行它,它会导致我的shell重新启动。我不知道最近会怎么样。我在安装了python 3.6.4的mac上运行它。 Matplotlib也已安装。

有人看到问题可能是什么吗?

#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
import tkinter as tk
import matplotlib.pyplot as plt
import csv

#Variablen Initialisierung 
xVals = []
yVals = []

#Diagramm anzeigen 
def makePlot():
    plt.plot(xVals,yVals)
    plt.scatter(xVals,yVals)
    plt.ylabel(yLabelText.get())
    plt.xlabel(xLabelText.get())  
    plt.title(titleText.get())
    plt.show()

#xls.Daten einlesen
def loadFile():
    global xVals
    global yVals
    xVals = [] #reset Data
    yVals = [] #reset Data
    file_path = tk.filedialog.askopenfilename()
    with open(file_path,'r') as csvfile:
        dataReader = csv.reader(csvfile, delimiter=',') 
        for row in dataReader:
            if len(row) < 2: #Auf validen Input pruefen
                print("Invalid Input")
                exit(-1)
            xVals.append(row[0])
            yVals.append(row[1])

root = tk.Tk(className="Diagramm-Creator Anna, Deny, Sina") #Überschrift

#Label Initalisierung             
tk.Label(root, text="Diagramm").grid(row=0)
tk.Label(root, text="Titel").grid(row=1)
tk.Label(root, text="X-Achse").grid(row=2)
tk.Label(root, text="Y-Achse").grid(row=3)

#Input Initalisierung
yLabelText = tk.StringVar()
xLabelText = tk.StringVar()
titleText = tk.StringVar()
e1 = tk.Entry(root,textvariable = xLabelText)
e2 = tk.Entry(root,textvariable = yLabelText)
e3 = tk.Entry(root,textvariable = titleText)
e3.grid(row=1, column=1)
e1.grid(row=2, column=1)
e2.grid(row=3, column=1)

#Buttons Initalisierung
btnOpen = tk.Button(root, text="CSV-Datei öffnen",command=loadFile)
btnPlot = tk.Button(root, text="Diagramm erstellen",command=makePlot)
btnOpen.grid(row=0,column=2)
btnPlot.grid(row=4,column=2)

#Start
root.mainloop( )

1 个答案:

答案 0 :(得分:0)

我唯一的线索就是这段代码:

    with open(file_path,'r') as csvfile:
        dataReader = csv.reader(csvfile, delimiter=',') 
        for row in dataReader:
            if len(row) < 2: #Auf validen Input pruefen
                print("Invalid Input")
                exit(-1)

会让你突然退出执行,也会退出Python shell ......

也许您想要替换打印行和退出行,但有例外:

    with open(file_path,'r') as csvfile:
        dataReader = csv.reader(csvfile, delimiter=',') 
        for row in dataReader:
            if len(row) < 2: #Auf validen Input pruefen
                raise ValueError('Invalid Input')