Jun-12-2024, 01:08 PM
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
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()
