0

This script opens the first login page. If I enter the correct username and password another window should be opened and close the login window. My issue that if I closed the first window, the second window appears. How can I get over this? I need when I close the first window, the script should terminate.

##libraries
from tkinter import *
import paramiko
import time
from tkinter.ttk import Combobox



global win
LARGE_FONT= ("Verdana", 12)
NORM_FONT = ("Helvetica", 10)
SMALL_FONT = ("Helvetica", 8)
## def to check username and password then start the next loop
def done():
    global username1
    global password1
    if webui_username_Entry.get() == "alcatel" and webui_password_Entry.get() == "Nokia@2016" :
        root.quit()
        username1 = webui_username_Entry.get()
        password1 = webui_password_Entry.get()
        root.destroy()
    else:
        popupmsg("Wrong Username or password")

import tkinter as tk
from tkinter import ttk
def popupmsg(msg):
    popup = tk.Tk()
    popup.wm_title("Error")
    label = ttk.Label(popup, text=msg, font=NORM_FONT)
    label.pack(side="top", fill="x", pady=10)
    B1 = ttk.Button(popup, text="Try Again", command = popup.destroy)
    B1.pack()
    popup.mainloop()


class MyWindow():
    global root
    import tkinter as tk
    root = tk.Tk()

    image = r"D:\Python\My Projects\Nokia1.png"

    icon = PhotoImage(file=image)
    root.call('wm', 'iconphoto', root._w, icon)



    canvas1 = tk.Canvas(root, width=400, height=300)
    canvas1.pack()

    global webui_username_Entry
    global webui_password_Entry
    image = tk.PhotoImage(file=r"D:\Python\My Projects\Nokia.png")  # Use self.image
    canvas1.create_image(50, 10, image=image, anchor=tk.NW)

    webui_username_Entry = tk.Entry(root)
    canvas1.create_window(250, 150, window=webui_username_Entry)
    webui_username = tk.Label(root, text="Enter Username")
    canvas1.create_window(125, 150, window=webui_username)

    webui_password_Entry = tk.Entry(root,show="*")
    canvas1.create_window(250, 200, window=webui_password_Entry)
    webui_password = tk.Label(root, text="Enter Password")
    canvas1.create_window(125, 200, window=webui_password)

    result = "result"
    button1 = tk.Button(text='Login', command=done, bg='black', fg='white', font=('helvetica', 9, 'bold'))
    canvas1.create_window(200, 250, window=button1)

    root.title('Nokia Login')
    root.mainloop()

    def __init__(self,win):
        global cb
        global profile_unknown
        global profile_Voda
        global profile_All
        global outbox


        self.new_username = Label(win, text='New Username')
        self.new_password = Label(win, text='New Password')
        self.profile = Label(win, text='Profile Domain')

        self.result = Label(win, text='Result')
        global webui_username
        global webui_Password
        webui_username = self.t1 = Entry(bd=3)
        webui_Password = self.t2 = Entry()
        outbox = self.outbox_E = Entry()

        self.btn2 = Button(win, text='Create')
        self.new_username.place(x=100, y=50)
        self.t1.place(x=200, y=50)
        self.new_password.place(x=100, y=100)
        self.t2.place(x=200, y=100)
        self.profile.place(x=100, y=150)

        # self.b1=Button(win, text='Add', command=self.add)
        self.b2 = Button(win, text='Create', command=auth)
        self.b2.place(x=200, y=200)
        # self.b2.bind('<Button-1>', self.sub)
        # self.b1.place(x=100, y=150)

        self.result.place(x=100, y=240)
        self.outbox_E.place(x=200, y=240, width=200, height=40)

        # Droplist
        global data
        global cb
        profile_unknown = " unknown"
        profile_Voda = " Voda"
        profile_All = ' "Voda","unknown"'
        data = ("Voda", "unknown", 'All Network')
        cb = Combobox(window, values=data)
        cb.place(x=200, y=150)

def auth():
    ## App Athuntication.
    ##inputs/variable
    global ipa
    ipa = "xx.xx.xx.xx"
    # webui_user = str(input("Enter username                    :"))
    # webui_pass = str(input("Enter password                    :"))
    y = "yes"
    n = "no"
    global ssh
    ## here to try the first connection
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(ipa, 22, 'root', 'Nokia@2016')
        shell = ssh.invoke_shell()
        print("Auth Success")


    except paramiko.AuthenticationException:
        print(" \nWrong username or Password, Please try again\n")
        ## while this error how please do the authi() function.

    except  TimeoutError:
        print("\nIP is not reachable. please check connection\n")
        ## while this error show please do the authi() function.

    if cb.get() == "unknown":
        profile_d = profile_unknown
    elif cb.get() == "Voda":
        profile_d = profile_Voda
    elif cb.get() == 'All Network':
        profile_d = profile_All

window = Tk()
mywin = MyWindow(window)
image = r"D:\Python\My Projects\Nokia2.png"
icon = PhotoImage(file=image)
window.call('wm', 'iconphoto', window._w, icon)
window.title('Vendor tool')
window.geometry("500x400+10+10")
window.mainloop()
quamrana
  • 37,849
  • 12
  • 53
  • 71

1 Answers1

0

The solution to your question is simple. You just need to add logic to it

After checking whether the user has given the correct username or password, you can create a window using the Tk() class:

windowName = Tk()

Then making the variable name of the older window global you can close it using the destroy method.

Note: Creating a global variable is only essential when you are trying to put the code that creates the new window in a function. If you are intersted in implementing the code in the if statement itself, you do not neccessarily need to do that.

Pranav Krishna S
  • 324
  • 2
  • 13