Python 3.x 如何使tkinter标签可更新,以及如何在按enter键时提交输入值

Python 3.x 如何使tkinter标签可更新,以及如何在按enter键时提交输入值,python-3.x,tkinter,Python 3.x,Tkinter,我正在使用tkinter制作一个gui应用程序,我希望标签是可更新的 我有一个单词列表,我试着使用for循环遍历所有列表 没有tkinter它也能工作,但当我使用tkinter时,只有一个字被打印出来 from tkinter import * import random used = [] words = ['car','dog','ball'] plural = { 'car' : 'cars', 'dog': 'dogs', 'ball': 'balls'

我正在使用tkinter制作一个gui应用程序,我希望标签是可更新的 我有一个单词列表,我试着使用for循环遍历所有列表 没有tkinter它也能工作,但当我使用tkinter时,只有一个字被打印出来

 from tkinter import * 
 import random
 used = []
 
 words = ['car','dog','ball']
 plural = {
  'car' : 'cars',
  'dog': 'dogs',
  'ball': 'balls'
    }
 root = Tk()
 while i < len(words):
         word = random.choice(words)
         prompt = Label(root,text='what is the plural of the following word?')
          #i want currentWord to be updatable 
         currentWord = Label(root,text=word)
         answer = Entry(root,width=20)
         prompt.pack()
         currentWord.pack()
         def myClick():
                if answer.get() != '':
                   if answer.get() == plural.get(word):
                        myLabel = Label(root,text='Correct')
                        myLabel.pack()
                    else:
                       myLabel = Label(root,text='wrong')
                       myLabel.pack()
          myButton = Button(root,text='submit',command=myClick,bg='green')
          answer.pack()
          myButton.pack()
          root.mainloop()
从tkinter导入*
随机输入
已用=[]
单词=['car'、'dog'、'ball']
复数={
‘汽车’:‘汽车’,
“狗”:“狗”,
“球”:“球”
}
root=Tk()
而我(用词):
单词=随机。选择(单词)
prompt=Label(root,text='以下单词的复数形式是什么?')
#我希望currentWord是可更新的
currentWord=标签(根,文本=word)
回答=输入(根,宽度=20)
prompt.pack()
currentWord.pack()
def myClick():
如果回答。get()!='':
如果answer.get()==复数.get(单词):
myLabel=标签(根,text='Correct')
myLabel.pack()
其他:
myLabel=标签(根,text='error')
myLabel.pack()
myButton=Button(root,text='submit',command=myClick,bg='green')
答案:pack()
myButton.pack()
root.mainloop()

如果这是您要查找的内容,请检查代码段

config
用于在初始化对象后访问其属性

例如,在这里,您定义
currentWord=Label(root,text=“Answer”)

但是你要设置它的
text
属性

所以您使用config:
currentWord.config(text=answer.get())

提示标签也一样

from tkinter import *
root = Tk()
words = ['word1','word2','word3']
def myClick():
    if answer.get() != '':
        if answer.get() in words:
            currentWord.config(text=answer.get())
            prompt.config(text="Correct")
        else:
            currentWord.config(text=" ")
            prompt.config(text="Wrong")

currentWord = Label(root,text="Answer")
currentWord.pack()
answer = Entry(root,width=20)         
answer.pack()
prompt = Label(root,text='Status')
prompt.pack()
myButton = Button(root,text='submit',command=myClick,bg='green')
myButton.pack()
root.mainloop()
编辑1 我认为没有必要使用
列表
,因为您正在通过
条目小部件
输入并在字典中搜索

plural = {
  'car' : 'cars',
  'dog': 'dogs',
  'ball': 'balls'
    }
def myClick():
    if answer.get() != '':
        if answer.get() in plural.values():
            currentWord.config(text=answer.get())
            prompt.config(text="Correct")
        else:
            currentWord.config(text=" ")
            prompt.config(text="Wrong")
Edit2

如果在列表中循环,那么如果随机单词不匹配,那么您的条件将失败。因此我使用了一个无限循环

from tkinter import *
import random
root = Tk()
words = ['car','dog','ball']
plural = {
  'car' : 'cars',
  'dog': 'dogs',
  'ball': 'balls'
    }
def myClick():
  while True:
    word=random.choice(words)
    print(word)
    if answer.get() != '':
        if (answer.get() in words) and(answer.get()==word):
            currentWord.config(text=plural[answer.get()])
            prompt.config(text="Correct")
            break
        elif(answer.get() not in words):
            currentWord.config(text=answer.get()+" does not exist")
            prompt.config(text="Wrong")
            break
        else:
          pass
currentWord = Label(root,text="Answer")
currentWord.pack()
answer = Entry(root,width=20)         
answer.pack()
prompt = Label(root,text='Status')
prompt.pack()
myButton = Button(root,text='submit',command=myClick,bg='green')
myButton.pack()
root.mainloop()

这个函数的作用是什么?get(word)
它检查单词是否与词典中的关键字匹配。为了您的答案,请重新检查问题我添加了完整的代码@ППааааааааааааа107。我想你不需要一个列表我用这个列表作为随机模块,所以它从列表中得到一个随机词@ПППППааааааааааааааааааа