Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Is there any way to duplicate a window in tkinter ?

Here's the code:

from tkinter import *
root = Tk()
root.geometry("500x100")

def new_window():
    # Code to duplicate the window
    pass

button = Button(root , text = "Sample button")
button.pack(pady = 20)

mainmenu = Menu(root)
root.config(menu = mainmenu)

file_menu = Menu(mainmenu , tearoff = False)
menu_1 = Menu(mainmenu , tearoff = False)
menu_2 = Menu(mainmenu , tearoff = False)
menu_3 = Menu(mainmenu , tearoff = False)
menu_4 = Menu(mainmenu , tearoff = False)

mainmenu.add_cascade(label = "File" , menu = file_menu)
mainmenu.add_cascade(label = "Menu 1" , menu = menu_1)
mainmenu.add_cascade(label = "Menu 2" , menu = menu_2)
mainmenu.add_cascade(label = "Menu 3" , menu = menu_3)
mainmenu.add_cascade(label = "Menu 4" , menu = menu_4)

file_menu.add_command(label = "Duplicate window" , command = new_window)
menu_1.add_command(label = "Sub-menu 1")
menu_2.add_command(label = "Sub-menu 2")
menu_3.add_command(label = "Sub-menu 3")
menu_4.add_command(label = "Sub-menu 4")

mainloop()

What I want is to create a new window and all the elements that are in the first window should be there in the new window as well.

Is there any way to achieve this in tkinter without using classes ?

It would be great if anyone could help me out.

question from:https://stackoverflow.com/questions/65640872/duplicating-a-window-in-tkinter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
94 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use class to present the UI, then you can create many similar UI using the class:

from tkinter import *

class MyUI:
    def __init__(self, parent):
        parent.geometry("500x100")

        button = Button(parent, text="Sample button")
        button.pack(pady=20)

        mainmenu = Menu(parent)
        parent.config(menu=mainmenu)

        file_menu = Menu(mainmenu, tearoff=False)
        menu_1 = Menu(mainmenu, tearoff=False)
        menu_2 = Menu(mainmenu, tearoff=False)
        menu_3 = Menu(mainmenu, tearoff=False)
        menu_4 = Menu(mainmenu, tearoff=False)

        mainmenu.add_cascade(label="File", menu=file_menu)
        mainmenu.add_cascade(label="Menu 1", menu=menu_1)
        mainmenu.add_cascade(label="Menu 2", menu=menu_2)
        mainmenu.add_cascade(label="Menu 3", menu=menu_3)
        mainmenu.add_cascade(label="Menu 4", menu=menu_4)

        file_menu.add_command(label="Duplicate window", command=self.new_window)
        menu_1.add_command(label="Sub-menu 1")
        menu_2.add_command(label="Sub-menu 2")
        menu_3.add_command(label="Sub-menu 3")
        menu_4.add_command(label="Sub-menu 4")

    def new_window(self):
        win = Toplevel()
        MyUI(win)

root = Tk()
MyUI(root)
root.mainloop()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...