Jun-12-2024, 06:17 PM
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.
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)