微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从.get函数获取条目?

如何解决如何从.get函数获取条目?

我对此有所有疑问,应该是字典和翻译,但是我放弃了翻译。目前,该程序无法从.get()函数获取任何条目。

    from tkinter import *
import tkinter as tk
import json
from difflib import get_close_matches
from googletrans import Translator
#LIBs are done#
root = Tk()
root.geometry('300x500')
root.configure(background = '#71baff')
#Window Created#
data = json.load(open('data.json'))
translator = Translator()
#Dictionary data has been read#

#Variables attened#


#BACKEND ENDED#


#FRONTEND#

welcome = tk.Label(root,text = 'Welcome to the Dictionary & Translator App',bg = '#71baff',fg = '#ffffff')
welcome.pack()
emptylab = tk.Label(root,bg = '#71baff')
emptylab.pack()
defq = tk.Label(root,text = 'Enter the word : ',fg = '#ffffff')
defq.pack()
defentry = tk.Entry(root,fg = '#ffffff')
defentry.pack()
defin = defentry.get()
def getMeaning(w):        
    defin = defentry.get()
        
    if w in data:
        return data[w]
    elif w.title() in data:
        return data[w.title]
    elif len(get_close_matches(w,data.keys())) > 0:
        return data[get_close_matches(w,data.keys()) [0]]
    else :
        return 'The word does not exist'

emptylab = tk.Label(root,bg = '#71baff')
emptylab.pack()
defbutton = tk.Button(root,text = 'Get DeFinition!',fg = '#71baff',command = getMeaning(defin))
defbutton.pack()
output = getMeaning(defin)
outputlabel = tk.Label(root,fg = '#ffffff',text = output)#need to add text after the functions for the output are done#
outputlabel.pack()



root.mainloop()

解决方法

事件编程有点棘手。您不应该从事件函数返回数据,因为将其设置为命令时,Button()对象不知道如何处理它。我看到您尝试编写与之相关的程序,但这要复杂得多。尽管可以在需要时完成操作,但最好不要在命令中传递变量。当然,这还没有完成,这是您的工作,希望它可以使您走得更远。

from tkinter import *
import tkinter as tk
import json
from difflib import get_close_matches


def getMeaning():
    defin = defentry.get()
    w = defin
    if w in data:
        outputlabel.configure(text=data[w])
    elif w.title() in data:
        outputlabel.configure(text=data[w.title])
    elif len(get_close_matches(w,data.keys())) > 0:
        outputlabel.configure(text=data[get_close_matches(w,data.keys())[0]])
    else:
        outputlabel.configure(text='The word does not exist')


# LIBs are done#
root = Tk()
root.geometry('300x500')
root.configure(background='#71baff')
# Window Created#
data = json.load(open('data.json'))

# Dictionary data has been read#

# Variables attened#


# BACKEND ENDED#


# FRONTEND#

welcome = tk.Label(root,text='Welcome to the Dictionary & Translator App',bg='#71baff',fg='#ffffff')
welcome.pack()
emptylab = tk.Label(root,bg='#71baff')
emptylab.pack()
defq = tk.Label(root,text='Enter the word : ',fg='#ffffff')
defq.pack()
defentry = tk.Entry(root,fg='#ffffff')
defentry.pack()
emptylab = tk.Label(root,bg='#71baff')
emptylab.pack()
defbutton = tk.Button(root,text='Get Definition!',fg='#71baff',command=getMeaning)
defbutton.pack()
outputlabel = tk.Label(root,fg='#ffffff')
outputlabel.pack()


root.mainloop()

我没有文件,因此出现文件错误。如果您还有其他错误,请在评论中让我知道,我们可以找出答案。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。