How Do I Solve the Errors Within This Code?
Your code has several errors:
- In class
LoginPage
constructor (__init__()
function) you didn't callself.loginPage.mainloop()
, so the window wasn't displayed. RegistrationPage
inheritstk.Frame
, so you can't display its content in another window. The right way is to inheritRegistrationPage
fromtk.Tk
.- When you call
super().__init__()
, the created object is assigned toself
, so you don't need to assign theregistration_frame
variable. And onwards you can useself
instead ofregistration_frame
. - Call
Button(self.frame, ...)
always fails because there isn't aframe
property in theRegistrationPage
class or its parent class. Instead ofself.frame
you can just writeself
. - You can't
pack()
atk.Tk
object. Just remove this call. - Replace
self.registrationPage.mainloop()
byself.mainloop()
(See point 3).
self.loginPage.mainloop()
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.