Python Tkinter程序在关闭消息框后自动关闭,无错误

Python Tkinter程序在关闭消息框后自动关闭,无错误,python,matplotlib,tkinter,cx-freeze,tkmessagebox,Python,Matplotlib,Tkinter,Cx Freeze,Tkmessagebox,我创建了一个tkinter GUI,允许用户选择csv文件并使用matplotlib生成图形。该程序在IDE中运行良好,但在冻结的可执行文件(cx\U冻结)中创建一个图形后关闭。exe运行时没有错误-它在默认照片编辑器中调出图形,然后显示带有图表文件路径的弹出窗口。当弹出窗口(tx.messagebox)关闭时,主窗口也将关闭,程序必须重新运行 所需的行为是主窗口保持打开状态,允许用户生成其他图形。下面是我的代码的精简版本。有人知道在接受messagebox后,什么会触发主窗口关闭吗 from

我创建了一个tkinter GUI,允许用户选择csv文件并使用matplotlib生成图形。该程序在IDE中运行良好,但在冻结的可执行文件(cx\U冻结)中创建一个图形后关闭。exe运行时没有错误-它在默认照片编辑器中调出图形,然后显示带有图表文件路径的弹出窗口。当弹出窗口(tx.messagebox)关闭时,主窗口也将关闭,程序必须重新运行

所需的行为是主窗口保持打开状态,允许用户生成其他图形。下面是我的代码的精简版本。有人知道在接受messagebox后,什么会触发主窗口关闭吗

from pathlib import Path
from tkinter import Tk
from tkinter import Frame
from tkinter import Button
from tkinter import filedialog
import tkinter.messagebox as messagebox
import os
import traceback
import matplotlib.pyplot as plt

class graphing_program(Tk):
    def __init__(self,*args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        self.window_width   = 300
        self.window_height  = 150
        self.geometry(f"{str(self.window_width)}x{str(self.window_height + 20)}+300+300")
        container = Frame(self)
        container.place(width=self.window_width,height=self.window_height)
        self.wm_title('Graphing Program')
        self.button = Button(self, text="Select Files",
                            command= self.open_file)
        self.button.place(relwidth = 1,relheight = 1,relx=0,rely=0)
        #%% Variables
        self.data_dir = Path('.')
  
    def open_file(self):
        try:
            temp_files = filedialog.askopenfilenames(initialdir = self.data_dir,title = "Select File",filetypes = (("csv","*.csv"),("all files","*.*")))
            savefn = lp.plot_data(temp_files)
            os.startfile(savefn)
            messagebox.showinfo('Chart Generated',f'Chart Path:\n\t{savefn}')
        except:traceback.print_exc();raise

class lp():
    def plot_data(file_paths):
        savefn = Path('./example_graph.png')
        x = [1,2,3,4,5,6,7,8,9,10]
        y = [1,2,3,4,5,6,7,8,9,10]
        fig1 =  plt.figure(figsize=[7.5,10]) 
        plt.plot(x,y,'-',color = 'r')
        fig1.savefig(fname= savefn,format="png")
        plt.close(fig1)
        return(savefn)          

if __name__ == "__main__":
    app = graphing_program()
    app.mainloop()

您是从文件浏览器还是命令提示符启动程序?TigerhawkT3-我是从cx_freeze bdist_msi生成的桌面快捷方式启动程序。从命令提示符(终端仿真器)手动执行该快捷方式。当它崩溃时,错误仍将显示在命令提示窗口中。如果不这样做,则会显示错误,但窗口会立即关闭,因此您无法调查问题。TigerhawkT3-程序在命令提示符下执行时不会出错。重新启动新图表的程序只是一个小小的不便,但我很好奇为什么会发生这种情况。可能不值得深入研究。您是从文件浏览器还是命令提示符启动程序?TigerhawkT3-我是从cx_freeze bdist_msi生成的桌面快捷方式启动程序。从命令提示符(终端仿真器)手动执行该快捷方式。当它崩溃时,错误仍将显示在命令提示窗口中。如果不这样做,则会显示错误,但窗口会立即关闭,因此您无法调查问题。TigerhawkT3-程序在命令提示符下执行时不会出错。重新启动新图表的程序只是一个小小的不便,但我很好奇为什么会发生这种情况。这可能不值得深入调查。