2017-10-08 85 views
0

我在tkinter中做了一些GUI来做一些基本的微积分。我的问题是我无法关闭一些对话框。我想关闭它们并通过用户的选择来改变可视化。我的代码不会等到窗口关闭才能这样做,而是等到主类的末尾。我需要帮助来解决它。关闭Python的Tkinter多个对话框

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

这是什么,我想做一个简单求版本。选择数据关闭时,选择数据保持打开。

+1

请不要共享外部网站的代码。在阅读[MCVE]之后,您应该将其粘贴到此处。 – Lafexlos

+0

“不能”是什么意思?你为什么不能?当你尝试时会发生什么? –

+0

当我按下确定按钮选择数据对话框关闭,但选择数据对话框保持打开状态。 。 self.column_x [self.graph.selection] = self.graph.data [0]; self.column_y [self.graph.selection] = self.graph.data [1]即使关闭主窗口(Plotia),也不会执行该行,即使手动关闭了选择数据。我想在关闭Select Data时自动关闭ChoiceData,然后执行这两行。 – samuel

回答

0

如果你所要做的只是在两个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这两者各有command属性设置为App.close将运行该函数内部创建Button小部件,关闭Windows和运行任何代码段,我们插入。

另外两个比较有趣一点,我们覆盖了WM_DELETE_WINDOW事件,它在处理按下小红色“X”时关闭窗口,而是告诉它运行App.close(),它关闭了窗口和运行该片段。

+0

应用程序类关闭TopLevel窗口,但这不是我的观点。选择数据使用主类中的Menu对象打开。这个Menu对象连接到一个打开选择数据的函数(askData)。选择数据打开选择数据使用按钮分配功能,当选择数据关闭时,选择数据应该关闭,但不是。 – samuel

+0

我编辑我的问题,对您的代码@Ethan Field进行简短的更改,此更改显示我的GUI中存在的问题,即使使用self.top.destroy将其关闭,Choice Data仍保持打开状态。 – samuel

+0

@samuel我不明白你想要什么,你希望Choice Data保持开放状态,不管你想要关闭什么或者想要关闭它吗?如果你想关闭它,什么时候应该关闭,在什么条件下? –

0

最后,我发现了什么是我的GUI坏了,在最后一行,我应该使用:

self.quit() 

代替:

self.destroy() 

self.destroy()不要让您在选择数据的主循环后继续执行程序。 谢谢@Ethan Field帮助我。