我在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 import *
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()
这是我想要做的简化版本。选择数据关闭时,选择数据保持打开状态。
答案 0 :(得分:0)
如果你要做的就是在两个Toplevel
小部件上执行操作,关闭两个Toplevel
小部件并运行一段代码,那么下面的内容就会达到预期效果。< / p>
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()
函数运行。
前两个是我们在每个Button
内创建的Toplevel
小部件,它们都将command
属性设置为App.close
,这些属性将运行此功能,同时关闭两个窗口和运行我们插入的任何片段。
另外两个更有趣,我们覆盖WM_DELETE_WINDOW
事件,当小红色&#34; X&#34;处理关闭窗口时按下,而我们告诉它运行App.close()
关闭两个窗口并运行代码段。
答案 1 :(得分:0)
最后,我发现我的GUI上有什么不好,在最后一行,我应该使用:
self.quit()
而不是:
self.destroy()
self.destroy()不允许您在选择数据的主循环后继续您的程序。 谢谢@Ethan Field帮助我。