I am trying to make an interface where a user clicks a TkInter button to select a language, and then the button calls a function (with an argument for the specific language) to set the language for the program.
I tried using Lambdas for passing the functions, but that didn't work.
def showLangButtons():
tk = Tk()
root = Canvas(tk, width=100, height=100)
root.pack()
langButtons = []
langs = []
for a in langf:
langs.append(a)
for a in sorted(langs):
langButtons.append(Button(root, text=lang_names[a][a], width=19,
height=2, command = lambda:setLang(a)))
# This part of the function displays the buttons on a grid
const = 0
while const < (len(langButtons))**(1/2)/1.75:
const += 1
n = 0
while n < len(langButtons):
langButtons[n].grid(row = int(n/const), column = n%const, sticky = W)
n+=1
tk.update()
langf
is a dictionary which contains the list of supported languages. lang_names
is a dictionary which contains the names of each language (indexed by the ISO 639-3 code). setLang()
takes a string as its argument, specifically the ISO 639-3 code of the language.
I expect the language to be set corresponding to whichever button the user clicks, but it always sets the language to the last language in the language list. For example, there are currently 2 supported languages: English and Icelandic. Regardless of which button I click, it always sets the language to Icelandic, because it is last in alphabetical order.