简体   繁体   English

tkinker py2app应用程序无法关闭

[英]tkinker py2app application won't close

My Tkinter application will not shutdown properly when the X in the corner is clicked. 单击角落的X时,我的Tkinter应用程序将无法正确关闭。 The window closes but is still visible in the dock. 窗口关闭,但在扩展坞中仍然可见。 A force quit is required. 需要强制退出。

My Application compares two excel sheets and outputs a filtered excel sheet. 我的应用程序比较两个Excel工作表并输出一个过滤的Excel工作表。

I have used py2app to make it executable. 我已经使用py2app使其可执行。

Here is my app: 这是我的应用程序:

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()

You may need to add a handler for the WM_DELETE_WINDOW protocol: 您可能需要为WM_DELETE_WINDOW协议添加一个处理程序:

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

Protocols are how the Tk system interacts with the window manager. 协议是Tk系统与窗口管理器交互的方式。 So the above connects the window manager's close button to the root window's destory() method, which will end the Tk mainloop and should exit the application. 因此,上面的代码将窗口管理器的关闭按钮连接到根窗口的destory()方法,该方法将结束Tk mainloop并退出应用程序。

Although, this should have been the default behavior all along. 虽然,这应该一直都是默认行为。 Perhaps something with py2app has caused a problem here. 也许py2app引起了问题。 Or something else in your application (pandas maybe) requires some sort of shutdown. 或者您的应用程序中的其他内容(可能是熊猫)需要某种关闭。 In this case you could define a shutdown function for your app and do: 在这种情况下,您可以为应用定义关闭功能,然后执行以下操作:

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

To call it when the window manager's close button is pressed. 在按下窗口管理器的关闭按钮时调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM