0

so I have looked around for days and tried various solutions to other people issues that seem the same as my own but unfortunately I have not made any progress. I am still very new with python, tkinter, and mysqli databases. What I trying to do is have an inventory program, it's just that simple. You can login and add, delete or simply view what is in the database. I can add and delete items just fine but I am having an issue pulling the data in the correct columns.

So far I have tried to pull each as a variable and assign it accordingly, tried to pull one row at a time, even switch to a new database but have gotten no correct results. If I use sqlite 3 it works fine so is it because mysql is on a server and not local? Any way, I'd like some advice to point me in the right direction so any help is much appreciated. This is what happens when I run the program Edit:

 tree.insert("", 1, "dirIso", text="ProductID")
for n, dirIso in enumerate(results,1):
    list_of_column_values = [list(_dict.values())[0] for _dict in dirIso]
    tree.insert('dirIso', n, text=list_of_column_values[0],
                values=list_of_column_values[1:])
cursor.close()
conn.close()

This is what I have done, I am now getting a 'str' object has no attribute 'values'. Do I need to change n? Or is it looking for the name inside of my database as values and not the columns?

Outcome I'm trying to get is for the table to display its respective data for each item.

3
  • Read this Answer, how to get dict into a Treeview. Commented Nov 8, 2018 at 16:28
  • I have tried the way you suggested in the answer but I am getting "'str' object has no attribute 'values'". I have changed a few things around to try and fix it but have not gotten too far. Am I missing something? Commented Nov 8, 2018 at 19:48
  • Edit your Question, remove ALL code, except the function whith the .insert(... . Show your last uses version" Commented Nov 8, 2018 at 19:56

1 Answer 1

0

Question: Treeview is placing mysql data into one column


Edit your Question, showing three rows of results data, if your results does not look like the following.

Assuming the following results == list of dict!

 results = [{'produkt_id': '1234', 'Name': 'John', 'Address': 'NewYork'},
            {'produkt_id': '5678', 'Name': 'Peter', 'Address': 'Boston'}
           ]

You didn't need Tree Heading, as you want a Tabelview

# Dict's are unordered, 
# therfore you need a list of fieldnames in your desired order
fieldnames = ['produkt_id', 'Name', 'Address']

# Loop results
for n, _dict in enumerate(results,1):
    # Create a list of values from this _dict
    _list = []
    for key in fieldnames:
        _list.append(_dict[key])

    # The first value goes to 'text'
    # All others goes to 'values'
    tree.insert('', 'end', n, text=_list[0], values=_list[1:])

Output:

1234  John     NewYork
5678  Peter    Boston
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.