我找不到清除窗户的方法

时间:2018-04-13 14:21:03

标签: python tkinter python-3.6

import tkinter as tk

class main_window(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        win_button = tk.Button(self, text="Searching and sorting algorithms",
                               command=self.window)
        win_button.grid(column=1,row=1)

        win_button2 = tk.Button(self, text="BIG 0",
                               command=self.window)
        win_button2.grid(column=3,row=1)

        win_button3 = tk.Button(self, text="Tree Traversal",
                               command=self.window)
        win_button3.grid(column=1, row=3)

        win_button4 = tk.Button(self, text="profix,infix and prefix",
                               command=self.window)
        win_button4.grid(column=3, row=3)

    def all_children (wid) :
        _list = wid.winfo_children()

        for item in _list :
            if item.winfo_children() :
                _list.extend(item.winfo_children())

        return _list

    def clear(self):
        list= root.grid_slaves()
        for k in list:
            k.destroy()


    def window(self):
        self.clear()

if __name__ == "__main__":
    global root
    root = tk.Tk()
    main_window(root).pack(fill="both", expand=True)
    root.mainloop()

我试图让它创建一个具有4种选择的屏幕。然后,窗口将清除用户选择的窗口并弹出新信息,但我在清除时遇到问题。我试过了 。毁灭,但确实有效。

1 个答案:

答案 0 :(得分:0)

您的问题只是您正在调用root.grid_slaves,但直接在root中的唯一窗口由pack而不是grid管理。不要使用root.grid_slaves(),而是在self.grid_slaves()中使用clear