Reputation: 91
I have a question about application created with tkinter. Application almost created, but now additional option must be added.
I must add extra language for widget names and for combobox options. Here is code snippet about how initialized my widgets:
DISTRICTS = ["A", "B", "C"]
dist_lable = tkinter.Label(self.parent, text="Seller Name")
dist_name = tkinter.ttk.Combobox(self.parent, textvariable=dist_var, state="readonly",
values=DISTRICTS)
I want to add radiobuttons\entry to give user ability to choose language.
This application also saves some selected options when user close it and load setting at the next start. Autoload function defined just after my init method. I think I can also save selected by user language to config file and then use this options to use selected language.
Now I need to choose the best way to achieve this, but only one solution I figured out yet: after function that loads my previous config reinit my app with language option:
Here is example (suppose that LANG = AZ
):
ENG, AZ = 1,2
DISTRICTS = ["A", "B", "C"],[["D", "E", "F"]
dist_lable = tkinter.Label(self.parent, text=["Seller Name","Satici Adi"][LANG])
dist_name = tkinter.ttk.Combobox(self.parent, textvariable=dist_var, state="readonly",
values=DISTRICTS[LANG])
In this solution my app will be initialized with default language and will be reinitialized either will find config file or when user choose other language in GUI.
Also I think that I can write some code before tkinter which will try to load LANG from config file prior tkinter mainloop().
I'm new in programming and not sure that this way is best (because may be I can avoid reinitialization) and just ask you to suggest me new solution or comment my.
Upvotes: 2
Views: 3439
Reputation: 352
You can use widget.config(**options)
to change the text on the fly. For example:
# New language chosen here
dist_label.config(text=["Seller name", "Naam verkoper"][LANG])
You could also try using StringVar()
var = Tkinter.StringVar()
var.set(["Seller name", "Naam verkoper"][LANG])
lbl = Tkinter.Label(root, textvariable=var)
Upvotes: 3