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

Python - Import CSV File To Tkinter Table

Cracker

Layer 2 Pro
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
118
Likes
122
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial we will create Import CSV File To Tkinter Table Using Python. Python has a design philosophy which emphasizes code readability. Python is very easy to learn the syntax emphasizes readability and it can reduces time consuming in developing. Beginners find Python a clean syntax and indentation structure based, and it is easy to learn because of its less semi colon problem. 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

    *
  2. import

    tkinter.ttk

    as

    ttk
  3. import

    csv

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. root =

    Tk(

    )
  2. root.title

    (

    "Python - Import CSV File To Tkinter Table"

    )
  3. width =

    500
  4. height =

    400
  5. screen_width =

    root.winfo_screenwidth

    (

    )
  6. screen_height =

    root.winfo_screenheight

    (

    )
  7. x =

    (

    screen_width/2

    )

    - (

    width/2

    )
  8. y =

    (

    screen_height/2

    )

    - (

    height/2

    )
  9. root.geometry

    (

    "%dx%d+%d+%d"

    % (

    width,

    height,

    x,

    y)

    )
  10. root.resizable

    (

    0

    ,

    0

    )

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. TableMargin =

    Frame(

    root,

    width=

    500

    )
  2. TableMargin.pack

    (

    side=

    TOP)
  3. scrollbarx =

    Scrollbar(

    TableMargin,

    orient=

    HORIZONTAL)
  4. scrollbary =

    Scrollbar(

    TableMargin,

    orient=

    VERTICAL)
  5. tree =

    ttk.Treeview

    (

    TableMargin,

    columns=

    (

    "Firstname"

    ,

    "Lastname"

    ,

    "Address"

    )

    ,

    height=

    400

    ,

    selectmode=

    "extended"

    ,

    yscrollcommand=

    scrollbary.set

    ,

    xscrollcommand=

    scrollbarx.set

    )
  6. scrollbary.config

    (

    command=

    tree.yview

    )
  7. scrollbary.pack

    (

    side=

    RIGHT,

    fill=

    Y)
  8. scrollbarx.config

    (

    command=

    tree.xview

    )
  9. scrollbarx.pack

    (

    side=

    BOTTOM,

    fill=

    X)
  10. tree.heading

    (

    'Firstname'

    ,

    text=

    "Firstname"

    ,

    anchor=

    W)
  11. tree.heading

    (

    'Lastname'

    ,

    text=

    "Lastname"

    ,

    anchor=

    W)
  12. tree.heading

    (

    'Address'

    ,

    text=

    "Address"

    ,

    anchor=

    W)
  13. tree.column

    (

    '#0'

    ,

    stretch=

    NO,

    minwidth=

    0

    ,

    width=

    0

    )
  14. tree.column

    (

    '#1'

    ,

    stretch=

    NO,

    minwidth=

    0

    ,

    width=

    200

    )
  15. tree.column

    (

    '#2'

    ,

    stretch=

    NO,

    minwidth=

    0

    ,

    width=

    200

    )
  16. tree.column

    (

    '#3'

    ,

    stretch=

    NO,

    minwidth=

    0

    ,

    width=

    300

    )
  17. tree.pack

    (

    )

Creating the Main Function

This is where the code that contains the main functions. This code will read the csv file then display all the data to the Tkinter TreeView. To do that just copy and write these blocks of code.
  1. with

    open

    (

    'test.csv'

    )

    as

    f:
  2. reader =

    csv

    .DictReader

    (

    f,

    delimiter=

    ','

    )
  3. for

    row in

    reader:
  4. firstname =

    row[

    'firstname'

    ]
  5. lastname =

    row[

    'lastname'

    ]
  6. address =

    row[

    'address'

    ]
  7. tree.insert

    (

    ""

    ,

    0

    ,

    values=

    (

    firstname,

    lastname,

    address)

    )

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 Import CSV File To Tkinter Table 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