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'
#1
Hi,
I'm learning python and tkinter and trying to create a multilingual project.
Everything works fine except the variable <a> which must fetch a language code (en, es, fr) in my database.
This <a> variable must be passed to the <pack> variable (pack=a) to translate pack["_title"] and pack["_menu"] from a Dictionary
Here is my problem:
- When I write <pack = es> (for Spanish) everything works fine and root.title(pack["_title"]) = MiTitulo
- But when I write <pack = a>, I get this error:
line 53, in <module>
root.title(pack["_title"])
TypeError: string indices must be integers, not 'str'
However, when I print <print(a)>, the output indicates <es>
THANKS

from tkinter import *
import sqlite3

root = Tk()

#-------------------------------------------> Dictionary pack lang
en = {
    "_title": "MyTitle",
    "_menu": "MyMenu",
}
fr = {
    "_title": "MonTitre",
    "_menu": "MonMenu",
}
es = {
    "_title": "MiTitulo",
    "_menu": "MiMenĂº",
}

#-------------------------------------------> connection database
pack = []
conn = sqlite3.connect('dataBase.db')
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS langPack (lang TEXT)")
rows = cursor.fetchall()
conn.commit()

rows = cursor.execute("SELECT * FROM langPack").fetchall()

for i in rows:
    a = i[0]
    print(a) # = es
    if a == "fr": x = 0
    if a == "en": x = 1
    if a == "es": x = 2
    
pack = fr # default value x = fr, en, es from database
# pack = a # not working

OPTIONS = ["fr", "en", "es"]

variable = StringVar(root)
variable.set(OPTIONS[x])  # default value x = fr, en, es from for i in rows (line 32)

w = OptionMenu(root, variable, *OPTIONS)
w.pack()

def ok():
    lang = variable.get()
    # delete all = delete previous language
    query = "DELETE FROM langPack"
    cursor.execute(query)
    conn.commit()
    # insert new choice of language
    query = "INSERT INTO langPack (lang) VALUES (?)"
    cursor.execute(query, (lang,))
    conn.commit()
#-------------------------------------------> end data base

button = Button(root, text="OK", command=ok)
button.pack(ipadx=50, ipady=30)

root.title(pack["_title"])

menuButton = Button(root, text=pack["_menu"])
menuButton.place(x=0, y=0)
menuButton.pack(ipadx=50, ipady=30)

root.mainloop()
Reply


Messages In This Thread
TypeError: string indices must be integers, not 'str' - by LEMA - Jun-12-2024, 01:08 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tuple indices must be integers or slices, not str cybertooth 16 12,594 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,441 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,605 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,875 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,290 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,728 Dec-30-2022, 05:38 PM
Last Post: dee
  TypeError: string indices must be integers JonWayn 12 3,809 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 5,161 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,863 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  string indices must be integers when parsing Json ilknurg 3 6,867 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