重启脚本

时间:2016-10-26 18:54:23

标签: python-2.7 matplotlib tkinter runtime-error tkinter-canvas

我有一个简单的Tkinter,Matplotlib python程序。我正在使用Windows 10,程序工作正常,直到我关闭窗口。重新启动脚本后,我收到运行时错误(见图)。我试图“联系应用程序的支持团队获取更多信息”然后我意识到我是应用程序支持团队....

enter image description here

    #!/usr/bin/env python
import matplotlib
import time
matplotlib.use('TkAgg')

from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from tkFileDialog import *

import sys
import Tkinter as Tk


def destroy(e):
    sys.exit()

class Window():
    def __init__(self):

        self.root = Tk.Tk()
        self.root.wm_title("Embedding in TK")
        self.QuitButton = None
        self.UploadButton = None
        self.filepath = None
        self.fig = None
        self.canvas =   None
        self.image = None




    def showWindow(self):
        self.root.mainloop()

    def addQuitButton(self, ButtonText):
        if self.QuitButton is None:
            self.QuitButton = Tk.Button(master = self.root, text = ButtonText, command = self.closeWindow)
            self.QuitButton.pack(side=Tk.BOTTOM)

    def addUploadButton(self, ButtonText):
        if self.UploadButton is None:
            self.UploadButton = Tk.Button(master = self.root, text = ButtonText, command = self.showFileDialog)
            self.UploadButton.pack(side=Tk.BOTTOM)

    def showFileDialog(self):
        print 'oh yeah'
        if self.filepath is None:
            print 'Oh no...'
            self.filepath = askopenfilename(parent=self.root)
            print self.filepath
        self.image = mpimg.imread(self.filepath)
        self.buildFigure()
        self.showCanvas()

    def buildFigure(self):
        self.fig = plt.figure()
        self.im = plt.imshow(self.image) 


    def showCanvas(self):
        self.canvas = FigureCanvasTkAgg(self.fig, master=self.root)
        self.canvas.show()
        self.canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)



    def closeWindow(self):
        #sys.exit()
        self.root.quit()
        self.root.destroy()





if __name__== "__main__":
    base = Window()
    base.addQuitButton("Quit")
    base.addUploadButton("Upload")
    base.showWindow()

1 个答案:

答案 0 :(得分:0)

我发现闲置是问题,但我不知道为什么。当我在命令行中运行我的脚本时没有问题。