Python使用变量来确定要使用哪个数组?

2024-10-02 10:21:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为我在学校做的一个it项目编写一个python程序,它正在帮我的忙!我对python很陌生,所以我在这里很挣扎。你知道吗

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

我一直在尝试通过在number\u 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()

Tags: lambdaselfcmdnumberenglishtkintergifselect
1条回答
网友
1楼 · 发布于 2024-10-02 10:21:06

如果我理解正确的话,我想我的方法是用字典。您可以执行类似于numbers = {'maori': maori, 'english': english}的操作,然后调用language = numbers['maori']返回列表maori。因此,您的语言下拉列表只需在'english''maori'之间更改一个字符串变量,然后将其传递给字典以选择适当的语言。你知道吗

编辑:更多关于词典here。你知道吗

相关问题 更多 >

    热门问题