pythondictionarytkinteracronym

How to create custom dictionary output. Currently picks up the first letter and gives output


I'm trying to create a simple acronym definition GUI. I have it for the most part. My goal is to achieve two different types of outputs. If the user inputs A I want it to output all of the acronyms that start with A, and this happens. When I type for example ACL it is bringing back all of the acronyms again not just the ACL acronym. I suspect the error or non error may be coming from .join

from tkinter import *
import tkinter.messagebox


HEIGHT = 400
WIDTH = 700


root = Tk()
root.title("Testing")


a_canvas = Canvas(root, height=HEIGHT, width=WIDTH)
a_canvas.pack()


def clicked():
    entered = a_entry.get()
    a_textbox.delete(0.0, END)
    try:
        textin = ", ".join(loop_over_input(entered))
    except:
        textin = 'Sorry invalid information provided.\nResubmit with the correct information.\n'
    a_textbox.insert(0.0, textin)


def clr():
    a_entry.delete(0, 'end')
    a_textbox.delete(0.0, 'end')


# def gone():
#     tkinter.messagebox.showinfo("Goodbye", 'Come back soon.')
#     exit()


# Keep this is the master code
def loop_over_input(the_str=''):
    master_list = []
    for char in the_str:
        tmp_char = passwordConversion[char]
        master_list.append(tmp_char)
    print("Master Pass List: ", master_list)
    return master_list


def cont():
    tkinter.messagebox.showinfo("About",
                                '\n Wrote by Me \n Version 1.0 \n Python\Tkinter')


# Custom Dictionary
passwordConversion = {
    "A": "AWS - Amazon Web Services \nAmazon ES - Amazon Elasticsearch Service \nAMI - Amazon Machine Image \nAPI - Application Programming Interface \nAI - Artificial Intelligence \nACL - Access Control List \nARN - Amazon Resource Name \nAZ - Availability Zone \nASG - Auto Scaling Group \nAES - Advanced Encryption System \nADFS - Active Directory Federation Service",
    "AWS": "Amazon Web Services",
    "Amazon ES": "Amazon Elasticsearch Service",
    "AMI": "Amazon Machine Image",
    "API": "Application Programming Interface",
    "AI": "Artificial Intelligence",
    "ACL": "Access Control List",
    "ARN": "Amazon Resource Name",
    "AZ": "Availability Zone",
    "ASG": "Auto Scaling Group",
    "AES": "Advanced Encryption System",
    "ADFS": "Active Directory Federation Service",

}

# Creating the Frames
a_frame = Frame(root, bg='Red', bd=5)
a_frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n')

a_middle_frame = Frame(root, bg='White')
a_middle_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.25, anchor='n')


# Creating entry boxes
a_entry_var = StringVar()
a_entry = Entry(a_frame, font=20, textvariable=a_entry_var)
a_entry.place(relwidth=0.65, relheight=1)


# Create a text box
a_textbox = Text(a_middle_frame, font=12)
a_textbox.place(relwidth=1.00, relheight=1)

# Creating button
a_button = Button(a_frame, padx=2, pady=2, text='Get Defination', command=clicked, bg='Silver', font=('Calibri 11 bold'))
a_button.place(relx=0.7, relheight=1, relwidth=0.3)


a_buttons_frame = Frame(root)
a_buttons_frame.place(relx=0.4, rely=0.6)


a_button2 = Button(a_buttons_frame, padx=2, pady=2, text='Clear Fields', font=('Calibri 11 bold'), bg='Silver', command=clr)
a_button2.grid(column=0, row=0, padx=(20, 20))

# a_button3 = Button(a_buttons_frame, padx=2, pady=2, text='Exit', command=gone, bg='Silver', font=('none 18 bold'))
# a_button3.grid(column=1, row=0, padx=(20, 0))


a_label = Label(text="- For Me Only - \n- Contains information for me only", )
a_label.pack(side='bottom')

# Define Menu Items
my_menu = Menu(root)
root.config(menu=my_menu)

# Create Menu Items
file_menu = Menu(my_menu)
my_menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Exit", command=root.quit)

# Create another submenu Edit
edit_menu = Menu(my_menu)
my_menu.add_command(label="About", command=cont)

root.mainloop()

