我正在尝试为我正在学校做的I.T项目组建一个python程序,它正在努力!我对python很新,所以我真的在这里苦苦挣扎。
无论如何,我创建的程序应该将数字转换为单词(英语和毛利语)。例如,如果用户点击“1”,则当选择英语时,屏幕上的图像变为“1”,当选择毛利人时,屏幕上的图像变为“tahi”。我有两个列表,每个语言一个(我只使用数字1-10),当用户点击下拉列表中的语言时,我需要更改通过我的number_converter函数传递的列表。
我一直试图通过在number_converter函数中使用一个由两个列表名称中的任何一个替换的变量来做到这一点。对不起,如果这一切都非常令人困惑,我很难自己理解它!无论如何,这是我的代码,也许你可以搞清楚:
# Number Converter Version 1.04
import tkinter, sys
class newpulldownlist:
# Creates a pull-down list of options through mypulldown = newpulldownlist(parent, [array of options], command)
# mypulldown.value() returns the selected item
# mypulldown.chngevlue("to this") forces a selection change
# mypulldown.options (without brackets) returns an array of the option choices
# By default, fist item in the list is pre-selected.
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:
# Create a new button as mybutton = newbutton(parent, x, y, startingtext, command)
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): # Image changing function, calls images from the 'number' array.
global language
print("You pressed", n, ".") # One function is used to streamline the code and avoid having a function for each number.
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())
# Window.
appwindow = tkinter.Tk() # Makes window
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 options == 'Maori':
language = maori
print("Maori")
elif options == '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)
# Number converter debug
print(" Number Converter Debug ")
print("##############################")
appwindow.mainloop()
答案 0 :(得分:0)
如果我理解正确,我认为我的方法是使用字典。您可以执行numbers = {'maori': maori, 'english': english}
之类的操作,然后调用language = numbers['maori']
返回列表maori
。因此,您的语言下拉列表只需更改'english'
和'maori'
之间的字符串变量,您可以将其传递给字典以选择适当的语言。
编辑:有关词典的更多信息here。