如何在 运行 另一个函数后关闭 tkinter 对话框?

How to close a tkinter dialog box after running another function?

我正在使用 python tkinter 并弹出一个对话框 window。我在框中有两个按钮:

from tksheet import Sheet
from tkinter import *
import os 
import sys
import mapMaker2


root=Tk()
root.title('Map Tool')
root.geometry("750x750")


sheetframe = Frame(root)
sheetframe.grid(row = 0, column = 0,)


buttonEditlabel = Button(sheetframe, text='Edit Labels', width=12, command=lambda: [openEditWindow()], bg='#cacccf',fg='black')
buttonEditlabel.grid(row = 0, sticky=W, column = 0, pady = (25,5), padx = (50,0))


def openEditWindow():

    top = Toplevel(root)
    top.geometry("260x195")
    top.title('Edit Axes Labels')
    frm = Frame(top, borderwidth=0, relief='ridge')
    frm.grid(row = 0, column = 0, pady = (20,0),padx=(20,0))


    b_cancel = Button(frm, text='Close', width=10)
    b_cancel['command'] = top.destroy
    b_cancel.grid(column = 0, row = 6, pady = (15,0),padx=(0,0))

    b_save = Button(frm, text='Save', width=10)
    b_save['command'] = lambda: editLabels()
    b_save.grid(column = 1, row = 6, sticky = E, pady = (15,0),padx=(0,0))


    def editLabels():
        pass


mainloop()

取消 按钮关闭 window 和 top.destroy 命令。我希望 Save 按钮也能在 运行 之后先关闭 window editLabels() 函数。我试过:

b_save['command'] = [lambda: editLabels(), top.destroy]

但这行不通。

这是您可以做到的一种方法。创建一个函数来销毁顶部window.

def kill_main():
    top.destroy()
    top.update()

然后在任何地方调用该函数。您不必将 kill_main() 添加到按钮本身。只需将它放在您要打开的下一个函数中,这样它将首先关闭 Top windows,然后 运行 editlabels() 函数的其余部分。希望对您有所帮助。

def editLabels():
    kill_main()
    pass