Solution

  • As for me you check it in wrong way in loop_over_input and this makes problem to display correct output.

    You should get keys from dictionary and check every key if it starts with user text - startswith(). And it resolves problem with wrong output.

    And then you don't need "A": "..." because it will find all items which start with "A".

    I would also use .upper() to get AWS when user write aws.

    def loop_over_input(the_str=''):
        master_list = []
        
        for key, value in passwordConversion.items():
            if key.upper().startswith(the_str.upper()):
                master_list.append(f'{key} - {value}')
    
        print("Master Pass List: ", master_list)
        return master_list
    

    Because master_list has list so I use "\n" instead of ", " in

    textin = "\n".join(loop_over_input(entered))
    

    enter image description here

    Full working code with better organized code.

    import tkinter as tk  # PEP8: `import *` is not preferred
    import tkinter.messagebox
    
    # --- constants --- (PEP8: UPPPER_CASE_NAMES)
    
    HEIGHT = 400
    WIDTH = 700
    
    # --- classes --- (PEP8: CamelCaseNames)
    
    # ... empty ...
    
    # --- functions --- (PEP8: lower_case_names)
    
    def clicked():
        entered = a_entry.get()
        a_textbox.delete(0.0, 'end')
        try:
            textin = "\n".join(loop_over_input(entered))
        except:
            textin = 'Sorry invalid information provided.\nResubmit with the correct information.\n'
        a_textbox.insert(0.0, textin)
    
    
    def clr():
        a_entry.delete(0, 'end')
        a_textbox.delete(0.0, 'end')
    
    
    # def gone():
    #     tkinter.messagebox.showinfo("Goodbye", 'Come back soon.')
    #     exit()
    
    
    # Keep this is the master code
    def loop_over_input(the_str=''):
        master_list = []
        
        for key, value in passwordConversion.items():
            if key.upper().startswith(the_str.upper()):
                master_list.append(f'{key} - {value}')
    
        print("Master Pass List: ", master_list)
        return master_list
    
    
    def cont():
        tkinter.messagebox.showinfo("About",
                                    '\n Wrote by Me \n Version 1.0 \n Python\Tkinter')
    
    # --- main --- (PEP8: lower_case_names)
    
    # - data -
    
    # Custom Dictionary
    passwordConversion = {
        #"A": "AWS - Amazon Web Services \nAmazon ES - Amazon Elasticsearch Service \nAMI - Amazon Machine Image \nAPI - Application Programming Interface \nAI - Artificial Intelligence \nACL - Access Control List \nARN - Amazon Resource Name \nAZ - Availability Zone \nASG - Auto Scaling Group \nAES - Advanced Encryption System \nADFS - Active Directory Federation Service",
        
        "AWS": "Amazon Web Services",
        "Amazon ES": "Amazon Elasticsearch Service",
        "AMI": "Amazon Machine Image",
        "API": "Application Programming Interface",
        "AI": "Artificial Intelligence",
        "ACL": "Access Control List",
        "ARN": "Amazon Resource Name",
        "AZ": "Availability Zone",
        "ASG": "Auto Scaling Group",
        "AES": "Advanced Encryption System",
        "ADFS": "Active Directory Federation Service",
    }
    
    # - code -
    
    root = tk.Tk()
    root.title("Testing")
    
    a_canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
    a_canvas.pack()
    
    
    # Creating the Frames
    a_frame = tk.Frame(root, bg='Red', bd=5)
    a_frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n')
    
    a_middle_frame = tk.Frame(root, bg='White')
    a_middle_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.25, anchor='n')
    
    
    # Creating entry boxes
    a_entry_var = tk.StringVar()
    a_entry = tk.Entry(a_frame, font=20, textvariable=a_entry_var)
    a_entry.place(relwidth=0.65, relheight=1)
    
    
    # Create a text box
    a_textbox = tk.Text(a_middle_frame, font=12)
    a_textbox.place(relwidth=1.00, relheight=1)
    
    # Creating button
    a_button = tk.Button(a_frame, padx=2, pady=2, text='Get Defination', command=clicked, bg='Silver', font=('Calibri 11 bold'))
    a_button.place(relx=0.7, relheight=1, relwidth=0.3)
    
    
    a_buttons_frame = tk.Frame(root)
    a_buttons_frame.place(relx=0.4, rely=0.6)
    
    
    a_button2 = tk.Button(a_buttons_frame, padx=2, pady=2, text='Clear Fields', font=('Calibri 11 bold'), bg='Silver', command=clr)
    a_button2.grid(column=0, row=0, padx=(20, 20))
    
    # a_button3 = tk.Button(a_buttons_frame, padx=2, pady=2, text='Exit', command=gone, bg='Silver', font=('none 18 bold'))
    # a_button3.grid(column=1, row=0, padx=(20, 0))
    
    
    a_label = tk.Label(text="- For Me Only - \n- Contains information for me only", )
    a_label.pack(side='bottom')
    
    # Define Menu Items
    my_menu = tk.Menu(root)
    root.config(menu=my_menu)
    
    # Create Menu Items
    file_menu = tk.Menu(my_menu)
    my_menu.add_cascade(label="File", menu=file_menu)
    file_menu.add_command(label="Exit", command=root.quit)
    
    # Create another submenu Edit
    edit_menu = tk.Menu(my_menu)
    my_menu.add_command(label="About", command=cont)
    
    root.mainloop()
    

    PEP 8 -- Style Guide for Python Code


    EDIT:

    I would use regex to use more complex search, ie. I$ to get strings with I at the end.

    Eventually I would use fnmatch to search with star - '*I' or 'A*S'


    Code with regex

    import re
    
    def loop_over_input(the_str=''):
        master_list = []
        
        for key, value in passwordConversion.items():
            #if re.match(the_str, key, re.I):  # check at the beginning of string
            if re.search(the_str, key, re.I):  # check in any place of string
                master_list.append(f'{key} - {value}')
    
        print("Master Pass List: ", master_list)
        return master_list
    

    All keys which end with S

    enter image description here