在 Python 中,有一个有趣的项目需要完成。这个项目的功能是将数字转换为单词(英语和毛利语),例如,当用户点击“1”时,屏幕上的图像会根据下拉列表中选择的语言而改变,当选择英语时变为“one”,当选择毛利语时变为“tahi”。为此,需要创建两个列表,分别对应每种语言(仅使用数字 1-10),然后根据用户在下拉列表中点击的语言,将相应的列表传递给 number_converter 函数。
2、解决方案
为了解决这个问题,可以使用一个变量来代替 number_converter 函数中的两个列表名称。当用户在下拉列表中选择一种语言时,该变量将被替换为所选列表的名称。这样,number_converter 函数就可以根据变量的值来确定要使用的列表。
代码示例
import tkinter, sys
class newpulldownlist:
def __init__ (self, parent, valuearray, cmd):
self.options = valuearray
self.listvariable = tkinter.StringVar()
self.listvariable.set(valuearray[0])
self.guilist = tkinter.OptionMenu(parent, self.listvariable, *valuearray, command = cmd)
self.guilist.configure(width = "0")
self.guilist.pack()
def value(self):
return self.listvariable.get()
def changevalue(self, tothis):
self.listvariable.set(tothis)
class newbutton:
def __init__ (self, parent, x, y, label, cmd):
self.width = x
self.height = y
self.label = label
self.guibut = tkinter.Button(parent, width = x, height = y, text = label, command = cmd)
self.guibut.pack(side = "left")
def number_select(n):
global language
print("You pressed", n, ".")
picture.delete('all')
newpic = picture.create_image(602, 2, image = language[n], anchor = "n")
def select(self):
options = lang_list.value()
print(lang_list.value())
appwindow = tkinter.Tk()
appwindow.title("Number Converter")
rightside = tkinter.Frame(appwindow)
rightside.pack(side = tkinter.RIGHT)
topside = tkinter.Frame(appwindow)
topside.pack(side = tkinter.TOP)
bottomside = tkinter.Frame(appwindow)
bottomside.pack(side = tkinter.BOTTOM)
picture = tkinter.Canvas(topside, width = 1200, height = 70, bg = "white")
picture.pack()
# Arrays for each language.
options = ['English', 'Maori']
maori = []
maori.append(tkinter.PhotoImage(file = "Title_1.gif"))
maori.append(tkinter.PhotoImage(file = "1_Tahi.gif"))
maori.append(tkinter.PhotoImage(file = "2_Rua.gif"))
maori.append(tkinter.PhotoImage(file = "3_Toru.gif"))
maori.append(tkinter.PhotoImage(file = "4_Wha.gif"))
maori.append(tkinter.PhotoImage(file = "5_Rima.gif"))
maori.append(tkinter.PhotoImage(file = "6_Ono.gif"))
maori.append(tkinter.PhotoImage(file = "7_Whitu.gif"))
maori.append(tkinter.PhotoImage(file = "8_Waru.gif"))
maori.append(tkinter.PhotoImage(file = "9_Iwa.gif"))
maori.append(tkinter.PhotoImage(file = "10_Tekau.gif"))
english = []
english.append(tkinter.PhotoImage(file = "Title_1.gif"))
english.append(tkinter.PhotoImage(file = "1.gif"))
english.append(tkinter.PhotoImage(file = "2.gif"))
english.append(tkinter.PhotoImage(file = "3.gif"))
english.append(tkinter.PhotoImage(file = "4.gif"))
english.append(tkinter.PhotoImage(file = "5.gif"))
english.append(tkinter.PhotoImage(file = "6.gif"))
english.append(tkinter.PhotoImage(file = "7.gif"))
english.append(tkinter.PhotoImage(file = "8.gif"))
english.append(tkinter.PhotoImage(file = "9.gif"))
english.append(tkinter.PhotoImage(file = "10.gif"))
lang_list = newpulldownlist(appwindow, options, cmd = select)
language = lang_list.value()
if language == 'Maori':
language = maori
print("Maori")
elif language == 'English':
language = english
print("English")
startpic = picture.create_image(602, 2, image = language[0], anchor = "n")
# Buttons
one = newbutton(bottomside, 12, 1, "1", cmd = lambda n=1: number_select(n))
two = newbutton(bottomside, 12, 1, "2", cmd = lambda n=2: number_select(n))
three = newbutton(bottomside, 12, 1, "3", cmd = lambda n=3: number_select(n))
four = newbutton(bottomside, 12, 1, "4", cmd = lambda n=4: number_select(n))
five = newbutton(bottomside, 12, 1, "5", cmd = lambda n=5: number_select(n))
six = newbutton(bottomside, 12, 1, "6", cmd = lambda n=6: number_select(n))
seven = newbutton(bottomside, 12, 1, "7", cmd = lambda n=7: number_select(n))
eight = newbutton(bottomside, 12, 1, "8", cmd = lambda n=8: number_select(n))
nine = newbutton(bottomside, 12, 1, "9", cmd = lambda n=9: number_select(n))
ten = newbutton(bottomside, 12, 1, "10", cmd = lambda n=10: number_select(n))
quitbutton = newbutton(bottomside, 12, 1, "Quit", cmd = sys.exit)
appwindow.mainloop()
在上面的代码中,通过在 number_converter 函数中使用一个名为 language
的全局变量,成功地解决了在两个列表之间切换的问题。language
变量的值由下拉列表中的选择决定,并在每次选择更改时进行更新。这样,number_converter 函数就可以根据 language
的值来确定要使用的列表。