0

thank you in advance. I am very new to Tkinter, hope you understand. My question is, now I'm developing a software, a certain page of this software is to upload a csv file, and load the first 10 records of it. What I'm thinking is to use Tkinter Treeview to display them. However, I'm now having issue of displaying it. This is my function of uploading the file and display it.

`

def UploadAction(contentFrame, s_homeMainFrame, topFrame):
    filename = filedialog.askopenfilename(title="Select file", filetypes=(("CSV Files", "*.csv"),))
    #=== if user didnt choose a file after dialog appears =====
    if filename == "":
        print("")
    else:
        for widget in topFrame.winfo_children():
            widget.destroy()

    lbl_title = Label(topFrame, text="Upload Data", font=('arial', 20))
    lbl_title.pack(fill=X)

    for widget in contentFrame.winfo_children():
        widget.destroy()
    lbl_preview_data = Label(contentFrame, text="Preview Data:", font=('times new roman', 20))
    lbl_preview_data.pack(side='top')

    df = pd.read_csv(filename)
    print(df)


    resultTree = ttk.Treeview(contentFrame)

    df_col = df.columns.values
    resultTree['columns'] = (df_col)

    counter = len(df)

    for x in range(len(df_col)):
        resultTree.column(x, width=100)
        resultTree.heading(x, text=df_col[x])
        for i in range(counter):
            rowLabels = df.index.tolist()
            resultTree.insert('', 0, values=(df[df_col[x]][i]))


    resultTree.pack(side=TOP)
    btn_clean_data = Button(contentFrame, text="Clean Data", width=20, command="")
    btn_clean_data.pack(side='top', pady=30)

    Click_Save = partial(Save_Data, contentFrame, s_homeMainFrame, topFrame, filename)
    btn_save_data = Button(contentFrame, text="Save Data", width=20, command=Click_Save)
    btn_save_data.pack(side='top', pady=40)`

I do not have any error on this, however, the display field is displaying everything in reverse order except the header, and everyting is displayed in the first column.

As in something like this:

userid | email | username | password | role | status

Available

Available

Supermarket Staff

aaa

admin

aaa

admin_1

aaa@gmail.com

admin@gmail.com

2

1

But this is certainly not what I expect. What I expect is to display top 10 records nicely. As in something like this:

userid | email | username | password | role | status

1 | admin@gmail.com | admin_1 | admin | Admin | Available

2 | aaa@gmail.com | aaa | aaa | Supermarket Staff | Available

which is exactly the same as how the records are displayed in the csv file. I would like to know what is the problem here. Many Thanks!

zzzTeee
  • 53
  • 7
  • Does this answer your question? [How to iterate over rows in a DataFrame in Pandas?](https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas) – stovfl Jan 02 '20 at 11:24
  • @stovfl I'm sorry but this doesn't solve my question, my question is to iterate it by using tkinter treeview.Thank you – zzzTeee Jan 02 '20 at 11:33
  • ***"to iterate it by using tkinter treeview."***: Your `treeview` can only show correct records if you `.insert(...` correct records row by row from `pandas`. Garbled input results in garbled treeview. – stovfl Jan 02 '20 at 11:37
  • @stovfl Hi thanks for your answer, am I right to say that I can't use a for loop to iterate the top 10 elements from my csv file then display it use treeview? – zzzTeee Jan 02 '20 at 12:02
  • ***"am I right to say that I can't use a for loop"***: No, basicly your approach is ok. I think using `pandas` is overkill for such a simple task. – stovfl Jan 02 '20 at 12:06
  • @stovfl ok, I think I didn't make myself clear, what I list above is just an example, my csv file has over 10k rows of data, so I want to display first few records as a preview to users, and the csv file is not fixed, I may have different columns and rows as well as titles uploading. Thanks – zzzTeee Jan 02 '20 at 12:21
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/205226/discussion-between-stovfl-and-zexuan). – stovfl Jan 02 '20 at 12:23

0 Answers0