在 tkinter 中创建多个框架

问题描述

我有一个用户登录代码。当应用程序启动时,此处显示“欢迎....您是新用户吗?...是?...不是?”。然后用户点击是或否,相应的框架就会出现。 当我编写代码时,我没有将 GUI 合并到一起,现在我对如何去做感到困惑。我已经搜索了在 Python 中创建多个框架的各种方法,但它仍然没有帮助我想出一个解决方案。如果有人可以帮忙。

# 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.",bg="light blue")
    signup.grid(row = 0,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",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,ipadx="100")
    email_id_field.grid(row=13,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()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)