我的文本文件中包含一些法语动词:
accepter – to accept
adorer – to adore
aimer – to like
annuler – to cancel
apporter – to bring
我有一个Python文件,它将打开该文件,读取它并生成一些有关将这些动词缀合为现在时的随机问题:
from tkinter import *
from tkinter import ttk
from definitions import *
from random import *
def check_conjugation(*args):
answer_given = str(answer.get())
correct_answer = present_tense_regular(random_verb, random_pronoun)
def unaccent(word):
output = ""
for x in word:
english_letters = [y for y in "abcdefghijklmnopqrstuvwxyz"]
accents = [y for y in "àâáçèéêëìíîòóôùúû"]
replacements = [y for y in "aaaceeeeiiiooouuu"]
if x in english_letters:
output += x
else:
for y in range(len(accents)):
if x == accents[y]:
output += replacements[y]
break
return output
unaccented_answer_given = unaccent(answer_given)
unaccented_correct_answer = unaccent(correct_answer)
if answer_given == correct_answer:
decision.set("That is correct, well done!")
next_button.focus()
fr_conj_present.bind('<Return>', set_new_pair)
elif unaccented_answer_given == unaccented_correct_answer:
decision.set(
"""You have conjugated correctly, well done!
However there were missing accents in your typed answer.
The correct answer was %s""" % correct_answer)
next_button.focus()
fr_conj_present.bind('<Return>', set_new_pair)
else:
decision.set("That is not correct. Try again.")
list_of_verbs = []
with open("french_regular_verbs.txt", 'r') as file:
for line in file:
verb = ""
for letter in line:
if letter == " ":
break
else:
verb += letter
list_of_verbs.append(verb)
number_of_verbs = len(list_of_verbs)
def new_pair():
global random_pronoun
global random_verb
random_number = randint(0,number_of_verbs - 1)
random_verb = list_of_verbs[random_number]
random_pronoun = ["je", "tu", "il", "vous", "nous", "ils"][randint(0,5)]
return ((random_verb, random_pronoun))
def set_new_pair(*args):
question_text.set("Conjugate the verb '%s' to the pronoun '%s' in the present tense:" % new_pair())
decision.set("")
conjugation.focus()
fr_conj_present.bind('<Return>', check_conjugation)
def present_tense_conjugate():
global mainframe, fr_conj_present, answer, decision, next_button, conjugation, question_text, number_of_verbs, list_of_verbs
list_of_verbs = []
with open("french_regular_verbs.txt", 'r') as file:
for line in file:
verb = ""
for letter in line:
if letter == " ":
break
else:
verb += letter
list_of_verbs.append(verb)
number_of_verbs = len(list_of_verbs)
new_pair()
fr_conj_present = Tk()
fr_conj_present.title("Conjugating verbs")
mainframe = ttk.Frame(fr_conj_present, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
fr_conj_present.columnconfigure(0, weight=1)
fr_conj_present.rowconfigure(0, weight=1)
answer = StringVar()
decision = StringVar()
decision.set("")
question_text = StringVar()
question_text.set("Conjugate the verb '%s' to the pronoun '%s' in the present tense:" % new_pair())
ttk.Label(mainframe, textvariable=question_text).grid(column=1, row=1)
conjugation = ttk.Entry(mainframe, width=20, textvariable=answer)
conjugation.grid(column=1, row=2, sticky=(W, E))
ttk.Label(mainframe, textvariable=decision).grid(column=1,row=3, sticky=(W, E))
ttk.Button(mainframe, text="Check", command=check_conjugation).grid(column=2,row=2)
next_button = ttk.Button(mainframe, text="Next", command=set_new_pair)
next_button.grid(column=2,row=3)
for child in mainframe.winfo_children():
child.grid_configure(padx=5, pady=5)
conjugation.focus()
fr_conj_present.bind('<Return>', check_conjugation)
fr_conj_present.mainloop()
我还有另一个程序,应该是调用第一个函数的主窗口:
from tkinter import *
from tkinter import ttk
import french_present_conjugate
def french_appear():
l2_text.set("You have chosen French. What would you like to do next?")
b4.grid(row=2, column=2)
master = Tk()
master.title('MRL Languages')
master.geometry("700x500")
l1 = ttk.Label(master, text="Choose a langugage:")
l1.grid(row=1, column=1, pady=20)
b1 = ttk.Button(master, text="French\nFrancais", command=french_appear)
b1.grid(row=2, column=1, sticky=W, padx = 25, pady=5)
b2 = ttk.Button(master, text="German\nDeutsch")
b2.grid(row=3, column=1, sticky=W, padx = 25, pady=5)
b3 = ttk.Button(master, text="Russian\nРусскйи")
b3.grid(row=4, column=1, sticky=W, padx = 25, pady=5)
l2_text = StringVar()
l2_text.set("Please select a language from the left for more options.")
l2 = ttk.Label(master, textvariable=l2_text)
l2.grid(row=1, column=2, padx=25, pady=20)
b4 = ttk.Button(master,
text="Conjugate verbs to present tense",
command=french_present_conjugate.present_tense_conjugate)
mainloop()
但是,只要我运行主窗口(上面的代码的第三块)并选择“法语”,然后选择“使动词呈现时态”,它就会打开窗口,但没有任何问题,等等:
看起来应该像这样:
知道我在做什么错吗?
答案 0 :(得分:0)
对于某些奇怪的Tkinter Lable,它不支持textvariable
参数,尽管如果我们使用text='Hello'
,则它可以完美地工作。
因此,如果您将代码更改为
txt = "Conjugate the verb '%s' to the pronoun '%s' in the present tense:" % new_pair()
ttk.Label(mainframe, text=txt).grid(column=1, row=1)
然后标签将在用户界面中可见,我认为您还需要更新其他标签,看起来更特定于在现有标签顶部创建的新框架。