-1

I have a user login code. here when the application starts it to show a " Welcome ....are you a new user? ...yes?...no?". Then the user clicks on yes or no and the respective frame appears. When I wrote the code I had not incorporated the GUI alongside and now I'm confused as to how to go about it. I have searched for various ways of creating multiple frames in Python but it still hasn't helped me come up with a solution. If someone could pls help.

# import openpyxl and tkinter modules
from openpyxl import *
from tkinter import *

# globally declare wb and sheet variable

# opening the existing excel file
wb = load_workbook('C:\\Users\\HP\\PycharmProjects\\pythonProject\\database.xlsx')

# create the sheet object
sheet = wb.active


def excel():
    # resize the width of columns in excel spreadsheet
    sheet.column_dimensions['A'].width = 30
    sheet.column_dimensions['B'].width = 30
    sheet.column_dimensions['C'].width = 50

    # write given data to an excel spreadsheet at particular location
    sheet.cell(row=1, column=1).value = "Username"
    sheet.cell(row=1, column=2).value = "Main Password"
    sheet.cell(row=1, column=3).value = "Email ID"

# Function to set focus (cursor)
def focus1(event):
    # set focus on the username_field box
    username_field.focus_set()

def focus2(event):
    # set focus on the email_id_field box
    main_password_field.focus_set()

def focus3(event):
    # set focus on the form_no_field box
    email_id_field.focus_set()

# Function for clearing the contents of text entry boxes
def clear():
    # clear the content of text entry box
    username_field.delete(0, END)
    main_password_field.delete(0, END)
    email_id_field.delete(0, END)

# Function to take data from GUI window and write to an excel file
def insert_OldUser():
    # welcome back existing user
    signin = Label(root, text="SIGN-IN", font= "50", bg="light blue")
    signin.grid(row = 0 , column =1)
    # if user not fill any entry then print "-"
    if (username_field.get() == "" and
            main_password_field.get() == "" and
            email_id_field.get() == ""):
        print("-")

    else:
        # assigning the max row and max column value upto which data is written in an excel sheet to the variable
        current_row = sheet.max_row
        current_column = sheet.max_column

        # get method returns current text as string which we write into excel spreadsheet at specific cell
        sheet.cell(row=current_row + 1, column=1).value = username_field.get()
        sheet.cell(row=current_row + 1, column=2).value = main_password_field.get()
        sheet.cell(row=current_row + 1, column=3).value = email_id_field.get()

        #call func to compare passwords
        incorrect_pass()
        # save the file
        wb.save('C:\\Users\\HP\\PycharmProjects\\pythonProject\\database.xlsx')

        # set focus on the username_field box
        username_field.focus_set()

        # call the clear() function
        clear()

def insert_newUser():
    # welcome back existing user
    signup = Label(root, text="PLEASE SIGN UP.", font= "50", bg="light blue")
    signup.grid(row = 0 , column =1)
    # if user not fill any entry then print "-"
    if (username_field.get() == "" and
            main_password_field.get() == "" and
            email_id_field.get() == ""):
        print("-")

    else:
        # assigning the max row and max column value upto which data is written in an excel sheet to the variable
        current_row = sheet.max_row
        current_column = sheet.max_column

        # get method returns current text as string which we write into excel spreadsheet at specific cell
        sheet.cell(row=current_row + 1, column=1).value = username_field.get()
        sheet.cell(row=current_row + 1, column=2).value = main_password_field.get()
        sheet.cell(row=current_row + 1, column=3).value = email_id_field.get()

        existing_username()

        # save the file
        wb.save('C:\\Users\\HP\\PycharmProjects\\pythonProject\\database.xlsx')

        # set focus on the username_field box
        username_field.focus_set()

        # call the clear() function
        clear()

def existing_username():
    for sheet in database:
        for t in username_field:
            if t==username_field:
                printl3 = Label(text= "Username not available.Try Again" , bg = "light blue")
                printl3.grid(row = 17 , column = 1)
                insert_newUser()

def incorrect_pass():
    for sheet in database:
        for t in main_password_field:
            if t==main_password_field.get():
                printl1= Label(text= "LOGIN SUCCESSFUL ! " , bg ="light blue" )
                printl1.grid(row = 17 , column = 1)
            else :
                printl2 = Label(text ="Password does not match. Try Again. ", bg = "light blue")
                printl2.grid(row =17 , column =1)

