def replace_acronym(): # function not yet implemented
#FIND
for abbr, text in acronyms.items():
if abbr == acronym_edit.get():
textadd.insert(0,text)
#DELETE
name = acronym_edit.get().upper()
name.upper()
r =dict(acronyms)
del r[name]
with open('acronym_dict.py','w')as outfile:
outfile.write(str(r))
outfile.close() # uneccessary explicit closure since used with...
message ='{0} {1} {2} \n '.format('Removed', name,'with its text from the database.')
display.insert('0.0',message)
#ADD
abbr_in = acronym_edit.get()
text_in = add_expansion.get()
acronyms[abbr_in] = text_in
# write amended dictionary
with open('acronym_dict.py','w')as outfile:
outfile.write(str(acronyms))
outfile.close()
message ='{0} {1}:{2}{3}\n '.format('Modified entry', abbr_in,text_in, 'added')
display.insert('0.0',message)
我正在尝试在我的 tkinter 小部件中添加编辑字典条目的功能。字典的格式是{ACRONYM: text, ACRONYM2: text2...}
我认为该功能将实现的是在字典中查找条目,删除首字母缩写词及其相关文本,然后添加首字母缩写词和文本已更改为的任何内容。例如,如果我有一个条目TEST: test
并且我想将它修改为TEXT: abc
函数返回的内容,会发生什么TEXT: testabc
- 尽管我已经(我认为)覆盖了文件,但仍附加更改的文本。
我究竟做错了什么?