tkinker py2app应用程序不会关闭

时间:2015-05-13 14:57:06

标签: python tkinter py2app

单击角落中的X时,我的Tkinter应用程序将无法正常关闭。窗口关闭但仍在Dock中可见。强制戒烟是必需的。

我的应用程序比较两个Excel工作表并输出已过滤的Excel工作表。

我已经使用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()

1 个答案:

答案 0 :(得分:1)

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

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

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

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

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

按下窗口管理器的关闭按钮时调用它。