猿问

创建多个函数以在 1 个结果框中返回单独的字典结果?

我花了一些时间更新我的代码。我正在尝试创建一个返回首字母缩略词的工具。我为字母表中的每个字母创建了单独的词典,并创建了一个结果框来返回首字母缩略词的含义。但是,在运行代码时,我只能获取最新的字典来返回结果,即“c”字典中的任何首字母缩略词,并且每次添加新字典时都会失去以前的功能。我对编码很陌生,在问这个问题之前我对以前的问题做了很多研究,所以任何帮助都将不胜感激。谢谢。这是我到目前为止的代码:


from tkinter import*


acronym_dictionary={"A":"Annual", "AC":"Air Conditioning",

                    }


acronym_dictionary_b={"BA":"British Airway", "BB":"BumbleBee",

                      }                    


acronym_dictionary_c={"Ca":"Calcium","Co":"Company",

                      }


def Return_Entry(en):

    content= entry.get()

    result= acronym_dictionary.get(content, "Not found")

    print(result)

    resultBox.delete(0,END)

    resultBox.insert(0,result)


def Return_EntryB(en):

    content= entry.get()

    result= acronym_dictionary_b.get(content, "Not found")

    print(result)

    resultBox.delete(0,END)

    resultBox.insert(0,result)`


def Return_EntryC(en):

    content= entry.get()

    result= acronym_dictionary_c.get(content, "Not found")

    print(result)

    resultBox.delete(0,END)

    resultBox.insert(0,result)


def EntryDel():

    resultBox.delete(0,END)

    entry.delete(0,END)


master=Tk()

master.title("The Acronym Search Engine")

master.geometry('500x400')`




Button(master, text="Clear",command=EntryDel).grid(row=7, sticky=W)`


Label(master, text="A:").grid(row=0, sticky=W)

entry=Entry()

entry.grid(row=0, column=1)

entry.bind('<Return>', Return_Entry)


Label(master, text="B:").grid(row=1, sticky=W)

entry=Entry()

entry.grid(row=1, column=1)

entry.bind('<Return>', Return_EntryB)`


Label(master, text="C:").grid(row=2, sticky=W)

entry=Entry()

entry.grid(row=2, column=1)

entry.bind('<Return>',Return_EntryC)


Label(master, text="Result:").grid( row=3,column=0 )

resultBox=Entry(master)

resultBox.grid(row=3,column=1)


mainloop()


慕沐林林
浏览 177回答 2
2回答

宝慕林4294392

您的代码需要做一些工作。您正在覆盖entry变量,因此只能访问您创建的最后一个框。这与您master在评论部分中提到的删除无关。您需要做的是为每个字段指定一个唯一的名称。然后对于每个函数,您需要更改content以反映正确的字段。这是您重新编写的代码,并更改了一些函数/变量名称和间距以反映 PEP8 标准。import tkinter as tkacronym_dictionary = {"A": "Annual", "AC": "Air Conditioning"}acronym_dictionary_b = {"BA": "British Airway", "BB": "BumbleBee"}acronym_dictionary_c = {"Ca": "Calcium", "Co": "Company"}def return_entry(event):&nbsp; &nbsp; content = entry.get()&nbsp; &nbsp; result = acronym_dictionary.get(content, "Not found")&nbsp; &nbsp; print(result)&nbsp; &nbsp; result_box.delete(0, "end")&nbsp; &nbsp; result_box.insert("end", result)def return_entry_b(event):&nbsp; &nbsp; content = entry2.get()&nbsp; &nbsp; result = acronym_dictionary_b.get(content, "Not found")&nbsp; &nbsp; print(result)&nbsp; &nbsp; result_box.delete(0, "end")&nbsp; &nbsp; result_box.insert("end", result)def return_entry_c(event):&nbsp; &nbsp; content = entry3.get()&nbsp; &nbsp; result = acronym_dictionary_c.get(content, "Not found")&nbsp; &nbsp; print(result)&nbsp; &nbsp; result_box.delete(0, "end")&nbsp; &nbsp; result_box.insert("end", result)def entry_del():&nbsp; &nbsp; result_box.delete(0, "end")&nbsp; &nbsp; entry.delete(0, "end")&nbsp; &nbsp; entry2.delete(0, "end")&nbsp; &nbsp; entry3.delete(0, "end")master = tk.Tk()master.title("The Acronym Search Engine")master.geometry('200x110')tk.Button(master, text="Clear", command=entry_del).grid(row=7, sticky="w")tk.Label(master, text="A:", anchor="e").grid(row=0, sticky="ew")tk.Label(master, text="B:", anchor="e").grid(row=1, sticky="ew")tk.Label(master, text="C:", anchor="e").grid(row=2, sticky="ew")tk.Label(master, text="Result:").grid(row=3, column=0)entry = tk.Entry(master)entry2 = tk.Entry(master)entry3 = tk.Entry(master)result_box = tk.Entry(master)#result_box = tk.Text(master, width=40, height=3)entry.grid(row=0, column=1, sticky="w")entry2.grid(row=1, column=1, sticky="w")entry3.grid(row=2, column=1, sticky="w")result_box.grid(row=3, column=1, sticky="w")entry.bind('<Return>', return_entry)entry2.bind('<Return>', return_entry_b)entry3.bind('<Return>', return_entry_c)master.mainloop()结果:也就是说,实际上有比使用自己的变量名称创建单独的字典/条目字段更好的方法。如果我们使用列表,我们可以将所有字典放入一个列表,将所有条目字段放入一个列表,然后获取所有结果。我们还可以使用文本框按顺序显示所有结果。import tkinter as tkacronym_dictionary_list = [{"A": "Annual", "AC": "Air Conditioning"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{"BA": "British Airway", "BB": "BumbleBee"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{"CA": "Calcium", "CO": "Company"}]entry_list = []def return_entry(event):&nbsp; &nbsp; result_box.delete(1.0, "end")&nbsp; &nbsp; for ndex, ent in enumerate(entry_list):&nbsp; &nbsp; &nbsp; &nbsp; if ent.get().strip() != "":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sub_dict = acronym_dictionary_list[ndex]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result_box.insert("end", "{}\n".format(sub_dict.get(ent.get().strip().upper(), "Not found")))def entry_del():&nbsp; &nbsp; result_box.delete(1.0, "end")master = tk.Tk()master.title("The Acronym Search Engine")master.geometry('400x200')for i in range(3):&nbsp; &nbsp; tk.Label(master, text="{}:".format(chr(ord('@')+i+1)), anchor="e").grid(row=i, column=0, sticky="ew")&nbsp; &nbsp; entry_list.append(tk.Entry(master))&nbsp; &nbsp; entry_list[i].grid(row=i, column=1, sticky="w")&nbsp; &nbsp; entry_list[i].bind('<Return>', return_entry)result_box = tk.Text(master, width=40, height=3)tk.Label(master, text="Result:").grid(row=3, column=0)result_box.grid(row=3, column=1, sticky="w")tk.Button(master, text="Clear", command=entry_del).grid(row=7, sticky="w")master.mainloop()结果:

holdtom

听起来是因为你在每个新的“条目”上覆盖条目entry=Entry(master)每次创建一个新条目。这可能会导致您在何处看到的行为每次我添加一个新词典时,我都会失去以前的功能
随时随地看视频慕课网APP

相关分类

Python
我要回答