Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13981

How to change the language of the whole interface by command from the user?

$
0
0

How to implement that, for example, the user can select a language from the drop-down list and the whole interface will change to the selected language? I want to implement at least 3 languages: English, Polish, Ukrainian. How to implement (the programme is written in customtkinter)?

import customtkinterimport osfrom PIL import Imageclass App(customtkinter.CTk):    def __init__(self):        super().__init__()        self.title("FinPulse")        self.geometry("750x450")        self.wm_resizable(False, False)        self.iconbitmap(r'C:\Users\vladm\Desktop\FinPulseCode\images\logo1.ico')        # set grid layout 1x2        self.grid_rowconfigure(0, weight=1)        self.grid_columnconfigure(1, weight=1)        # load images with light and dark mode image        image_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "images")        self.logo_image = customtkinter.CTkImage(Image.open(os.path.join(image_path, "logo.png")), size=(40, 40))        self.kurs = customtkinter.CTkImage(light_image=Image.open(os.path.join(image_path, "kurs_dark.png")),                                                 dark_image=Image.open(os.path.join(image_path, "kurs_light.png")), size=(25, 25))        self.news = customtkinter.CTkImage(light_image=Image.open(os.path.join(image_path, "news_dark.png")),                                                 dark_image=Image.open(os.path.join(image_path, "news_light.png")), size=(25, 25))        self.swap = customtkinter.CTkImage(light_image=Image.open(os.path.join(image_path, "swap_dark.png")),                                                 dark_image=Image.open(os.path.join(image_path, "swap_light.png")), size=(25, 25))        self.settings = customtkinter.CTkImage(light_image=Image.open(os.path.join(image_path, "settings_dark.png")),                                                 dark_image=Image.open(os.path.join(image_path, "settings_light.png")), size=(25, 25))        # create navigation frame        self.navigation_frame = customtkinter.CTkFrame(self, corner_radius=0)        self.navigation_frame.grid(row=0, column=0, sticky="nsew")        self.navigation_frame.grid_rowconfigure(4, weight=1)        self.navigation_frame_label = customtkinter.CTkLabel(self.navigation_frame, text="", image=self.logo_image)        self.navigation_frame_label.grid(row=0, column=0, padx=20, pady=20)        self.kurs_button = customtkinter.CTkButton(self.navigation_frame, corner_radius=0, height=40, border_spacing=10, text="Курси",                                                   fg_color="transparent", text_color=("gray10", "gray90"), hover_color=("gray70", "gray30"),                                                   image=self.kurs, anchor="w", command=self.kurs_button_event)        self.kurs_button.grid(row=1, column=0, sticky="ew")        self.swap_button = customtkinter.CTkButton(self.navigation_frame, corner_radius=0, height=40, border_spacing=10, text="Конвертер",                                                      fg_color="transparent", text_color=("gray10", "gray90"), hover_color=("gray70", "gray30"),                                                      image=self.swap, anchor="w", command=self.swap_button_event)        self.swap_button.grid(row=2, column=0, sticky="ew")        self.news_button = customtkinter.CTkButton(self.navigation_frame, corner_radius=0, height=40, border_spacing=10, text="Новини",                                                      fg_color="transparent", text_color=("gray10", "gray90"), hover_color=("gray70", "gray30"),                                                      image=self.news, anchor="w", command=self.news_button_event)        self.news_button.grid(row=3, column=0, sticky="ew")        self.settings_button = customtkinter.CTkButton(self.navigation_frame, corner_radius=0, height=40, border_spacing=10, text="Налаштування",                                                      fg_color="transparent", text_color=("gray10", "gray90"), hover_color=("gray70", "gray30"),                                                      image=self.settings, anchor="w", command=self.settings_button_event)        self.settings_button.grid(row=4, column=0, sticky="s")        # create kurs frame        self.kurs_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent")        # create swap frame        self.swap_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent")        # create news frame        self.news_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent")        # create settings frame        self.values_ukrainian = ["Системи", "Світла", "Темна"]        self.values_english = ["System", "Light", "Dark"]        self.settings_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent")        self.settings_frame.grid_columnconfigure(0)        self.appearance_mode_label = customtkinter.CTkLabel(self.settings_frame, text="Кольоровасхема:", anchor="w")        self.appearance_mode_label.grid(row=0, column=0, padx=20, pady=(10, 0))        self.settings_appearance_mode_menu = customtkinter.CTkOptionMenu(self.settings_frame, values=self.values_ukrainian, command=self.change_appearance_mode_event)        self.settings_appearance_mode_menu.grid(row=1, column=0, padx=(10, 10), pady=(0, 20))        self.scaling_label = customtkinter.CTkLabel(self.settings_frame, text="Масштаб:", anchor="w")        self.scaling_label.grid(row=0, column=1, padx=20, pady=(10, 0))        self.scaling_optionemenu = customtkinter.CTkOptionMenu(self.settings_frame, values=["100%", "110%", "120%", "130%", "140%", "150%"], command=self.change_scaling_event)        self.scaling_optionemenu.grid(row=1, column=1, padx=(10, 10), pady=(0, 20))        # select default frame        self.select_frame_by_name("kurs")    def select_frame_by_name(self, name):        # set button color for selected button        self.kurs_button.configure(fg_color=("gray75", "gray25") if name == "kurs" else "transparent")        self.swap_button.configure(fg_color=("gray75", "gray25") if name == "swap" else "transparent")        self.news_button.configure(fg_color=("gray75", "gray25") if name == "news" else "transparent")        self.settings_button.configure(fg_color=("gray75", "gray25") if name == "settings" else "transparent")        # show selected frame        if name == "kurs":            self.kurs_frame.grid(row=0, column=1, sticky="nsew")        else:            self.kurs_frame.grid_forget()        if name == "swap":            self.swap_frame.grid(row=0, column=1, sticky="nsew")        else:            self.swap_frame.grid_forget()        if name == "news":            self.news_frame.grid(row=0, column=1, sticky="nsew")        else:            self.news_frame.grid_forget()        if name == "settings":            self.settings_frame.grid(row=0, column=1, sticky="nsew")        else:            self.settings_frame.grid_forget()    def kurs_button_event(self):        self.select_frame_by_name("kurs")    def swap_button_event(self):        self.select_frame_by_name("swap")    def news_button_event(self):        self.select_frame_by_name("news")    def settings_button_event(self):        self.select_frame_by_name("settings")    def change_appearance_mode_event(self, selected_value):        index = self.values_ukrainian.index(selected_value)        english_value = self.values_english[index]        customtkinter.set_appearance_mode(english_value)    def change_scaling_event(self, new_scaling: str):        new_scaling_float = int(new_scaling.replace("%", "")) / 100        customtkinter.set_widget_scaling(new_scaling_float)if __name__ == "__main__":    app = App()    app.mainloop()[text]

I can't find normal documentation or a similar problem


Viewing all articles
Browse latest Browse all 13981

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>