-1

There are Three code files

Main.py :-

from tkinter import *
from tkinter.ttk import *
from tkinter import messagebox
from sqlite3 import *
from turtle import home
import home


class LoginWindow(Tk):
    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
    
        self.title("login")
        self.geometry("400x300")
    
        s = Style()
        s.configure('Header.TFrame', background = 'blue')
    
        header_frame = Frame(self, style= 'Header.TFrame')
        header_frame.pack(fill = X)
    
        s.configure('Header.TLabel', background = 'blue', foreground = 'white', font = ('Arial', 25))
    
        header_label = Label(header_frame,text="Office contact book", style='Header.TLabel')
        header_label.pack(pady=10)
        
        s.configure('Content.TFrame', background = 'white')
    
        content_frame = Frame(self, style= 'Content.TFrame')
        content_frame.pack(fill= BOTH, expand= TRUE)
    
        login_frame = Frame(content_frame, style= 'Content.TFrame')
        login_frame.place(relx=.5, rely=.5, anchor=CENTER)
    
        s.configure('Login.TLabel', background = 'white', font = ('Arial', 15))
    
        username_label = Label(login_frame, text= 'Username:', style= 'Login.TLabel')
        username_label.grid(row= 0, column= 0, pady= 5)
    
        self.username_entry = Entry(login_frame,width= 15, font = ('Arial', 15))
        self.username_entry.grid(row= 0, column= 1, pady= 5)
    
        password_label = Label(login_frame, text= 'Password:', style= 'Login.TLabel')
        password_label.grid(row= 1, column= 0, pady= 5)
    
        self.password_entry = Entry(login_frame,width= 15, font = ('Arial', 15), show= '*')
        self.password_entry.grid(row= 1, column= 1, pady= 5)
    
        s.configure("Login.TButton", font = ('Arial', 15))
    
        login_button = Button(login_frame, text ="Login",width= 15, style= 'Login.TButton', command= self.login_button_click)
        login_button.grid(row=3, column= 1, pady= 5)
    
    
    
    def login_button_click(self):
        con = connect('contacts.db')
        cur = con.cursor()
        cur.execute("select * from Login where Username = ? and Password = ?", (self.username_entry.get(), self.password_entry.get()))
        row = cur.fetchone()
        if row is not None:
            self.destroy()
            home.HomeWindow()
           
    
        else:
            messagebox.showerror("Error message", "Incorrect username or password.")
if __name__ == '__main__':
    lw = LoginWindow()
    lw.mainloop()

Home.py :-

from tkinter import *
from tkinter.ttk import *
from tkinter import messagebox
from sqlite3 import *
from turtle import width
import main
import changepassword


class HomeWindow (Tk):


def __init__(self, *args, **kwargs):
    Tk.__init__(self, *args, **kwargs)

    self.title("Home")
    self.state('zoomed')

    s = Style()
    s.configure('Header.TFrame', background = 'blue')

    header_frame = Frame(self, style= 'Header.TFrame', height= 50)
    header_frame.pack(fill = X)

    s.configure('Header.TLabel', background = 'blue', foreground = 'white', font = ('Arial', 25))

    header_label = Label(header_frame,text="Office contact book", style='Header.TLabel')
    header_label.pack(pady=10)

    navigation_frame = Frame(self, style= 'Header.TFrame', width= 50)
    navigation_frame.pack(side= LEFT, fill= Y)

    s.configure('Navigation.TButton', width= 30, font= ('Arial', 15))

    manage_contacts_button = Button(navigation_frame, text= 'Manage contacts', style= 'Navigation.TButton')
    manage_contacts_button.pack(ipady= 10, pady= 2)

    def change_password_button_click (self):
        changepassword.ChangePasswordFrame(content_frame)

    change_password_button = Button(navigation_frame, text= 'Change password', style= 'Navigation.TButton', command= self.change_password_button_click(*args))
    change_password_button.pack(ipady= 10, pady= 2)

    def logout_button_click (self):
        self.destroy()
        main.LoginWindow()

    # logout_button = Button(navigation_frame, text= 'Logout', style= 'Navigation.TButton', command= self.logout_button_click)
    # logout_button.pack(ipady= 10, pady= 2)
    
    s.configure('Navigation.TFrame', background = 'white')

    content_frame = Frame(self, style= 'Navigation.TFrame')
    content_frame.pack(fill= BOTH, expand= TRUE)



# lw = HomeWindow()
# lw.mainloop()

changepassword.py :-

    from tkinter import *
    from tkinter.ttk import *


    class ChangePasswordFrame (Frame):
    def __init__(self, parent):
    Frame.__init__(self, parent)


    s = Style()
    s.configure('TFrame', background = 'white')
    s.configure('TLabel', background = 'white', font = ('Arial', 25))

    self.place(relx = .5, rely = .5, anchor = CENTER)

    old_password_label = Label(self, text= "Old Password:")
    old_password_label.grid(row=0, column= 0)

    old_password_entry = Entry(self)
    old_password_entry.grid(row=0, column=1)

    New_password_label = Label(self, text= "New Password:")
    New_password_label.grid(row=1, column= 0)

    New_password_entry = Entry(self)
    New_password_entry.grid(row=1, column=1)

    confrim_password_label = Label(self, text= "Confrim new Password:")
    confrim_password_label.grid(row=2, column= 0)

    confrim_password_entry = Entry(self)
    confrim_password_entry.grid(row=2, column=1)

    change_password_button = Button(self, text = "Change Password")
    change_password_button.grid(row = 3, column = 1)

the error i get is: -

[Running] python -u "g:\Projects\Coding\Python\Folder files\xomtact book\main.py"
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Sandeep Sharma\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "g:\Projects\Coding\Python\Folder files\xomtact book\main.py", line 63, in login_button_click
    home.HomeWindow()
  File "g:\Projects\Coding\Python\Folder files\xomtact book\home.py", line 38, in __init__
    change_password_button = Button(navigation_frame, text= 'Change password', style= 'Navigation.TButton', command= self.change_password_button_click)
  File "C:\Users\Sandeep Sharma\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 2383, in __getattr__
    return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'change_password_button_click'

PLEASE HELP ME I'M STUCK WITH THIS PEICE OF CODE

BTW my name is Vedant

Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
  • your indention's are off. I tried fixing them but it appears still off. Please provide a [mre] in order to get help. – Thingamabobs Sep 05 '22 at 16:11
  • The problem is that you have the change password function inside the init function, so calling `self.change_password_button_click` makes the compiler think it should be outside the `__init__` function while it is not. You should define the function outside init function but inside the class. Check my answer for more details. – ss3387 Sep 05 '22 at 16:21
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Sep 05 '22 at 18:31

1 Answers1

0

This error is coming because there is no such thing as self.change_password_button_click in your HomeWindow class. You have never created that function in your HomeWindow class.

The problem is here:

class HomeWindow:
    def __init__(self)
        def change_password_button_click(self): # Here is the error because this function should not be indented it should be something like this below:
    def change_password_button_click(self): #This is where the function should be defined.

The problem is that you have the change password function inside the init function, so calling self.change_password_button_click makes the compiler think it should be outside the __init__ function.

If you want to have it indented then you should remove self as parameter in you change password function and call it as change_password_button_click(): Just another approach to solve your problem.

Hopefully this works for you. I don't have enough time to test your code. If it does work, you can tick this answer.

ss3387
  • 299
  • 3
  • 19