-1

I've got a text file with some French verbs in:

accepter – to accept
adorer – to adore
aimer – to like
annuler – to cancel
apporter – to bring

I've got a Python file which will open this file, read it and generate some random questions about conjugating those verbs to the present tense:

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()

And I've got another program, which is supposed to be the main window from which the first function is called:

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()

But whenever I run the main window (the third block of code above) and choose "French" then "Conjugate verbs to present tense" it opens the window but without any questions etc:

Bad

When it should look like:

Good

Any idea what I'm doing wrong?

Luke Richards
  • 45
  • 2
  • 6
  • Read [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged) – stovfl Dec 26 '19 at 18:01

1 Answers1

0

For some strange Tkinter Lable is not working with textvariable argument though if we use text='Hello' then it works perfectly.

So if you change your code as

 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)

Then label would be visible in the UI, I think you need to update other labels as well, looks to be more specific to new frame created on the top of existing one.

sonus21
  • 5,178
  • 2
  • 23
  • 48