mardi 21 avril 2020

How do I iterate a random generator in Tkinter? - Python

I have a basic GUI for my app using Tkinter, the generator will show me a random word from the dictionary in Hebrew, there are two main functionalities I do not know how to create.

1) When I click next, I want to see a new vocabulary word. How do I create a function to generate a new word in the designated Tkinter frame when I click the 'next' button?

2) When I click submit, I want to check if the English (key) matches the Hebrew (value) in my dictionary. How do I create a function to verify the user input against the corresponding key to the given value for the button command?

I'm new, so appreciate the learning opportunity, and I actually want to use this app for my own studying. Here is most of the code:

dictionary = {'give her':'תנ לה', 'give me': 'תני לי', 'give us': 'תן לנו' ,'give him': 'תן לו', 'give them': 'תן להם' , 'give to them (f)': 'תן להן'}

question = random.choice(list(dictionary.values()))

answer = dictionary.keys() == question

class Feedback:

    def __init__(self, master):    

        self.frame_header = ttk.Frame(master)
        self.frame_header.pack()
        # self.logo = PhotoImage(file = '/Users/las/Desktop/Python/flagicon.png')
        # ttk.Label(self.frame_header, image = self.logo).grid(row = 0, column = 0, rowspan = 2)
        ttk.Button(self.frame_header, text = 'Quit', command = self.Quit).grid(row=0, column=3)

        ttk.Label(self.frame_header, wraplength = 250, text = 'Type the English translation of the random word in Hebrew.').grid(row = 1, column = 1)
        ttk.Label(self.frame_header, wraplength = 300,
                  text = ("Hebrew Word", question)).grid(row = 2, column = 1)


        self.frame_content = ttk.Frame(master)
        self.frame_content.pack()

        ttk.Label(self.frame_content, text = 'English:').grid(row = 0, column = 1, padx = 5, sticky = 'sw')

        self.user_input = ttk.Entry(self.frame_content, text = "Your Answer?", width = 24)
        self.user_input.grid(row = 1, column = 1, padx = 5)

        ttk.Button(self.frame_content, text = 'Submit', command = self.submit).grid(row = 2, column = 1, padx = 5, pady = 5, sticky = 'e')
        ttk.Button(self.frame_content, text = 'Clear', command = self.clear).grid(row = 3, column = 0, padx = 5, pady = 5, sticky = 'w')
        ttk.Button(self.frame_content, text = 'Next', command = self.next).grid(row = 3, column = 2, padx = 5, pady = 5, sticky = 'w')

    def submit(self):
        self.answer = answer
        user_input = self.user_input
        if user_input == answer:
            print ("Correct")
        else:
            print ("!לא נכון ", "The answer is:", dictionary.keys(question))

    def clear(self):
        self.user_input.delete(0, 'end')

    def next(self):
        print (question)



Aucun commentaire:

Enregistrer un commentaire