Python - Simple Adding Value In Dictionary

In this tutorial we will create a Simple Adding Value In Dictionary using Python. Python has a design philosophy which emphasizes code readability. That's why python is very easy to use especially for beginners who just started programming. It is very easy to learn the syntax emphasizes readability and it can reduces time consuming in developing. So let's now do the coding...

Getting started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Importing Modules

After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python. Then copy code that I provided below and paste it inside the IDLE text editor.
  1. from tkinter import *

Setting up the Main Frame

After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.
  1. #==================================MAIN FRAME=====================================
  2. root=Tk()
  3. root.title("Python - Simple Adding Value In Dictionary")
  4. width=450
  5. height=300
  6. screen_width=root.winfo_screenwidth()
  7. screen_height=root.winfo_screenheight()
  8. x=(screen_width/2)-(width/2)
  9. y=(screen_height/2)-(height/2)
  10. root.geometry("%dx%d+%d+%d" % (width, height, x, y))
  11. root.resizable(0, 0)
  12.  
  13.  
  14. #=================================FRAMES==========================================
  15. Top = Frame(root, relief=SOLID, bd=1)
  16. Top.pack(fill=X)
  17. Mid = Frame(root)
  18. Mid.pack(pady=10)
  19. Left = Frame(Mid)
  20. Left.pack(side=LEFT)
  21. Right = Frame(Mid)
  22. Right.pack(side=RIGHT)
  23. Bottom = Frame(root)
  24. Bottom.pack()
  25. Below = Frame(root)
  26. Below.pack()

Assigning Variables

This is where the we will assign the variables. This code will assign each of the variables to be use later in the application.
  1. #=================================VARIABLES=======================================
  2. NAME = StringVar()

Designing the Layout

After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.
  1. #=================================BUTTON WIDGETS==================================
  2. btn_add = Button(Bottom, text="ADD", command=Add)
  3. btn_add.pack()
  4.  
  5. #=================================LABEL WIDGETS===================================
  6. lbl_title = Label(Top, text="Python - Simple Adding Value In Dictionary", font=('calibri', 16))
  7. lbl_title.pack()
  8. lbl_text = Label(Left, text="Enter some text", font=('calibri', 14))
  9. lbl_text.pack()
  10. lbl_list = Label(Bottom, text="List of Dictionary", font=('calibri', 14))
  11. lbl_list.pack()
  12. lbl_status = Label(Bottom,  font=('calibri', 14))
  13. lbl_status.pack()
  14.  
  15. #=================================ENTRY WIDGET====================================
  16. txt_text = Entry(Right, font=('calibri', 14), textvariable=NAME)
  17. txt_text.pack()

Creating the Main Function

This is where the code that contains the main functions. This code will add a new entry to the dictionary when user input some data. To do that just copy and write these blocks of code.
  1. #=================================METHODS=========================================
  2.  
  3. def Dictionary():
  4.     global data
  5.     dictionary = [
  6.         {"name": "Naruto"},
  7.         {"name": "Sasuke"},
  8.         {"name": "Sakura"}
  9.     ]
  10.  
  11.     data = dictionary
  12.  
  13.     for member in data:
  14.         temp_text = '{0}'.format(member['name'])
  15.         Label(Below, text=temp_text).pack(anchor=CENTER)
  16.  
  17. def Add():
  18.     if NAME.get() == "":
  19.         lbl_status.config(text="Please enter something", fg="Red")
  20.     else:
  21.         lbl_status.config(text="")
  22.         data.append({"name": "%s" % (NAME.get())})
  23.         for widget in Below.winfo_children():
  24.           widget.destroy()
  25.  
  26.         for member in data:
  27.             temp_text = '{0}'.format(member['name'])
  28.             Label(Below, text=temp_text).pack(anchor=CENTER)
  29.         NAME.set('')
  30.  
  31. Dictionary()

Initializing the Application

After finishing the function save the application as index.py. This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.
  1. #=================================INITIALIZATION==================================
  2. if __name__ == '__main__':
  3.     root.mainloop()
There you have it we just created a Simple Adding Value In Dictionary Using Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Add new comment