我正在尝试为我在学校做的一个 IT 项目组合一个 python 程序,它正在我的脑海里!我对python很陌生,所以我在这里真的很挣扎。
无论如何,我正在创建的程序应该将数字转换为单词(英语和毛利语)。例如,如果用户单击“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()