Home > front end >  How do I automatically notify a user that they forgot to select an action?
How do I automatically notify a user that they forgot to select an action?

Time:11-09

I'm creating a login system, but I'm having a bug that I don't know how to handle, the situation is this: I want every user to be able to choose between using encryption and not using encryption. For example, a person has entered the correct login information, but the person has forgotten to select the message type, and when the person presses the Enter button, they receive an error that they forgot to select the message type. How do I implement this? Code below:

from tkinter import messagebox
from tkinter import *


window = Tk()

window.title('Login')
window.geometry('320x200')
window.resizable(True, True)

name = StringVar()
password = StringVar()



def crypt():     

    r = (lis.get(lis.curselection()))
    c = (lis.get(lis.curselection()))

    string_name = name.get()
    string_password = password.get()

    #r = (lis.get(lis.curselection()))
    #c = (lis.get(lis.curselection()))



    if string_name == 'John':
        if string_password == '6789':
            if r == 'Use encrypted':
                window.after(1000, lambda: window.destroy())
                
                print('Hello.')


    if string_name == 'John':
        if string_password == '6789':
            if r == 'Use decrypted':
                window.after(1000, lambda: window.destroy())

                print('Hello bro!')
            

            
    if string_name not in 'John':
        messagebox.showerror('Error', 'Error')
    elif string_password not in '6789':
        messagebox.showerror('Error', 'Error')

    elif r not in r:                                                        
        messagebox.showerror('Error', 'Oops, please crypt message')    #This Error

    elif string_name == 'John':
        messagebox.showerror('Error', 'Error')
    elif string_password == '6789':
        messagebox.showerror('Error', 'Error')
    
entry = Entry(window, textvariable=name, width=10)
entry.grid(column=1, pady=7, padx=4)

label = Label(window, text='Enter name: ')
label.grid(row=0, padx=1)

entry1 = Entry(window, textvariable=password, width=10, show='*')
entry1.grid(column=1, pady=7, padx=2)

label1 = Label(window, text='Enter password: ')
label1.grid(row=1, padx=1)

listbox = Listbox(window, selectmode=SINGLE, width=12, height=2)
listbox.grid(column=1, row=2, pady=7, padx=2)



r = ['Use encrypted']
c = ['Use decrypted']
lis = Listbox(window, selectmode=SINGLE, width=10, height=2)
lis.grid(column=1, row=2, pady=7, padx=2)
for i in r:
    lis.insert(END, i)  
for i in c:
    lis.insert(END, i)

label_crypto = Label(window, text='Encrypted/decrypted message: ', bg='black', fg='red')
label_crypto.grid(row=2)

button = Button(window, text='Enter', command=crypt)
button.grid(pady=30)


window.mainloop()

CodePudding user response:

As suggested in my comment, improving the names of your variables will better allow you to distinguish between them.

The below code, uses a try-catch block to detect that the user hasn't selected an item from the list box. Tkinter will throw an error if you try to get the selected item form a list when one hasn't been selected.

from tkinter import messagebox
from tkinter import *
import _tkinter


window = Tk()

window.title('Login')
window.geometry('320x200')
window.resizable(True, True)

name = StringVar()
password = StringVar()



def crypt():     

    try:
        user_encryption_selection = (encryption_listbox.get(encryption_listbox.curselection()))
    except _tkinter.TclError:
        messagebox.showerror('Error','User has not selected an encryption type')
        return
        

    string_name = name.get()
    string_password = password.get()

    if string_name == 'John':
        if string_password == '6789':
            if user_encryption_selection == 'Use decrypted':
                window.after(1000, lambda: window.destroy())

                print('Hello bro!')
        else:
            messagebox.showerror('Error', 'Error Password')
    else:
        messagebox.showerror('Error', 'Invalid Username')

    
entry = Entry(window, textvariable=name, width=10)
entry.grid(column=1, pady=7, padx=4)

label = Label(window, text='Enter name: ')
label.grid(row=0, padx=1)

entry1 = Entry(window, textvariable=password, width=10, show='*')
entry1.grid(column=1, pady=7, padx=2)

label1 = Label(window, text='Enter password: ')
label1.grid(row=1, padx=1)

listbox = Listbox(window, selectmode=SINGLE, width=12, height=2)
listbox.grid(column=1, row=2, pady=7, padx=2)



encryption_options = ['Use encrypted','Use decrypted']
encryption_listbox = Listbox(window, selectmode=SINGLE, width=10, height=2)
encryption_listbox.grid(column=1, row=2, pady=7, padx=2)
for i in encryption_options:
    encryption_listbox.insert(END, i)  


label_crypto = Label(window, text='Encrypted/decrypted message: ', bg='black', fg='red')
label_crypto.grid(row=2)

button = Button(window, text='Enter', command=crypt)
button.grid(pady=30)


window.mainloop()

I've also removed some un-necessary code. You should aim to only check the username/password/encryption values once rather than multiple times in separate if/elif/else conditions

  • Related