# Driver code
if __name__ == "__main__":
    # create a GUI window
    root = Tk()

    # set the background colour of GUI window
    root.configure(background='light blue')

    # set the title of GUI window
    root.title("PASSWORD MANAGER")

    # set the configuration of GUI window

    root.geometry("500x500")

    excel()

    # create labels

    welcome = Label(root , text="WELCOME" , font = "Times 30 bold" , bg="light blue")
    ques1 = Label(root, text="Are you a new user?", font = '50bold' , bg="light blue")
    yes1 = Button(root, text="YES", bg="Black", fg="White", command=insert_newUser)
    white1 = Label(root , fg = "light blue" , bg = "light blue" , text="             ")
    no1 = Button(root, text="NO", bg="Black", fg="White", command=insert_OldUser)

    heading = Label(root, text="user details", bg="light blue")
    username = Label(root, text="Username", bg="light blue")
    main_password = Label(root, text="Main Password", bg="light blue")
    email_id = Label(root, text="Email-ID", bg="light blue")

    # grid method is used for placing the widgets at respective positions in table like structure .
    welcome.grid(row=0 , column = 1)
    ques1.grid(row=1, column = 1)
    yes1.grid(row=2, column = 1)
    no1.grid(row=4 , column = 1)
    white1.grid(row=3 , column =1)
    heading.grid(row=10, column=1)
    username.grid(row=11, column=0)
    main_password.grid(row=12, column=0)
    email_id.grid(row=13, column=0)

    # create a text entry box for typing the information
    username_field = Entry(root)
    main_password_field = Entry(root)
    email_id_field = Entry(root)

    # bind method of widget is used for the binding the function with the events
    # whenever the enter key is pressed then call the focus function respectively
    username_field.bind("<Return>", focus1)
    main_password_field.bind("<Return>", focus2)
    email_id_field.bind("<Return>", focus3)


    username_field.grid(row=11, column=1, ipadx="100")
    main_password_field.grid(row=12, column=1, ipadx="100")
    email_id_field.grid(row=13, column=1, ipadx="100")

    # call excel function
    excel()

    # create a Submit Button and place into the root window
    submit = Button(root, text="Submit", fg="Black",  bg="Red", command=clear)
    submit.grid(row=20, column=1)


    # start the GUI
    root.mainloop()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • Have you considered using one frame and just updating the content of that frame? That is normally a cleaner solution. – Mike - SMT Jun 03 '21 at 13:52
  • 1
    Start by creating a [mcve] with just the one welcome window and two buttons, then see how you can open a second window by clicking one of the buttons. Familiarize yourself with the basics of the problem before trying to write an entire program. You've posted a lot of code that isn't directly related to the problem being asked about. – Bryan Oakley Jun 03 '21 at 15:14

2 Answers2

1

A frame is a containter in tkinter, it shares the ability of carring widgets with windows (Tk), like your root Window or for additional windows Toplevel.

What you have done so far is to set the master argument to root in each of your widgets. For exampel:

submit = Button(root,...)

What you want are different containers and you can create them by Frames. To pack or grid your widgets in another container you have to change the master argument.

like:

...code...
my_frame = Frame(root,...)
submit = Button(my_frame,...)

my_frame.pack()
submit.pack()

This simplified code will pack the frame in the root window and the submit button in my_frame. This architecture gives you the ability to change frames, means to populate the window with a different container.

Please take also a look at OOP programming because it makes you things more clean and easier.

You may are intrested in link

Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
0

For what you are trying to achieve you need 3 frames. You will also need a variable to store the currently visible frame, which will later be used to hide the previous frame.

Now you can make use of pack/grid, pack_forget/grid_forget to show/hide different frames.

Here is an example:

import tkinter as tk


def back_to_main():
    global current_frame
    current_frame.pack_forget()
    main_frame.pack()
    current_frame = main_frame

def show_signUP():
    global current_frame
    current_frame.pack_forget()
    signup_frame.pack()
    current_frame = signup_frame

def show_signIn():
    global current_frame
    current_frame.pack_forget()
    signin_frame.pack()
    current_frame = signin_frame

def create_signUp():

    tk.Label(signup_frame, text="Sign Up").pack()
    tk.Entry(signup_frame).pack()
    tk.Entry(signup_frame, show='*').pack()
    tk.Button(signup_frame, text="Back", command=back_to_main).pack()
    
def create_signIn():
    tk.Label(signin_frame, text="Sign In").pack()
    tk.Entry(signin_frame).pack()
    tk.Entry(signin_frame, show='*').pack()
    tk.Button(signin_frame, text="Back", command=back_to_main).pack()

def create_mainPage():

    tk.Button(main_frame, text="new here?", command=show_signUP).pack()
    tk.Button(main_frame, text="already have an account?", command=show_signIn).pack()

root = tk.Tk()

main_frame = tk.Frame(root)
main_frame.pack()
signin_frame = tk.Frame(root)
signup_frame = tk.Frame(root)

current_frame = main_frame

create_mainPage()
create_signIn()
create_signUp()

root.mainloop()

Art
  • 2,836
  • 4
  • 17
  • 34