【问题标题】:Close python Tkinter multiple dialogs关闭 python Tkinter 多个对话框
【发布时间】:2018-03-20 00:39:41
【问题描述】:

我正在 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()

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

【问题讨论】:

  • 请不要分享来自外部网站的代码。您应该在阅读minimal reproducible example后将其粘贴到此处。
  • “不能”是什么意思?为什么你做不到?当你尝试时会发生什么?
  • 当我按下确定按钮时,选择数据对话框关闭,但选择数据对话框保持打开状态。 . self.column_x[self.graph.selection] = self.graph.data[0]; self.column_y[self.graph.selection] = self.graph.data[1] 即使我手动关闭选择数据,在我关闭主窗口(Plotia)之前,这条线也不会执行。我想在关闭 Select Data 时自动关闭 ChoiceData,然后执行这两行。

标签: tkinter python-3.5


【解决方案1】:

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

self.quit()

代替:

self.destroy()

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

【讨论】:

    【解决方案2】:

    如果您只想对两个Toplevel 小部件执行操作,关闭两个Toplevel 小部件并运行sn-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() 函数运行。

    前两个是我们在每个Toplevel 中创建的Button 小部件,它们的command 属性都设置为App.close,它将运行这个函数,关闭两个窗口并运行我们插入的任何sn-p。

    另外两个更有趣一些,我们重写了WM_DELETE_WINDOW事件,当按下红色小“X”时,它处理窗口的关闭,而是告诉它运行App.close(),它关闭两个窗口并运行sn-p。

    【讨论】:

    • App 类关闭 TopLevel 窗口,但这不是我的意思。选择数据使用主类中的 Menu 对象打开。此菜单对象连接到打开选择数据的函数(askData)。选择数据打开使用按钮分配选择数据,在选择数据关闭时,选择数据应该关闭,但它没有。 span>
    • 我通过对代码@Ethan Field 的简短更改来编辑我的问题,此更改在我的 GUI 中显示了我的问题,即使我使用 self.top.destroy 关闭它,选择数据仍保持打开状态。
    • @samuel 我不明白您想要什么,您希望 Choice Data 无论如何都保持打开状态,或者您希望它关闭?如果你想让它关闭,它应该在什么时候关闭,在什么条件下?
    猜你喜欢
    • 1970-01-01
    • 2014-08-01
    • 2015-05-24
    • 2017-12-01
    • 1970-01-01
    • 2020-12-19
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多