在 tkinter 中创建多个框架

creating multiple frames in 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.", 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()

框架是 tkinter 中的容器,它与 windows (Tk) 共享承载小部件的能力,就像你的根 Window 或者附加 windows 顶级.

到目前为止,您所做的是在每个小部件中将 master 参数设置为 root。例如:

submit = Button(root,...)

你想要的是不同的容器,你可以通过Frames创建它们。要在另一个容器中打包或网格化您的小部件,您必须更改主参数。

喜欢:

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

my_frame.pack()
submit.pack()

此简化代码将 打包 根 window 中的框架和 my_frame 中的提交按钮。此架构使您能够更改框架,意味着用不同的容器填充 window。

另请看一下 OOP 编程,因为它使您的事情变得更干净、更容易。

您可能对

感兴趣

对于您要实现的目标,您需要 3 帧。您还需要一个变量来存储当前可见的帧,稍后将用于隐藏前一帧。

现在你可以利用pack/gridpack_forget/grid_forget来show/hide个不同的帧。

这是一个例子:

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()