【发布时间】:2019-09-02 16:50:15
【问题描述】:
我正在尝试制作一个界面,用户单击 TkInter 按钮以选择一种语言,然后该按钮调用一个函数(带有特定语言的参数)来设置程序的语言。
我尝试使用 Lambda 来传递函数,但没有成功。
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 是一个包含支持语言列表的字典。 lang_names 是一个字典,其中包含每种语言的名称(由 ISO 639-3 代码索引)。 setLang() 将字符串作为参数,特别是该语言的 ISO 639-3 代码。
我希望根据用户单击的任何按钮设置语言,但它总是将语言设置为语言列表中的最后一种语言。例如,目前支持 2 种语言:英语和冰岛语。无论我点击哪个按钮,它总是将语言设置为冰岛语,因为它按字母顺序排在最后。
【问题讨论】: