-2

Below is PySimpleGUI code to create a multilanguage dictionary where I can add entries for each word in different languages, and also have the possibility to search by language. Unfortunately, I got the following error:

AttributeError: 'list' object has no attribute 'tree_dict'

How can I fix this?

import sqlite3
import PySimpleGUI as sg

# Create connection to SQLite database
conn = sqlite3.connect('Data_Enteries.db')
c = conn.cursor()

# Create table if it doesn't exist
c.execute('''CREATE TABLE IF NOT EXISTS dictionary
             (id INTEGER PRIMARY KEY,
              English TEXT,
              French TEXT,
              Spanish TEXT,
              Chinese TEXT,
              Swedish TEXT,
              Turkish TEXT)''')
conn.commit()

# Create PySimpleGUI layout
layout = [[sg.Text('Enter word to search: '), sg.InputText(key='search'), 
           sg.Button('Search')],
          [sg.Tree(data=[], headings=['English', 'French', 'Spanish', 'Chinese', 'Swedish', 
            'Turkish'], key='treeview', col_widths=[20]*6)]]

# Create PySimpleGUI window
window = sg.Window('Multilanguage Dictionary', layout)

# Function to retrieve data from database
def search_database(word):
    # Execute SELECT query
    c.execute('''SELECT * FROM dictionary
                 WHERE English = ? OR
                       French = ? OR
                       Spanish = ? OR
                       Chinese = ? OR
                       Swedish = ? OR
                       Turkish = ?''', (word, word, word, word, word, word))
    results = c.fetchall()
    # Convert results to PySimpleGUI treeview data format
    data = []
    for row in results:
        data.append([row[1], row[2], row[3], row[4], row[5], row[6]])
    return data

# Loop to handle PySimpleGUI events
while True:
    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    if event == 'Search':
        search_word = values['search']
        tree_data = search_database(search_word)
        window['treeview'].update(values=tree_data)

# Close PySimpleGUI window and SQLite database connection
conn.close()
window.close()

Traceback:

C:\UsersUserName\Desktop>MultiLangPSG.py
Traceback (most recent call last):
  File "C:\UsersUserName\Desktop\MultiLangPSG.py", line 47, in <module>
    event, values = window.read()
                    ^^^^^^^^^^^^^
  File "C:\UsersUserName\AppData\Local\Programs\Python\Python311\Lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 10075, in read
    results = self._read(timeout=timeout, timeout_key=timeout_key)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\UsersUserName\AppData\Local\Programs\Python\Python311\Lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 10146, in _read
    self._Show()
  File "C:\UsersUserName\AppData\Local\Programs\Python\Python311\Lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 9886, in _Show
    StartupTK(self)
  File "C:\UsersUserName\AppData\Local\Programs\Python\Python311\Lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 16866, in StartupTK
    _convert_window_to_tk(window)
  File "C:\UsersUserName\AppData\Local\Programs\Python\Python311\Lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 16753, in _convert_window_to_tk
    PackFormIntoFrame(window, master, window)
  File "C:\UsersUserName\AppData\Local\Programs\Python\Python311\Lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 16402, in PackFormIntoFrame
    for key, node in element.TreeData.tree_dict.items():
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'tree_dict'
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 1
    I suggest you post a [mre] and the complete stack trace of the exception. – khelwood Apr 14 '23 at 11:13
  • @khelwood, Thanks for prompt feedback, an example is shared in my above question. – PyiPath2022 Apr 14 '23 at 11:17
  • @khelwood, anything to add from my side? Thx – PyiPath2022 Apr 14 '23 at 11:27
  • @PyiPath2022 can you provide the traceback as requested by khelwood? We kinda need it... it doesn't' appear you actually have something calling `tree_dict` in the code snippet, so it might be something to do with `PySimpleGUI`. Eitherways, we need the traceback, the full thing. – Anony Mous Apr 14 '23 at 11:30
  • @AnonyMous .Updated above please check – PyiPath2022 Apr 14 '23 at 11:35
  • @PyiPath2022 ok... sorry to bother you again, but can you edit that but surround the traceback with triple backquotes (like ` character)? That being said, I think the issue is with the module itself, not with your code. Maybe try reinstalling the module. – Anony Mous Apr 14 '23 at 11:46
  • @AnonyMous, no issue. Done – PyiPath2022 Apr 14 '23 at 11:48
  • @PyiPath2022 Ok so I think the issue might be related to your input (maybe line 26? or 20?) or it would otherwise be the module's fault. I'm not going to pretend I know this module, but maybe your layout is incorrect? – Anony Mous Apr 14 '23 at 11:51
  • layout should be OK – PyiPath2022 Apr 14 '23 at 11:54

1 Answers1

0

Argument data in sg.Tree

The data represented using a PySimpleGUI provided TreeData class

type data: (TreeData)

Demo Code

treedata = sg.TreeData()
layout = [
    [sg.Text('Enter word to search: '),
     sg.InputText(key='search'),
     sg.Button('Search')],
    [sg.Tree(data=treedata, headings=['English', 'French', 'Spanish', 'Chinese', 'Swedish','Turkish'], key='treeview', col_widths=[20]*6)],
]
Jason Yang
  • 11,284
  • 2
  • 9
  • 23
  • Excellent!!! How to add "Add New" "Delete" and "update" ? – PyiPath2022 Apr 14 '23 at 12:51
  • Seems the layout is OK , but not possible to retrieve data from database using search field – PyiPath2022 Apr 14 '23 at 13:26
  • Again, the type for the option `values` of method `update` is `sg.TreeData`, not a list. Check `tree_data = search_database(search_word)`, the function `search_database` return a list. There's no method provided to Inset/Delete/Update for items in the `Tree`, you need to check the structure of `sg.TreeData` for your requirements, or more tkinter knowledge to know how they work. – Jason Yang Apr 14 '23 at 13:37