以下是程序
from tkinter import*
from tkinter import messagebox
import tkinter.ttk
class Pic:
def __init__(self):
self.root=tkinter.Tk()
self.root.title('电子词典')
self.root['height']=480
self.root['width']=330
self.root.resizable(0,0)
self.lines = list() # ['abbreviation & 节略,指节约 & a short form of a word', ...]
self.items = StringVar(value=self.lines) # 列表型字符变量
self.initialize_datas() # 定义字符变量
labelEnglish =tkinter.Label(self.root,text='英文单词:',justify=tkinter.RIGHT,width=50)
labelEnglish.place(x=60,y=40,width=50,height=20)
self.input_word=tkinter.StringVar(self.root,value=' ')
self.entryinput_word=tkinter.Entry(self.root,width=120,textvariable=self.input_word)
self.entryinput_word.place(x=120,y=40,width=120,height=20)
labelChina=tkinter.Label(self.root,text='汉语:',justify=tkinter.RIGHT,width=50)
labelChina.place(x=60,y=80,width=50,height=20)
self.var_chinese=tkinter.StringVar(self.root,value=' ')
self.entryvar_chinese=tkinter.Entry(self.root,width=120,textvariable=self.var_chinese)
self.entryvar_chinese.place(x=120,y=80,width=120,height=20)
labelExplain=tkinter.Label(self.root,text='英语解释',justify=tkinter.RIGHT,width=50)
labelExplain.place(x=60,y=120,width=50,height=20)
self.var_english=tkinter.StringVar(self.root,value=' ')
self.entryvar_english=tkinter.Entry(self.root,width=120,textvariable=self.var_english)
self.entryvar_english.place(x=120,y=120,width=120,height=20)
buttonQuery=tkinter.Button(self.root,text='查询',width=40,command=self.query)
buttonQuery.place(x=40,y=190,width=40,height=20)
buttonAdd=tkinter.Button(self.root,text='添加',width=40,command=self.add)
buttonAdd.place(x=100,y=190,width=40,height=20)
buttonModify=tkinter.Button(self.root,text='修改',width=40,command=self.modify)
buttonModify.place(x=160,y=190,width=40,height=20)
buttonDelete=tkinter.Button(self.root,text='删除',width=100,command=self.delete)
buttonDelete.place(x=220,y=190,width=40,height=20)
self.listboxPic=tkinter.Listbox(self.root,width=200)
self.listboxPic.place(x=60,y=250,width=200,height=200)
self.listboxPic.bind('' ,self.select)
self.shoice=None
self.root.mainloop()
#图形界面
def initialize_datas(self):
with open('dictionary.txt', 'r', encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
line = line.strip() # 去掉换行符
self.lines.append(line)
def query(self):
"""根据 英文单词 输入框的内容来查询词典"""
input_word = self.input_word.get().strip()
if not input_word:
tkinter.messagebox.showinfo('提示','查询的单词不能为空哦!') # 弹窗提示
return
words = [line.split(' & ')[0] for line in self.lines] # 获取英文单词列表
if input_word not in words:
tkinter.messagebox.showinfo('提示','词典中尚未录入该单词!') # 弹窗提示
return
for line in self.lines:
word, chinese, english = line.strip().split(' & ')
if word == input_word:
self.var_chinese.set(chinese)
self.var_english.set(english)
def select(self,*args):
line = self.listbox.get(self.listbox.curselection()) # 获取列表中选定的一行词典内容
word,chinese,english=line.split('&') # 拆分出英文单词,中文释义,英文释义
self.input_word.set(word)
self.var_chinese.set(chinese)
self.var_english.set(english)
def add(self):
input_word = self.input_word.get().strip()
chinese = self.var_chinese.get()
english = self.var_english.get()
if (not input_word) or (not chinese) or (not english):
tkinter.messagebox.showinfo( '新增不能为空')
return
words = [line.split(' & ')[0] for line in self.lines] # 获取英文单词列表
if input_word in words:
tkinter.messagebox.showinfo( '词典中已录入该单词,不能重复录入')
return
new_line = f"{input_word} & {chinese} & {english}"
self.lines.append(new_line)
self.items.set(self.lines) # 刷新列表型字符变量的值,以更新列表盒子的显示内容
messagebox.showinfo( '新增成功!')
def modify(self):
input_word = self.input_word.get().strip()
if not input_word:
tkinter.messagebox.showinfo('修改的单词不能为空')
return
words = [line.split(' & ')[0] for line in self.lines]
if input_word not in words:
tkinter.messagebox.showinfo('词典中尚未录入该单词,需先录入后才能做修改')
return
for index, line in enumerate(self.lines):
if line.split(' & ')[0] == input_word:
chinese = self.var_chinese.get()
english = self.var_english.get()
new_line = f"{input_word} & {chinese} & {english}"
self.lines[index] = new_line
self.items.set(self.lines) # 刷新列表型字符变量的值,以更新列表盒子的显示内容
messagebox.showinfo('修改成功!')
def delete(self):
"""从词库中删除一条单词记录"""
input_word = self.input_word.get().strip()
if not input_word:
tkinter.messagebox.showinfo( '删除的单词不能为空')
return
words = [line.split(' & ')[0] for line in self.lines]
if input_word not in words:
tkinter.messagebox.showinfo( '词典中尚未录入该单词,只能删除已录入的单词')
return
for index, line in enumerate(self.lines):
if line.split(' & ')[0] == input_word:
del self.lines[index]
self.items.set(self.lines) # 刷新列表型字符变量的值,以更新列表盒子的显示内容
messagebox.showinfo( '删除成功')
if __name__=='__main__':
dictionary=Pic
望采纳!!点击该回答右侧的“采纳”按钮即可采纳!!!对的,这个程序里只有一个类,名为 Pic
是的~一个class代表一个类
class代表类,缩进后的def是类里面的方法