【问题标题】:How do I iterate a random generator in Tkinter? - Python如何在 Tkinter 中迭代随机生成器? - Python
【发布时间】:2020-04-21 20:45:07
【问题描述】:

我有一个使用 Tkinter 的应用程序的基本 GUI,生成器会向我显示希伯来语词典中的一个随机单词,我不知道如何创建两个主要功能。

1) 当我点击下一步时,我想看到一个新的词汇。当我单击“下一步”按钮时,如何创建一个函数以在指定的 Tkinter 框架中生成一个新单词?

2)当我点击提交时,我想检查英语(键)是否与我的字典中的希伯来语(值)匹配。如何创建一个函数来验证用户输入与按钮命令给定值的对应键的对应关系?

我是新人,非常感谢这次学习机会,我真的想用这个应用程序来自己学习。 以下是大部分代码:

from tkinter import ttk
import random

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)

【问题讨论】:

    标签: python dictionary tkinter random generator


    【解决方案1】:

    以下两行应删除,因为它们对您的应用程序无用:

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

    __init__()函数中更改以下行:

    ttk.Label(self.frame_header, wraplength = 300,
              text = ("Hebrew Word", question)).grid(row = 2, column = 1)
    

    到:

    self.question = ttk.Label(self.frame_header, wraplength=300)
    self.question.grid(row=2, column=1)
    

    然后更新next()submit()函数如下:

    def submit(self):
        user_input = self.user_input.get().strip()
        if user_input == self.answer:
            print ("Correct")
        else:
            print ("!לא נכון ", "The answer is:", self.answer)
    
    ...
    
    def next(self):
        # select a random choice (English phrase) from dictionary as question
        self.answer = random.choice(list(dictionary.keys()))
        self.question.config(text="Hebrew Word: "+dictionary[self.answer])
    

    最后在__init__()末尾调用next()显示第一个问题:

    def __init__(self, master):
    
        ...
    
        self.next()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-10
      • 2018-04-02
      • 1970-01-01
      • 2020-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多