1

私は学校で行っている IT プロジェクト用の Python プログラムをまとめようとしていますが、頭を悩ませています! 私はPythonに非常に慣れていないので、ここで本当に苦労しています。

とにかく、私が作成しているプログラムは、数字を単語 (英語とマオリ語) に変換することになっています。たとえば、ユーザーが「1」をクリックすると、画面上の画像は、英語を選択すると「one」に、maori を選択すると「tahi」に変わります。各言語に 1 つずつ、2 つのリストがあり (1 ~ 10 の数字のみを使用しています)、ユーザーがドロップダウン リストで言語をクリックしたときに、number_converter 関数を介して渡されるリストを変更する必要があります。

2 つのリストの名前のいずれかに置き換えられる 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()
4

1 に答える 1