Saturday, 26 August 2023

Is it possible to create a window from a frame?

I have a frame and I want to make a Toplevel window from it. I want to make a system similar to how the web browser UI works. For example, in Google Chrome you can take a tab and make a new window from it. You can also add other tabs to that new window. That is what I have tried to "demonstrate" this behavior:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
moving_frame = tk.Frame()
moving_frame.pack()

notebook = ttk.Notebook(moving_frame, width=400, height=700)
notebook.pack()

movingFrame = tk.Frame(notebook, bg='green', width=400, height=700)
movingFrame2 = tk.Frame(notebook, bg='green', width=400, height=700)

lab = tk.Label(movingFrame, text='some text')
ent = tk.Entry(movingFrame)

ent2 = tk.Entry(movingFrame2, width=10, bg='grey30')

lab.pack()
ent.pack()
ent2.pack()

notebook.add(movingFrame, sticky='nesw', text='tab1')
notebook.add(movingFrame2, sticky='nesw', text='tab2')

def window_create(e):
    if notebook.identify(e.x, e.y):
        print('tab_pressed')
        frame_to_window = root.nametowidget(notebook.select())
        root.wm_manage(frame_to_window)

notebook.bind('<ButtonRelease-1>', window_create)
root.mainloop()

Note: This code works by pressing the tab, not by dragging it.

I wonder if I can somehow adjust the window that is created by using wm_manage. This function return None and works not as I thought, but close enough to what I need.

If that is not possible to make by using wm_manage, how should I do that?

I thought I can create a custom class, for example from a Frame, but there is a problem: new window should be the same to the frame that it is created from. If something has been changed by a user, e.g., User added a text to the Entry; Marked some checkbuttons; If he has used a Treeview hierarchy, like this:

, the new window should remember which "folders" are opened (that are marked as minus on the picture), which items are currently selected and so on. And that was only about the Treeview, similar things should be considered for every single widget. Quite a lot of work, so maybe there is a better way to do that.



from Is it possible to create a window from a frame?

No comments:

Post a Comment