关闭消息框后,Tkinter 程序自动关闭,没有错误

问题描述

我创建了一个 tkinter GUI,允许用户选择 csv 文件并使用 matplotlib 生成图形。这个程序在我的 IDE 中运行良好,但在我冻结的可执行文件 (cx_freeze) 中创建一个图形后关闭。 exe 运行没有错误 - 它在认照片编辑器中显示图表,然后显示带有图表文件路径的弹出窗口。当弹出窗口(tx.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,**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,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()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)