tkinter py2app 应用程序不会关闭

tkinker py2app application won't close

单击角落中的 X 后,我的 Tkinter 应用程序无法正常关闭。 window 关闭但在停靠栏中仍然可见。需要强制退出。

我的应用程序比较两个 excel sheet 并输出过滤后的 excel sheet.

我已经使用 py2app 使其可执行。

这是我的应用程序:

import Tkinter as tk
import pandas as pd


class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.short_path = ""
        self.long_path = ""
        self.output = ""

        self.short_path_label = tk.Label(self, text="Path to short file: ")
        self.short_path_btn = tk.Button(self, text="Browse", command=self.browse_short_path)

        self.long_path_label = tk.Label(self, text="Path to long file: ")
        self.long_path_btn = tk.Button(self, text="Browse", command=self.browse_long_path)

        self.column_label = tk.Label(self, text="Column to filter: ")
        self.column = tk.Entry(self)

        self.outpath_label = tk.Label(self, text="Output directory: ")
        self.out_path_btn = tk.Button(self, text="Browse", command=self.browse_out_path)

        self.file_name_label = tk.Label(self, text="Filename: ")
        self.file_name = tk.Entry(self)

        self.button = tk.Button(self, text="Run", command=self.on_button)

        self.short_path_label.pack()
        self.short_path_btn.pack()
        self.long_path_label.pack()
        self.long_path_btn.pack()
        self.column_label.pack()
        self.column.pack()
        self.outpath_label.pack()
        self.out_path_btn.pack()
        self.file_name_label.pack()
        self.file_name.pack()
        self.button.pack()

    def browse_short_path(self):
        from tkFileDialog import askopenfilename

        tk.Tk().withdraw()
        self.short_path = askopenfilename()

    def browse_long_path(self):
        from tkFileDialog import askopenfilename

        tk.Tk().withdraw()
        self.long_path = askopenfilename()

    def browse_out_path(self):
        from tkFileDialog import askdirectory

        tk.Tk().withdraw()
        self.output = askdirectory()

    def on_button(self):
        short_df = pd.io.excel.read_excel(self.short_path)
        long_df = pd.io.excel.read_excel(self.long_path)

        short_df = short_df[~short_df[str(self.column.get())].isin(long_df[str(self.column.get())].unique())]

        short_df.to_excel(self.output + "/" + self.file_name.get())

app = SampleApp()
app.mainloop()

您可能需要为 WM_DELETE_WINDOW 协议添加处理程序:

app = SampleApp()
app.protocol("WM_DELETE_WINDOW", app.destroy)
app.mainloop()

协议是 Tk 系统与 window 管理器交互的方式。所以上面将 window 管理器的关闭按钮连接到根 window 的 destory() 方法,这将结束 Tk 主循环并应该退出应用程序。

尽管如此,这应该一直是默认行为。也许 py2app 的某些东西在这里引起了问题。或者您的应用程序中的其他内容(可能 pandas)需要某种关闭。在这种情况下,您可以为您的应用程序定义一个关闭功能并执行:

app.protocol("WM_DELETE_WINDOW", app.shutdown)

当 window 经理的关闭按钮被按下时调用它。