I have function in my program calls a class which opens a new window

class TopLevel3:
     def __init__(self):
        self.root2 = Toplevel()
        self.canvas3 = Canvas(self.root2, width=400, height=200)
        self.canvas3.pack()
        self.root2.title("ImageDB")
        r = 0
        for c in range(6):
            Label(relief=RIDGE,  width=55).grid(row=r, column=0)
            Entry(relief=SUNKEN, width=20).grid(row=r, column=1)
            Button(text='View Image').grid(row=r, column=2)
            Button(text='Update Tag').grid(row=r, column=3)
            r = r+1
        self.root2.mainloop()

This is called by a function in another class:

def img_db(self):
            TopLevel3()

But instead of the labels & buttons appearing in the new child window they appear in the parent window itself.
(Image attached)
Where am I going wrong?

You got to let Python know where you want to create the Tkinter widgets. By default it's the MainWindow root. Change your code like this ...

from Tkinter import *

class TopLevel3:
     def __init__(self):
        self.root2 = Toplevel()
        # lift root2 over MainWindow root1
        self.root2.lift(aboveThis=mw.root1)
        self.canvas3 = Canvas(self.root2, width=400, height=200)
        self.canvas3.pack()
        # easier parent name
        cv = self.canvas3
        self.root2.title("ImageDB")
        r = 0
        for c in range(6):
            # make canavas the parent here
            Label(cv, relief=RIDGE,  width=55).grid(row=r, column=0)
            Entry(cv, relief=SUNKEN, width=20).grid(row=r, column=1)
            Button(cv, text='View Image').grid(row=r, column=2)
            Button(cv, text='Update Tag').grid(row=r, column=3)
            r = r+1
        self.root2.mainloop()

class MainWindow:
    """class for testing"""
    def __init__(self):
        self.root1 = Tk()
        self.root1.title("MainWindow")
        
    def img_db(self):
        TopLevel3()


mw = MainWindow()
mw.img_db()

Again, I would import with a namespace.

Thank you, I got it now
I can see you have used .lift method .. I tend to overlook minute details like this while coding... :D

And I didn't import with a namespace as this code is a part of a bigger code and I'd to make changes everywhere then..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.