-3

How is it possible to check the entries for login and password and if they are correct, new tabs will open. If the entries are incorrect, an error message will be displayed. The user should also be able to register for an account

 from tkinter import *
import sqlite3
import tkinter
import time
root = Tk()

root.title("Sale System")
root.geometry("500x500")
root.configure(background='grey')

Label(root, text="Checkout System", background='grey').grid(row=0, column=1)
Label(root, text="Time:", background='grey').grid(row=1, column=1)

def clock():
    t=time.strftime('%I:%M:%S',time.localtime())
    if t!='':
        labeltime.config(text=t)
    root.after(100,clock)
labeltime=Label(root, text="Current System Time", background='grey')
labeltime.grid(row=1, column=1)
time1 = clock()

def new_winF(): #Opens registration confirmation window
    newwin = Toplevel(root)
    display = Label(newwin, text="Your account has been created. Please log in with your new details. This window may be closed.")
    display.pack()    


Label(root, text="Username:", background='grey').grid(row=3, column=0, sticky=W)
Label(root, text="Password:", background='grey').grid(row=4, column=0, sticky=W)

e1 = Entry(root)
e1.grid(row=3, column=1)


if e1 == 'yes':
    command =new_winF


e2 = Entry(root)
e2.grid(row=4, column=1)

LoginButton = Button(root, text="Login", width=10)
LoginButton.grid(row=5, column=1)

RegisterButton = Button(root, text ="Register", command =new_winF, width=10)
RegisterButton.grid(row=6, column=1)


root.mainloop()
  • put all functions before `root = Tk()` to make code more readable. Now I see only mess. – furas Dec 07 '17 at 15:39
  • BTW: to get text from `Entry` you have to use `e1.get()` but program doesn't wait till you put text in Entry (it is not `input()` which checks foe "Enter') and checking `if e1 == 'yes':` directly after `e1 = Entry(root)` makes no sense - `e1.get()` will gives empty string – furas Dec 07 '17 at 15:42
  • You've told us what you want, but haven't explained what help you need. What is your question? Questions of the form "why isn't my code working?" are off topic here, you need to ask something more specific. – Bryan Oakley Dec 07 '17 at 16:15

2 Answers2

0

There is too many changes to describe it.

BTW: I use Frame to group widgets and later I can show one frame (pack()/grid()) and hide another (pack_forget()/grid_forget()). There are more useful examples with this method.

import tkinter as tk
import time

# --- functions ---

def clock():
    txt = time.strftime('%I:%M:%S', time.localtime())
    label_time.config(text=txt)
    root.after(1000, clock)

def on_register(): #Opens registration confirmation window
    newwin = tk.Toplevel(root)
    display = tk.Label(newwin, text="\nYour account has been created.\nPlease log in with your new details.\n\nThis window may be closed.")
    display.pack()    
    tk.Button(newwin, text='Close', command=newwin.destroy).pack()

def on_login(): #Opens registration confirmation window
    newwin = tk.Toplevel(root)
    if e1.get() == 'admin' and e2.get() == '12345':
        display = tk.Label(newwin, text="\nYour are logged in.\n\nThis window may be closed.")
        display.pack()    

        main_page.pack_forget() # hide it
        other_page.pack() # show it
    else:
        display = tk.Label(newwin, text="\nWrong login or password.\n\nThis window may be closed.")
        display.pack()    
    tk.Button(newwin, text='Close', command=newwin.destroy).pack()

def create_main_page():
    global label_time
    global e1
    global e2

    frame = tk.Frame(root)
    #frame.pack() # don't show it

    tk.Label(frame, text="Checkout System", background='grey').grid(row=0, column=1)
    tk.Label(frame, text="Time:", background='grey').grid(row=1, column=1)

    label_time = tk.Label(frame, text="Current System Time", background='grey')
    label_time.grid(row=1, column=1)

    clock()

    tk.Label(frame, text="Username:", background='grey').grid(row=3, column=0, sticky='w')
    tk.Label(frame, text="Password:", background='grey').grid(row=4, column=0, sticky='w')

    e1 = tk.Entry(frame)
    e1.grid(row=3, column=1)

    e2 = tk.Entry(frame)
    e2.grid(row=4, column=1)

    login_button = tk.Button(frame, text="Login", width=10, command=on_login)
    login_button.grid(row=5, column=1)

    register_button = tk.Button(frame, text="Register", width=10, command=on_register)
    register_button.grid(row=6, column=1)

    return frame

def create_other_page():
    frame = tk.Frame(root)
    #frame.pack() # don't show it

    tk.Label(frame, text="You are on other page", background='grey').grid(row=0, column=1)

    return frame

# ---- main ---

root = tk.Tk()

root.title("Sale System")
root.geometry("500x500")
root.configure(background='grey')

main_page = create_main_page()
main_page.pack() # show it

other_page = create_other_page()
#other_page.pack() # don't show it yet

root.mainloop()
furas
  • 134,197
  • 12
  • 106
  • 148
0

create a function and bind it to login button to check your entry inputs.Here your Username entry should be yes the password no if it matches then the window will pop up, if not mesagebox will pop it to alert the user.

from tkinter import *
import sqlite3
from tkinter import messagebox
import tkinter
import time


root = Tk()

root.title("Sale System")
root.geometry("500x500")
root.configure(background='grey')

Label(root, text="Checkout System", background='grey').grid(row=0, column=1)
Label(root, text="Time:", background='grey').grid(row=1, column=1)

def clock():
    t=time.strftime('%I:%M:%S',time.localtime())
    if t!='':
        labeltime.config(text=t)
    root.after(100,clock)

labeltime=Label(root, text="Current System Time", background='grey')
labeltime.grid(row=1, column=1)
time1 = clock()



def new_winF(): #Opens registration confirmation window
    newwin = Toplevel(root)
    display = Label(newwin, text="Your account has been created. Please log in with your new details. This window may be closed.")
    display.pack()



 Label(root, text="Username:", background='grey').grid(row=3,column=0,sticky=W)
Label(root, text="Password:", background='grey').grid(row=4, column=0, 
sticky=W)



def login_fun():
    if e1.get() == "yes" and e2.get() == "no":
       new_winF()
    else:
        messagebox.showerror("error","try agian boy")




e1 = Entry(root)
e1.grid(row=3, column=1)

e2 = Entry(root)
e2.grid(row=4, column=1)

LoginButton = Button(root, text="Login", width=10, command=login_fun)
LoginButton.grid(row=5, column=1)

RegisterButton = Button(root, text ="Register", command =new_winF, width=10)
RegisterButton.grid(row=6, column=1)


root.mainloop()
AD WAN
  • 1,414
  • 2
  • 15
  • 28