Python Forum
TypeError: string indices must be integers, not 'str'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: string indices must be integers, not 'str'
#2
Ignoring the database for now, your program cannot work because there is nothing linking the dictionary fr and the string "fr". Contrary to your comment "# default value x = fr, en, es from for i in rows (line 32)" x does not equal fr, en or es. it equals 0, 1 or 2. That is fairly worthless information because there is also no way to get from 0, 1 or 2 to fr, en or es.

Instead of having separate dictionaries for each language, I would have a dictionary of languages.
languages = {
    "en": {
        "_title": "MyTitle",
        "_menu": "MyMenu",
    },
    "fr": {
        "_title": "MonTitre",
        "_menu": "MonMenu",
    },
    "es": {
        "_title": "MiTitulo",
        "_menu": "MiMenĂº",
    },
}
Now you can use "en" to get the translation dictionary for "en". I use this dictionary in the program below.
import tkinter as tk  # Do not use wildcard imports


languages = {
    "en": {
        "_title": "MyTitle",
        "_menu": "MyMenu",
    },
    "fr": {
        "_title": "MonTitre",
        "_menu": "MonMenu",
    },
    "es": {
        "_title": "MiTitulo",
        "_menu": "MiMenĂº",
    },
}


def language_changed(*_):
    global language_pack
    lang = language_selection.get()
    language_pack = languages[lang]
    root.title(language_pack["_title"])
    translations.set("\n".join([f"{a}: {b}" for a, b in language_pack.items()]))


language_options = list(languages)
language_pack = languages[language_options[0]]

root = tk.Tk()
language_selection = tk.StringVar(root, language_options[0])
language_selection.trace_add("write", language_changed)
tk.OptionMenu(root, language_selection, *language_options).pack(padx=100, pady=10)

translations = tk.StringVar(root, "")
tk.Label(root, textvariable=translations).pack(padx=100, pady=10)

language_changed()

root.mainloop()
I left the database stuff out because I don't have your database, and it is not related to your current issue. But if I did have a database that I could use to get my translation language choices, I would use it like this:
language_options = [row[0] for row in cursor.execute("SELECT * FROM langPack")]
language = StringVar(root, language_options[0])
w = OptionMenu(root, language, *language_options)
LEMA likes this post
Reply


Messages In This Thread
RE: TypeError: string indices must be integers, not 'str' - by deanhystad - Jun-12-2024, 06:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tuple indices must be integers or slices, not str cybertooth 16 12,622 Nov-02-2023, 01:20 PM
Last Post: brewer32
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,445 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,619 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,893 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 2,300 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,737 Dec-30-2022, 05:38 PM
Last Post: dee
  TypeError: string indices must be integers JonWayn 12 3,832 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 5,175 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,870 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  string indices must be integers when parsing Json ilknurg 3 6,887 Mar-10-2022, 11:02 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020