Python Tkinter窗口打开时不带标签
我有一个文本文件,里面有一些法语动词:Python Tkinter窗口打开时不带标签,python,tkinter,Python,Tkinter,我有一个文本文件,里面有一些法语动词: 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 i
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()
但每当我运行主窗口(上面的第三段代码)并选择“法语”,然后选择“将动词变为现在时”,它就会打开窗口,但不会出现任何问题等:
当它看起来像:
你知道我做错了什么吗?因为一些奇怪的Tkinter标签不能与
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)
然后标签将在UI中可见,我认为您还需要更新其他标签,看起来更具体于在现有标签上创建的新框架。Read
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)