My GUI application has two radiobuttons for changing the displayed language. The words are assigned to the constants text1, text2, an so on, in two files:
file_en.py:
text1 = 'Hello'
text2 = 'Goodbye'
...
file_fr.py:
text1 = 'Bonjour'
text2 = 'Au revoir'
...
On start, the main module imports file_en or file_fr and everything is fine. Now, when the user changes the language - can I immediately replace one imported constants assignment with another?
I don't think that is possible, but I think I found a workaround for that. How about you have a function ( loadLang()
or something) that will change the language each time it is called, reading 2 different text files.
text1=''
text2=''
activeLang = 'en' #current language
def loadLang(lang):
global text1
global text2
global activeLang
if activeLang != lang: #check if current language is
already what user wants
file=open("file_" + lang + ".txt", "r") #open file
#assuming contents are same as your post without spaces
text1=file.readline().rstrip()
text2=file.readline(2).rstrip() #read line and remove
\n
return
#main code
#idk how you're doing it so:
def onClick():
global text1
global text2
if 'fr' in button.text:
loadLang('fr')
button1.text = text1
button2.text = text2
You should try this instead, it's must more simple and efficient.
import file_en
import file_fr
text1=""
text2=""
activeLang = 'en'#current language
def onClick():
global text1
global text2
global activeLang
if activeLang == 'en':
text1 = file_fr.text1
text2 = file_fr.text2
elif activeLang == 'fr': #check lang
text1 = file_en.text1
text2 = file_en.text2
else:
# Error handling goes here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.