Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-使用变量确定要使用的数组?_Python_Arrays_Variables_User Interface - Fatal编程技术网

Python-使用变量确定要使用的数组?

Python-使用变量确定要使用的数组?,python,arrays,variables,user-interface,Python,Arrays,Variables,User Interface,我正在尝试为我在学校做的一个I.T项目整合一个python程序,它正在做我的头!我对python非常陌生,所以我在这里真的很挣扎 无论如何,我正在创建的程序应该是将数字转换成英语和毛利语的单词。例如,如果用户单击1,屏幕上的图像在选择英语时变为1,在选择毛利语时变为tahi。我有两个列表,每种语言一个,我只使用数字1-10,当用户单击下拉列表中的语言时,我需要更改通过数字转换器函数传递的列表 我一直试图通过在number_converter函数中使用一个变量来实现这一点,该变量由两个列表的名称中

我正在尝试为我在学校做的一个I.T项目整合一个python程序,它正在做我的头!我对python非常陌生,所以我在这里真的很挣扎

无论如何,我正在创建的程序应该是将数字转换成英语和毛利语的单词。例如,如果用户单击1,屏幕上的图像在选择英语时变为1,在选择毛利语时变为tahi。我有两个列表,每种语言一个,我只使用数字1-10,当用户单击下拉列表中的语言时,我需要更改通过数字转换器函数传递的列表

我一直试图通过在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()

如果我理解正确,我想我的方法是使用字典。您可以做一些类似数字={'maori':maori,'english':english}的操作,然后调用language=numbers['maori']返回毛利人列表。因此,您的语言下拉列表可以在“english”和“maori”之间更改一个字符串变量,并将其传递到字典以选择适当的语言

编辑:更多关于字典的信息