Notification texts go here Contact Us Buy Now!

how do i solve the errors within this code?

How Do I Solve the Errors Within This Code?

Your code has several errors:

  1. In class LoginPage constructor (__init__() function) you didn't call self.loginPage.mainloop(), so the window wasn't displayed.
  2. self.loginPage.mainloop()
    

  3. RegistrationPage inherits tk.Frame, so you can't display its content in another window. The right way is to inherit RegistrationPage from tk.Tk.
  4. When you call super().__init__(), the created object is assigned to self, so you don't need to assign the registration_frame variable. And onwards you can use self instead of registration_frame.
  5. Call Button(self.frame, ...) always fails because there isn't a frame property in the RegistrationPage class or its parent class. Instead of self.frame you can just write self.
  6. You can't pack() a tk.Tk object. Just remove this call.
  7. Replace self.registrationPage.mainloop() by self.mainloop() (See point 3).

The full code will be

import tkinter as tk
from tkinter import Tk, Frame, Label, Entry, Button


class LoginPage:
    def __init__(self):
        self.loginPage = Tk()
        self.loginPage.title('Login')
        self.loginPage.geometry('550x510+400+100')
        self.loginPage.configure(bg='#075897')
        self.loginPage.resizable(False, False)

        self.frame = Frame(self.loginPage, width=350, height=350, bg='#075897')
        self.frame.place(x=115, y=40)
        self.heading = Label(self.frame, text='Unidirect', fg='black', bg='#075897', font=('Roboto', 25, 'bold'))
        self.heading.place(x=80, y=15)

        self.lblEmail = Entry(self.frame, width=25, border=0, fg='black', bg='#075897', font=('Roboto', 9))
        self.lblEmail.place(x=45, y=100)
        self.lblEmail.insert(0, 'Email')
        self.lblEmail.bind('<FocusIn>', self.on_enter)
        self.lblEmail.bind('<FocusOut>', self.on_leave)
        Frame(self.frame, width=230, height=1, bg='Pink').place(x=45, y=120)

        self.lblPassword = Entry(self.frame, width=25, border=0, fg='black', bg='#075897', font=('Times New Roman', 9))
        self.lblPassword.place(x=45, y=150)
        self.lblPassword.insert(0, 'Password')
        self.lblPassword.bind('<FocusIn>', self.on_enter)
        self.lblPassword.bind('<FocusOut>', self.on_leave)
        Frame(self.frame, width=230, height=1, bg='black').place(x=45, y=170)

        self.btnLogin = Button(self.frame, width=24, text='Log In', fg='black', bg='#075897', highlightbackground='blue',
                          activeforeground='black', activebackground='#36bbf1', cursor='hand2', border=1, font=('Roboto', 11, 'bold'))
        self.btnLogin.place(x=25, y=210)

        self.txtRegister = Label(self.frame, text="Don't have an account?", fg='black', bg='#075897', font=('Arial', 9))
        self.txtRegister.place(x=75, y=245)

        self.btnRegisterPage = Button(self.frame, width=6, text='Register', border=0, fg='black', bg='#075897', activeforeground='black',
                                activebackground='#075897', cursor='hand2', font=('Roboto', 9, 'bold underline'), command=self.show_registration_page)
        self.btnRegisterPage.place(x=230, y=245)

        self.forgotPassword = Button(self.frame, text='Forgot Password?', bd=0, fg='black', bg='#075897', activeforeground='black',
                                activebackground='#075897', cursor='hand2', font=('Times New Roman', 9))
        self.forgotPassword.place(x=175, y=180)

        self.loginPage.mainloop()

    def on_enter(self, e):
        if e.widget == self.lblEmail:
            self.lblEmail.delete(0, 'end')
        elif e.widget == self.lblPassword:
            self.lblPassword.delete(0, 'end')

    def on_leave(self, e):
        if e.widget == self.lblEmail:
            if self.lblEmail.get() == '':
                self.lblEmail.insert(0, 'Email')
        elif e.widget == self.lblPassword:
            if self.lblPassword.get() == '':
                self.lblPassword.insert(0, 'Password')

    def show_registration_page(self):
        # self.loginPage.withdraw()
        RegistrationPage()


class RegistrationPage(tk.Tk):
    def __init__(self):
        super().__init__()
        label_firstname = Label(self, text="First Name:")
        label_firstname.grid(row=0, column=0, padx=10, pady=5)
        entry_firstname = Entry(self)
        entry_firstname.grid(row=0, column=1, padx=10, pady=5)

        label_lastname = Label(self, text="Last Name:")
        label_lastname.grid(row=1, column=0, padx=10, pady=5)
        entry_lastname = Entry(self)
        entry_lastname.grid(row=1, column=1, padx=10, pady=5)

        label_email = Label(self, text="Email:")
        label_email.grid(row=2, column=0, padx=10, pady=5)
        entry_email = Entry(self)
        entry_email.grid(row=2, column=1, padx=10, pady=5)

        label_password = Label(self, text="Password:")
        label_password.grid(row=3, column=0, padx=10, pady=5)
        entry_password = Entry(self, show="*")
        entry_password.grid(row=3, column=1, padx=10, pady=5)

        self.btnRegister = Button(self, width=24, text='Register', fg='black', bg='#075897', highlightbackground='blue',
                              activeforeground='black', activebackground='#36bbf1', cursor='hand2', border=1, font=('Roboto', 11, 'bold'))
        self.btnRegister.place(x=25, y=210)
        # self.pack()
        # self.mainloop()


LoginPage()

If I misunderstand you and actually you want to use RegistrationPage as a tk.Frame, not another window, skip point 4. Also, remove the self.registrationPage.mainloop() call.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.