0

So, I want the user to be able to use my program in English. They should be able to switch to their language via a dropdown-menu. My program has only four pages and a few labels on each, translating them with a toolkit would be unnecessary.

This is, what I got this so far:

    # Language Settings
    def display_selected(choice):
        choice = variable.get()
        if choice == "Deutsch":
            languagelabel.config(text=str('Sprache:'))
            Page1(self).label_search.config(text=str('Suche'))

        else:
            languagelabel.config(text=str('Language:'))
            Page1(self).label_search.config(text=str('search'))


    # Dropdown Menu for Languages
    languages = ['Deutsch', 'English']
    variable = StringVar()
    variable.set(languages[0])
    dropdown = OptionMenu(self, variable, *languages, command=display_selected)

The languagelabel in the same class changes, but the search_label in the class 'Page1' doesn't. Nothing happens, am I missing something?

I'm a bit of a novice, so any guidance and/or other solutions would be appreciated!

Tom Orrow
  • 3
  • 2

1 Answers1

0

One option I can think of would be using StringVar()s to store the labels that need translation for each language option, and the assigning those variables to each label's textvariable parameter

# EXAMPLE
lang_label_var = tk.StringVar()
label_search = tk.Label(
    textvariable = lang_label_var
    # whatever other attributes
)
search_label_var = tk.StringVar()
languagelabel = tk.Label(
    textvariable = search_label_var
    # whatever other attributes
)

You should be able to set() these variables to whichever word is appropriate for English or German with similar logic following choice = variable.get(). The label text for each item should update automatically when the variable assigned to textvariable is set.

if choice == 'Deutsch':
    lang_label_var.set('Sprache:')
    search_label_var.set('Suche')
elif choice == 'English':
    lang_label_var.set('Language:')
    search_label_var.set('Search')
JRiggles
  • 4,847
  • 1
  • 12
  • 27
  • Thank you for your answer, but now I got this problem: `AttributeError: 'Page1' object has no attribute 'search_label_var` I tried: `Page1(self).search_label_var.set('Search')` How can I change the label across classes? – Tom Orrow May 17 '22 at 14:46
  • The easiest way I've found to share Tk variable data is to declare (and set) the `StringVar()` in your root class, then pass a reference to your root class to any child widget classes (see [here](https://stackoverflow.com/questions/33646605/how-to-access-variables-from-different-classes-in-tkinter)). Then access the variable from "Page1" via `self.controller.shared_data['search_label_var'].get()` (using the names from the method shown in the linked answer). The root class reference is called `controller` in that answer, but naturally you can call it whatever you like. – JRiggles May 17 '22 at 15:20
  • 1
    Thank you so much, I found it too. But you helped me a loot!!!! – Tom Orrow May 17 '22 at 15:22
  • 1
    This is a similar solution [link](https://stackoverflow.com/questions/50533079/how-to-properly-access-a-stringvar-of-a-class-from-another-class-python-tk) without you I wouldn't have found It. Thank you! – Tom Orrow May 17 '22 at 15:25