-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDictionary_Add&Del.py
More file actions
92 lines (77 loc) · 3 KB
/
Dictionary_Add&Del.py
File metadata and controls
92 lines (77 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding:utf-8 -*-
from Tkinter import *
import os
import sys
import json
class GUIDemo(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.grid()
self.createWidgets()
global ewb, ewords
if not os.path.exists('./EnWordBase.json'):
print 'EnWordBase.json not found!'
sys.exit()
with open('EnWordBase.json', 'r') as file2:
ewb = json.load(file2)
file2.close()
ewords = ewb['words']
def search(self):
global ewb, ewords
w = self.inputField.get()
if w in ewords:
self.outputText["text"] = w + " is found in " + str(ewords.index(w)) + "th position"
else:
self.outputText["text"] = w + " not found"
def addword(self):
global ewb, ewords
w = str(self.inputField.get())
if w in ewords:
self.outputText["text"] = w + " is found in " + str(ewords.index(w)) + "th position"
else:
ewb['words'].append(w)
with open('EnWordBase.json', 'w') as file1:
json.dump(ewb, file1)
file1.close()
self.outputText["text"] = "database uploaded with " + w + " added"
def delete(self):
global ewb, ewords
w = self.inputField.get()
if w in ewords:
self.outputText["text"] = w + " is found in " + str(ewords.index(w)) + "th position"
ewb['words'].remove(w)
with open('EnWordBase.json', 'w') as file1:
json.dump(ewb, file1)
file1.close()
self.outputText["text"] = "database uploaded with " + w + " deleted"
else:
self.outputText["text"] = w + " not found"
def createWidgets(self):
self.inputText = Label(self)
self.inputText.grid(row=0, column=0)
self.inputField = Entry(self)
self.inputField["width"] = 50
self.inputField.grid(row=0, column=1, columnspan=5)
self.outputText = Label(self)
self.outputText["text"] = "Input the word you want to search or add."
self.outputText.grid(row=1, column=0, columnspan=7)
self.add = Button(self)
self.add["text"] = "Add"
self.add.grid(row=2, column=2)
self.add["command"] = self.addword
self.srh = Button(self)
self.srh["text"] = "Search"
self.srh.grid(row=2, column=3)
self.srh["command"] = self.search
self.dele = Button(self)
self.dele["text"] = "Delete"
self.dele.grid(row=2, column=4)
self.dele["command"] = self.delete
self.displayText = Label(self)
self.displayText["text"] = u"Copyright © 2017 Jexus Chuang. All rights reserved."
self.displayText.grid(row=3, column=0, columnspan=7)
if __name__ == '__main__':
root = Tk()
root.title(string="Dictionary Add & Delete System")
app = GUIDemo(master=root)
app.mainloop()