关闭python Tkinter多个对话框

关闭python Tkinter多个对话框,tkinter,python-3.5,Tkinter,Python 3.5,我正在用tkinter做一个GUI来做一些基本的微积分。我的问题是我无法关闭一些对话框。我想关闭它们,并根据用户的选择改变可视化效果。我的代码不会等到窗口关闭时才这样做,而是等到主类结束时。我需要帮忙修理它 class Plotia(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, *kwargs) tk.Tk.wm_title(self, "Plotia")

我正在用tkinter做一个GUI来做一些基本的微积分。我的问题是我无法关闭一些对话框。我想关闭它们,并根据用户的选择改变可视化效果。我的代码不会等到窗口关闭时才这样做,而是等到主类结束时。我需要帮忙修理它

class Plotia(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, *kwargs)
        tk.Tk.wm_title(self, "Plotia")
        s = ttk.Style()
        s.theme_use("clam")

        # Configuration of the window ....
        #....
        #....
        #....
        ## Initial Values ##############################

        self.Number_Graph = 1
        self.path_file = [None for i in range(self.Number_Graph)] # Number of files
        self.columnData = [4 for i in range(self.Number_Graph)] # Number of column of the data
        self.column_x = [0 for i in range(self.Number_Graph)]
        self.column_y = [0 for i in range(self.Number_Graph)]


    def askData(self):
        # I don't understand why ChoiceData don't close. 

        self.graph = ChoiceData(self.Number_Graph, self.columnData)
        # Only when the main window close,  this two lines executes, 
        # and change this two variables. I want that this two lines executes when
        #Choice Data close
        self.column_x[self.graph.selection] = self.graph.data[0]
        self.column_y[self.graph.selection] = self.graph.data[1]







# This is is the Dialog  I can close correctly


class ChoiceData(tk.Toplevel):

    def __init__(self, NumberGraph, NumberData):
        super(ChoiceData, self).__init__()


        ## Initial Values #################################

        self.NumberGraph = NumberGraph
        self.selection = None
        self.numberData = NumberData
        self.data = []

        ########################################################

        # Layout Configure
        #...


    def Select(self):
        self.selection = int(self.BoxList.get())-1
        selectionData = SelectData(self.numberData[self.selection])
        self.data.append(selectionData.xData)
        self.data.append(selectionData.yData)
        self.destroy()



class SelectData(tk.Toplevel):

    def __init__(self, numberData):
        super(SelectData, self).__init__()



        ## Initial Values #################################

        self.xData= None
        self.yData = None
        self.NumberData = numberData

        ########################################################

        # Layout configuration.,,,

    def Update(self):
        self.xData = int(self.xBox.get())-1
        self.yData = int(self.yBox.get())-1
        self.destroy()


if __name__ == '__main__':
    app = Plotia()
    app.geometry("500x500")
    app.mainloop()
编辑: 从tkinter进口*

class Quit:
    def __init__(self, root):
        root.destroy()


class ChoiceData:
    def __init__(self,root):
        self.top = Toplevel(root)
        self.label = Label(self.top, text="ChoiceData")
        self.button = Button(self.top, text="Ok", command=self.stuff)
        self.top.protocol("WM_DELETE_WINDOW", lambda: Quit(self.top))
        self.label.pack()
        self.button.pack()

    def stuff(self):
        thing = SelectData(self.top)
        self.top.destroy()
        print("Windows closed")




class SelectData:
    def __init__(self,root):
        self.top = Toplevel(root)
        self.label = Label(self.top, text="SelectData")
        self.button = Button(self.top, text="Ok", command=self.closer)
        self.top.protocol("WM_DELETE_WINDOW", lambda: Quit(self.top))
        self.label.pack()
        self.button.pack()
        self.top.mainloop()
    def closer(self):
        self.top.destroy()




root = Tk()
ChoiceData(root)
root.mainloop()

这是我想做的事情的简化版本。选择数据关闭时,选择数据保持打开状态。

如果您所要做的只是对两个
Toplevel
小部件执行操作,请关闭两个
Toplevel
小部件并运行一段代码,然后类似下面的操作即可获得所需的结果

from tkinter import *

class App:
    def __init__(self, root):
        self.root = App.root = root
        ChoiceData()
        SelectData()
    def close():
        ChoiceData.top.destroy()
        SelectData.top.destroy()
        print("Windows closed")

class ChoiceData:
    def __init__(self):
        self.top = ChoiceData.top = Toplevel(App.root)
        self.label = Label(self.top, text="ChoiceData")
        self.button = Button(self.top, text="Ok", command=App.close)
        self.top.protocol("WM_DELETE_WINDOW", App.close)
        self.label.pack()
        self.button.pack()

class SelectData:
    def __init__(self):
        self.top = SelectData.top = Toplevel(App.root)
        self.label = Label(self.top, text="SelectData")
        self.button = Button(self.top, text="Ok", command=App.close)
        self.top.protocol("WM_DELETE_WINDOW", App.close)
        self.label.pack()
        self.button.pack()

root = Tk()
App(root)
root.mainloop()
这里我们有四个触发器来运行
App.close()
函数

前两个是我们在每个
Toplevel
中创建的
按钮
小部件,它们的
命令
属性都设置为
App.close
将运行此功能,关闭两个窗口并运行我们插入的任何代码段


另外两个更有趣,我们覆盖了
WM\u DELETE\u WINDOW
事件,当按下红色的小“X”时,它会关闭一个窗口,相反,我们告诉它运行
App.close()
,它会关闭两个窗口并运行代码段。

最后,我发现我的GUI上有什么问题,在最后一行,我应该使用:

self.quit()
而不是:

self.destroy()
self.destroy()不允许在Select数据的主循环之后继续程序。
谢谢@Ethan Field对我的帮助。

请不要共享来自外部站点的代码。读完后你应该把它贴在这里。“不能”是什么意思?你为什么不能呢?尝试时会发生什么?当我按“确定”按钮时,选择数据对话框关闭,但选择数据对话框保持打开状态。self.column_x[self.graph.selection]=self.graph.data[0];self.column_y[self.graph.selection]=self.graph.data[1]即使手动关闭选项数据,在关闭主窗口(Plotia)之前,该行不会执行。我想在关闭Select Data时自动关闭ChoiceData,然后执行这两行。App类关闭顶级窗口,但这不是我的重点。使用主类中的菜单对象打开选项数据。此菜单对象连接到打开选项数据的函数(askData)。Choice Data(选择数据)使用按钮分配打开选择数据,该功能在选择数据关闭时,选择数据应关闭,但不会关闭。我通过对代码@Ethan字段的简短更改来编辑我的问题,此更改在我的GUI中显示了我的问题,即使我使用self.top.destroy关闭选项数据,选项数据仍保持打开状态。@samuel我不明白您想要什么,您希望选项数据保持打开状态,无论发生什么,还是希望它关闭?如果您希望它关闭,它应该在什么时候关闭,在什么条件下关闭?