Julius Zamora
Julius Zamora

Reputation: 13

How do you add label to another window tkinter using OOP

When I add Label to the second Window, It doesn't add to the Second Window It Added to the First Window.

class testing:
    def __init__(self,root):
        root.config(bg='orange')
        root.geometry("500x500")
        lbl = Label(text="WOW TEXT").pack()


class login:
    def __init__(self,root):
        root.config(bg='orange')
        root.geometry("500x500")
        self.login_form(root)


    def login_onlick(self):
        c.execute("SELECT * FROM Login WHERE Name = ? AND Password = ?",(self.e_name.get(),self.e_pass.get()))
        self.right_login = c.fetchall()

        if self.right_login:
            print("Login Now")
            self.top = testing(root=Tk())
        else:
            print("Wrong")    


    def login_form(self,root):
        self.lg_form=Label(root, text="Login Form",font="Times 40",bg='red',width=50)
        self.lg_form.pack()

        self.lbl_name=Label(root, text="Enter Name",font="times 20",bg='orange')
        self.lbl_name.pack()
        self.e_name = Entry(root,width=50)
        self.e_name.pack()
        self.lbl_pass=Label(root, text="Enter Password",font="times 20",bg='orange')
        self.lbl_pass.pack()
        self.e_pass = Entry(root,width=50)
        self.e_pass.pack()
        self.btn_create = Button(root,text="Login", bg ='green',width=13,height=4,command= self.login_onlick)
        self.btn_create.pack()

root = Tk()

login(root)

conn.commit()

root.mainloop()

The Output of My Program

Upvotes: 1

Views: 1047

Answers (2)

demian-wolf
demian-wolf

Reputation: 1858

Though it is a bad idea to create a Tk() instance instead of Toplevel() instance if the main window is created already, it doesn't cause your problem, and it is not necessary to replace Tk() with Toplevel() here.

When you are creating the Label widget in the Testing class you should specify the window it belongs to, otherwise, it will try to find it automatically (and not always the right window will be chosen). Simply replace the Testing class with:

class Testing:
    def __init__(self, root):
        root.config(bg='orange')
        root.geometry("500x500")
        Label(root, text="WOW TEXT").pack()

Upvotes: 0

tgikal
tgikal

Reputation: 1680

You need to use a Toplevel() as any secondary windows, tkinter does not work very well having multiple Tk() calls.

I attempted to fix your spacing as well and some function names such as init to __init__.

from tkinter import *

class testing:
    def __init__(self, root):
        root.config(bg='orange')
        root.geometry("500x500")
        lbl = Label(text="WOW TEXT").pack()

class login:
    def __init__(self, root):
        self.login_form(root)

    def login_onlick(self, root):
        # Commented out for Testing
        #c.execute("SELECT * FROM Login WHERE Name = ? AND Password = ?",(self.e_name.get(),self.e_pass.get()))
        #self.right_login = c.fetchall()
        self.right_login = True #TESTING
        if self.right_login:
                print("Login Now")
                self.login_form(Toplevel(root))
        else:
                print("Wrong")

    def login_form(self, root):
        root.config(bg='orange')
        root.geometry("500x500")

        self.lg_form=Label(root, text="Login Form",font="Times 40",bg='red',width=50)
        self.lg_form.pack()

        self.lbl_name=Label(root, text="Enter Name",font="times 20",bg='orange')
        self.lbl_name.pack()

        self.e_name = Entry(root,width=50)
        self.e_name.pack()

        self.lbl_pass=Label(root, text="Enter Password",font="times 20",bg='orange')
        self.lbl_pass.pack()

        self.e_pass = Entry(root,width=50)
        self.e_pass.pack()

        self.btn_create = Button(root,text="Login", bg ='green',width=13,height=4,command=lambda:self.login_onlick(root))
        self.btn_create.pack()

root = Tk()
login(root)

# Commented out for Testing
#conn.commit()

root.mainloop()

enter image description here

Upvotes: 1

Related Questions