0

i learned treeview and tried to do this style of code just to try different perspective on how to open treeview and currently i tried to do this type and the treeview does not appear on toplevel and currently this is what i did:

from tkinter import ttk
import tkinter as tk

class setup(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self._frame = None
        self.switch_frame(mainpage)

    def switch_frame(self, frame_class):
        """Destroys current frame and replaces it with a new one."""
        new_frame = frame_class(self)
        if self._frame is not None:
            self._frame.destroy()
        self._frame = new_frame
        self._frame.place(x=0, y=0, relwidth=1, relheight=1)

class mainpage(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self,master)
        master.geometry("400x400")
        treeview = tk.Button(self, text="open treeview", command=lambda: display_treeview()).pack()

class my_treeview(tk.Toplevel):
    def __init__(self, tree):
        tk.Toplevel.__init__(self)
        self.tree = tree
        tree = ttk.Treeview()
        self.title("this is toplevel for treeview")
        self.geometry("600x500")

class display_treeview(ttk.Treeview):
    def __init__(self):
        ttk.Treeview.__init__(self)
        self.insert("", "0", "item1", text="fill width")
        self.insert("", "0", "item2", text="fill width")
        my_treeview(display_treeview)

if __name__ == "__main__":
    app = setup()
    app.mainloop()

the last working example i just did this:

from tkinter import ttk
import tkinter as tk


def my_treeview():
    mt = tk.Toplevel()
    mt.geometry("1000x580")

    tree = ttk.Treeview(mt)
    tree.insert("", "0", "item1", text="fill width")
    tree.insert("", "1", "item2", text="fill height")

    tree.pack(fill="both")


root = tk.Tk()
root.geometry("400x400")

treeview = tk.Button(root, text="open treeview", command=my_treeview).pack()

root.mainloop()
zakwan
  • 1
  • 2
  • I'm sure you had this working at some point, can you provide the last working example? – CrunchyLentils Dec 04 '21 at 15:02
  • @CrunchyLentils i edit it thats what i did without any class that has been created – zakwan Dec 04 '21 at 15:17
  • You didn't call the `pack()` method on the tree. It can't show in the window if you don't put it there. Also, you need to change the `tree = ttk.Treeview()` to `tree = ttk.Treeview(self)`, so that it's packed in the right window. – Sylvester Kruin Dec 04 '21 at 17:55
  • I'm sorry Sylvester, but you either; didn't test your fix on the code, or you made alterations that you have not explained. Can you post an answer with a code example that works so others can see all the fixes that had to occur? – CrunchyLentils Dec 04 '21 at 18:13

1 Answers1

0

You never told the tree what its window was it parent.

class my_treeview(tk.Toplevel):
    def __init__(self, tree):
        tk.Toplevel.__init__(self)
        self.tree = tree
        tree = ttk.Treeview()
        self.title("this is toplevel for treeview")
        self.geometry("600x500")

But that's only the beginning of the rabbit hole you are in.

TLDR: So you must create your Toplevel before you initialize your treeview so that you can pass the Toplevel window as the tree parent.

And so you dont go to much deeper into a hole that has no bottom.

Lets talk briefly about widgets, when you create a widget you need to give it a window to hold onto its 'parent'.

button = tk.Button(parent, text, command)

Then you place/pack/ or assign it to a grid position.

Someone had done some researching and had apparently discovered you cant necessarily change the parent of a widget after its initialized. https://stackoverflow.com/questions/6285648/can-you-change-a-widgets-parent-in-python-tkinter#:~:text=You%20cannot%20move%20a%20widget,recursively%20to%20a%20new%20parent.

This is what I came up with:

class My_Treeview(tk.Toplevel):
    def __init__(self):
        super().__init__()
        self.geometry("600x500")
        self.tree = ttk.Treeview(self)
        self.tree.insert("", "0", "item1", text="fill width")
        self.tree.insert("", "1", "item2", text="fill height")

        self.tree.pack(fill="both")



class main_window(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry('400x400')
        button_treeview = tk.Button(self, text="open treeview", command=lambda: My_Treeview())
        button_treeview.pack()

    def create_toplevel(widget):
        tp = tk.Toplevel()
        widget = widget(tp)

        tp.mainloop()

def main():
    app = main_window()
    app.mainloop()

if __name__ == '__main__':
    main()

You can add functions to this class to add functionality to the tree within it