0

i creating simple login form using python.if the username and password correct it should redirect to the main form. but i don't how to call the second form. i have two pages how to call the main.py i attached code below what i tried so far

Login.py

from tkinter import *
from tkinter import messagebox
        
        
def Ok():
     uname = e1.get()
     password = e2.get()
        
     if(uname == "" and password == "") :
           messagebox.showinfo("", "Blank Not allowed")
        
        
     elif(uname == "Admin" and password == "123"):        
           messagebox.showinfo("","Login Success")
           root.destroy()
        
     else :
           messagebox.showinfo("","Incorrent Username and Password")
root = Tk()
root.title("Login")
root.geometry("300x200")
global e1
global e2
        
Label(root, text="UserName").place(x=10, y=10)
Label(root, text="Password").place(x=10, y=40)
        
e1 = Entry(root)
e1.place(x=140, y=10)
        
e2 = Entry(root)
e2.place(x=140, y=40)
e2.config(show="*")
        
        
Button(root, text="Login", command=Ok ,height = 3, width = 13).place(x=10, y=100)
        
root.mainloop()

Main.py

from tkinter import *
from tkinter import messagebox

root = Tk()
root.title("Main")
root.geometry("500x500")
global e1
global e2

Label(root, text="Welcome").place(x=10, y=10)

root.mainloop()
Karthik
  • 2,181
  • 4
  • 10
  • 28
tuts fun
  • 117
  • 1
  • 11

2 Answers2

1

the Login.py

import os
from tkinter import *
from tkinter import messagebox

global e1
global e2

root = Tk()
root.title("Login")
root.geometry("300x200")

def Ok():
    global e1, e2
    uname = e1.get()
    password = e2.get()

    if(uname == "" and password == "") :
        messagebox.showinfo("", "Blank Not allowed")
        return False


    elif(uname == "Admin" and password == "123"):

        messagebox.showinfo("","Login Success")
        os.system("python3 main.py")
        root.destroy()

        return True

    else :
        messagebox.showinfo("","Incorrent Username and Password")
        return False

Label(root, text="UserName").place(x=10, y=10)
Label(root, text="Password").place(x=10, y=40)
Button(root, text="Login", command=Ok,height = 3, width = 13).place(x=10, y=100)

while True:


    global e1, e2
    e1 = Entry(root)
    e1.place(x=140, y=10)

    e2 = Entry(root)
    e2.place(x=140, y=40)
    e2.config(show="*")

    if Ok():
        os.system("python3 main_kinter.py")
        break

    root.mainloop()

` and the main file :

from tkinter import *
from tkinter import messagebox

root = Tk()
root.title("Main")
root.geometry("500x500")
global e1
global e2

Label(root, text="Welcome").place(x=10, y=10)

root.mainloop()
sadbro
  • 112
  • 7
1

Here is an answer on one of the ways on how to proceed. By importing the file onto another file.

main.py:

from tkinter import *
from tkinter import messagebox
import login

login.everything()

root = Tk()
root.title("Main")
root.geometry("500x500")

Label(root, text="Welcome").place(x=10, y=10)

root.mainloop()

login.py:

from tkinter import *
from tkinter import messagebox

def everything():
    def Ok():
        uname = e1.get()
        password = e2.get()

        if(uname == "" and password == "") :
            messagebox.showinfo("", "Blank Not allowed")


        elif(uname == "Admin" and password == "123"):

            messagebox.showinfo("","Login Success")
            top.destroy()
            main.destroy()

        else :
            messagebox.showinfo("","Incorrent Username and Password")
    main = Tk()
    main.withdraw()
    top = Toplevel()
    top.title("Login")
    top.geometry("300x200")

    Label(top, text="UserName").place(x=10, y=10)
    Label(top, text="Password").place(x=10, y=40)

    e1 = Entry(top)
    e1.place(x=140, y=10)

    e2 = Entry(top)
    e2.place(x=140, y=40)
    e2.config(show="*")


    Button(top, text="Login", command=Ok ,height = 3, width = 13).place(x=10, y=100)
    top.mainloop()

Hope you got an idea, do let me know if any errors or doubts.

Cheers

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46