• We just launched and are currently in beta. Join us as we build and grow the community.

Python - Simple Adding Value In Dictionary

dashmahmood

Shoujo Manga Narrator
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
66
Likes
177
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. #=================================FRAMES==========================================
  13. Top =

    Frame(

    root,

    relief=

    SOLID,

    bd=

    1

    )
  14. Top.pack

    (

    fill=

    X)
  15. Mid =

    Frame(

    root)
  16. Mid.pack

    (

    pady=

    10

    )
  17. Left =

    Frame(

    Mid)
  18. Left.pack

    (

    side=

    LEFT)
  19. Right =

    Frame(

    Mid)
  20. Right.pack

    (

    side=

    RIGHT)
  21. Bottom =

    Frame(

    root)
  22. Bottom.pack

    (

    )
  23. Below =

    Frame(

    root)
  24. 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. #=================================LABEL WIDGETS===================================
  5. lbl_title =

    Label(

    Top,

    text=

    "Python - Simple Adding Value In Dictionary"

    ,

    font=

    (

    'calibri'

    ,

    16

    )

    )
  6. lbl_title.pack

    (

    )
  7. lbl_text =

    Label(

    Left,

    text=

    "Enter some text"

    ,

    font=

    (

    'calibri'

    ,

    14

    )

    )
  8. lbl_text.pack

    (

    )
  9. lbl_list =

    Label(

    Bottom,

    text=

    "List of Dictionary"

    ,

    font=

    (

    'calibri'

    ,

    14

    )

    )
  10. lbl_list.pack

    (

    )
  11. lbl_status =

    Label(

    Bottom,

    font=

    (

    'calibri'

    ,

    14

    )

    )
  12. lbl_status.pack

    (

    )

  13. #=================================ENTRY WIDGET====================================
  14. txt_text =

    Entry(

    Right,

    font=

    (

    'calibri'

    ,

    14

    )

    ,

    textvariable=

    NAME)
  15. 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. def

    Dictionary(

    )

    :
  3. global

    data
  4. dictionary =

    [
  5. {

    "name"

    : "Naruto"

    }

    ,
  6. {

    "name"

    : "Sasuke"

    }

    ,
  7. {

    "name"

    : "Sakura"

    }
  8. ]

  9. data =

    dictionary

  10. for

    member in

    data:
  11. temp_text =

    '{0}'

    .format

    (

    member[

    'name'

    ]

    )
  12. Label(

    Below,

    text=

    temp_text)

    .pack

    (

    anchor=

    CENTER)

  13. def

    Add(

    )

    :
  14. if

    NAME.get

    (

    )

    ==

    ""

    :
  15. lbl_status.config

    (

    text=

    "Please enter something"

    ,

    fg=

    "Red"

    )
  16. else

    :
  17. lbl_status.config

    (

    text=

    ""

    )
  18. data.append

    (

    {

    "name"

    : "%s"

    % (

    NAME.get

    (

    )

    )

    }

    )
  19. for

    widget in

    Below.winfo_children

    (

    )

    :
  20. widget.destroy

    (

    )

  21. for

    member in

    data:
  22. temp_text =

    '{0}'

    .format

    (

    member[

    'name'

    ]

    )
  23. Label(

    Below,

    text=

    temp_text)

    .pack

    (

    anchor=

    CENTER)
  24. NAME.set

    (

    ''

    )

  25. 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!!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top