简体   繁体   中英

How to close multiple Tkinter windows with one button

I am trying to build a python application. The first UGI is requesting the username and password. Then a second tk window pops up if login is successful with an okay button. Once then user press the okay button, both tk windows (the one requesting username and password and the login successful msg window) will disappear and the program continues to run.

I am very stuck with being able to press the okay button and close "both" windows. Any insights will be appreciated, thank you!

All the codes seem to be working fine. the command to execute function LoginSuccessful within the CheckLogin is able to execute the print("ANCD") but it doesn't close the two tk windows.

#import packages
import time
import openpyxl
from openpyxl import load_workbook
import tkinter as tk
from tkinter import *
import sys

def Function():

    global user1
    user1 = 'testing'
    password1 = '0000' 



    def Login_form():
        global username
        global password
        global rootA


        rootA = Tk() 
        rootA.title('User Authentication')

        msgbox1 = Label(rootA, text='Please Login\n') 
        msgbox1.grid(sticky=E) 

        username = Label(rootA, text='Username: ') 
        password = Label(rootA, text='Password: ')
        username.grid(row=1, sticky=W)
        password.grid(row=2, sticky=W)

        username = Entry(rootA) 
        password = Entry(rootA, show='*') 
        username.grid(row=1, column=1)
        password.grid(row=2, column=1)

        login_btn = Button(rootA, text='Login', command=CheckLogin) 
        exit_btn=Button(rootA, text='Exit',command=sys.exit)
        login_btn.grid(row=4, column=1)
        exit_btn.grid(row=4, column=2)
        rootA.mainloop()


    def CheckLogin():

        if username.get() == user1 and password.get() == password1: 
            rootA = Tk()
            rootA.title('Authentication Cehck')
            rootA.geometry('150x100') # Makes the window a certain size
            rlbl = Label(rootA, text='\n Logged In') 
            okay_btn=Button(rootA, text='Okay',command=LoginSuccessful)
            okay_btn.pack()
            #LoginSuccessful()

        else:
            r = Tk()
            r.title('Authentication Cehck')
            r.geometry('150x160')
            rlbl = Label(r, text='\n Invalid Login')
            rlbl.pack()
            okay_btn=Button(r, text='Try Again',command=r.destroy)
            okay_btn.pack()
            exit_btn=Button(r, text='Exit',command=sys.exit)
            exit_btn.pack()
            #r.mainloop()

    def LoginSuccessful ():
        rootA.destroy
        print("ANCD")


    def Insert_Rows():
        for rows in range (len(All_Users_Sheet)):
            if rows == 0: 
                rows +1 
                continue
            if All_Users_Sheet[rows][10].value == None:
                break
            else:
                print(All_Users_Sheet[rows][10].value)
                print(type(All_Users_Sheet[rows][10].value))



    Login_form()

Function()

Is there any way that after checking the username and password, if it's correct, close all tk windows by pressing a button and continue running the remaining tasks?

You should only create one instance of Tk . If you needed additional window, use Toplevel instead.

def CheckLogin():

    if username.get() == user1 and password.get() == password1:
        rootA = Toplevel()
        rootA.title('Authentication Check')
        ...

    else:
        r = Toplevel()
        ...

i got one solution.

from tkinter import *
global user1
user1 = 'testing'
password1 = '0000'

def combine_funcs(*funcs):
    def combined_func(*args, **kwargs):
        for f in funcs:
            f(*args, **kwargs)
    return combined_func


def Login_form():
    global username
    global password
    global rootA


    rootA = Tk()
    rootA.title('User Authentication')

    msgbox1 = Label(rootA, text='Please Login\n')
    msgbox1.grid(sticky=E)

    username = Label(rootA, text='Username: ')
    password = Label(rootA, text='Password: ')
    username.grid(row=1, sticky=W)
    password.grid(row=2, sticky=W)

    username = Entry(rootA)
    password = Entry(rootA, show='*')
    username.grid(row=1, column=1)
    password.grid(row=2, column=1)

    login_btn = Button(rootA, text='Login', command=CheckLogin)
    exit_btn=Button(rootA, text='Exit',command=rootA.destroy)
    login_btn.grid(row=4, column=1)
    exit_btn.grid(row=4, column=2)
    rootA.mainloop()


def CheckLogin():
    global rootA
    if username.get() == user1 and password.get() == password1:
        rootA = Tk()
        rootA.title('Authentication Cehck')
        rootA.geometry('150x100') # Makes the window a certain size
        rlbl = Label(rootA, text='\n Logged In')
        okay_btn=Button(rootA, text='Okay',command=LoginSuccessful)
        okay_btn.pack()
        #LoginSuccessful()

    else:
        r = Tk()
        r.title('Authentication Cehck')
        r.geometry('150x160')
        rlbl = Label(r, text='\n Invalid Login')
        rlbl.pack()
        okay_btn=Button(r, text='Try Again',command=r.destroy)
        okay_btn.pack()
        exit_btn=Button(r, text='Exit',command= combine_funcs(rootA.destroy, r.destroy))
        exit_btn.pack()
        #r.mainloop()

def LoginSuccessful ():
    rootA.destroy
    print("ANCD")


def Insert_Rows():
    for rows in range (len(All_Users_Sheet)):
        if rows == 0:
            rows +1
            continue
        if All_Users_Sheet[rows][10].value == None:
            break
        else:
            print(All_Users_Sheet[rows][10].value)
            print(type(All_Users_Sheet[rows][10].value))



Login_form()

please dont't ask about def combine_funcs(*funcs): def combined_func(*args, **kwargs): for f in funcs: f(*args, **kwargs) return combined_func ,it just solves the problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM