【问题标题】:Python Runtime error on restart of script重新启动脚本时出现 Python 运行时错误
【发布时间】:2016-10-26 18:54:23
【问题描述】:

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

    #!/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()

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2019-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多