-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
219 lines (171 loc) · 7.71 KB
/
main.py
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from Tkinter import *
from thesaurus import Thesaurus
from tkFileDialog import askopenfilename
from tkFileDialog import asksaveasfilename
import tkSimpleDialog
class MyApp():
"""Class for a GUI """
def __init__(self, parent,thes=""):
""" initialize the GUI with all visible elements and menus """
#self.MyParent of MyApp
self.MyParent = parent
# import a thesaurus if given as an argument or create an empty one
if thes is "":
self.t1=Thesaurus("Neuer Thesaurus")
else:
self.t1=thes
self.MyParent.title("uberthesaurus - %s"% self.t1.name)
# scrollbars for the listboxes
self.scrollbar1 = Scrollbar(self.MyParent, orient=VERTICAL)
self.scrollbar2 = Scrollbar(self.MyParent, orient=VERTICAL)
# 2 listboxes for des and terms
self.deslistbox = Listbox(self.MyParent, yscrollcommand=self.scrollbar1.set, exportselection=0)
self.termlistbox = Listbox(self.MyParent, yscrollcommand=self.scrollbar2.set, exportselection=0)
#self.termlistbox.bind("<<Double-Button-1>>", lambda event:self.deslistbox.select_set())
self.deslistbox.bind("<<ListboxSelect>>", lambda event: self.update_tlist())
# a frame for changing elements
self.myContainer1 = Frame(self.MyParent)
# add scrollbars for the listboxes
self.scrollbar1.config(command=self.deslistbox.yview)
self.scrollbar2.config(command=self.termlistbox.yview)
#add buttons for interaktion with des
self.add1_button=Button(self.MyParent, text='Hinzufuegen', command=self.add_des, width=10)
self.edit1_button=Button(self.MyParent, text='Bearbeiten', command=self.edit_des, width=10)
self.del1_button=Button(self.MyParent, text="Loeschen", command=self.del_des, width=10)
#add buttons for interaktion with terms
self.add2_button=Button(self.MyParent, text='Hinzufuegen', command=self.add_term, width=10)
self.edit2_button=Button(self.MyParent, text='Bearbeiten', command=self.edit_term, width=10)
self.del2_button=Button(self.MyParent, text="Loeschen", command=self.del_term, width=10)
# confige the spacing
self.MyParent.columnconfigure(1, weight=0)
self.MyParent.columnconfigure(1, pad=0)
self.MyParent.columnconfigure(2, pad=7)
self.MyParent.columnconfigure(3, pad=7)
self.MyParent.rowconfigure(1, weight=0)
self.MyParent.rowconfigure(2, weight=0)
self.MyParent.rowconfigure(3, weight=1)
# place all GUI-elements
self.add1_button.grid(row=1, column=0, pady=2, sticky=NW)
self.edit1_button.grid(row=2, column=0, pady=2, sticky=NW)
self.del1_button.grid(row=3, column=0, pady=2, sticky=NW)
self.deslistbox.grid(row=1, column=1, rowspan=3,pady=5, sticky=NS)
self.scrollbar1.grid(row=1, column=2, rowspan=3, pady=5, sticky=NS)
self.termlistbox.grid(row=1,column=3, rowspan=3, pady=5, sticky=NS)
self.scrollbar2.grid(row=1,column=4, rowspan=3, pady=5, sticky=NS)
self.add2_button.grid(row=1,column=5, pady=2, sticky=NW)
self.edit2_button.grid(row=2,column=5, pady=2, sticky=NW)
self.del2_button.grid(row=3,column=5, pady=2, sticky=NW)
# self.myContainer1.grid()
# Menu
self.menu = Menu(self.MyParent)
self.MyParent.config(menu=self.menu)
self.filemenu = Menu(self.menu)
self.menu.add_cascade(label="Datei", menu=self.filemenu)
# Main Menu
self.filemenu.add_command(label="Neu", command=self.new_thes)
self.filemenu.add_command(label="Verbinden", command=self.t1.connect)
self.filemenu.add_command(label="Import", command=self.importdatei)
self.filemenu.add_command(label="Export", command=self.export)
self.filemenu.add_command(label="Schliessen", command=self.exit_prog)
def update_dlist(self):
""" Updates the listbox for the descriptors"""
self.deslistbox.delete(0, END)
for elem in sorted(self.t1.entries.keys()):
self.deslistbox.insert(END, elem)
def update_tlist(self):
""" Updates the listbox for the relations and terms"""
if self.t1.entries!={}:
if self.deslistbox.curselection()!=():
tlist=self.t1.entries[self.deslistbox.get(self.deslistbox.curselection())].get_terms()
else:
tlist=self.t1.entries[self.deslistbox.get(0)].get_terms()
self.termlistbox.delete(0, END)
for key,value in sorted(tlist.iteritems()):
for elem in value:
self.termlistbox.insert(END, key + " "+elem)
else:
self.termlistbox.delete(0, END)
def del_des(self):
""" Deletes the selected element of the listbox for the descriptors"""
if self.deslistbox.curselection() != ():
self.t1.delete_entries(self.deslistbox.get(self.deslistbox.curselection()))
self.update_dlist()
self.update_tlist()
def add_des(self):
""" Deletes the selected element of the listbox for the relations and terms"""
self.des = tkSimpleDialog.askstring("Deskriptor hinzufuegen", "Deskriptor:")
if self.des is not None:
self.t1.create_entries(self.des)
self.update_dlist()
self.update_tlist()
def edit_des(self):
"""Opens up a dialog for descriptor editing"""
self.des = tkSimpleDialog.askstring("Deskriptor bearbeiten", "Bearbeiten:")
if self.des is not None:
self.t1.edit_entries(self.deslistbox.get(self.deslistbox.curselection()),self.des)
self.update_dlist()
self.update_tlist()
def del_term(self):
""" Deletes the selected term from the termlist """
if self.termlistbox.curselection() != ():
self.term=self.termlistbox.get(self.termlistbox.curselection())
self.term=self.term.split(" ")
self.t1.entries[self.deslistbox.get(self.deslistbox.curselection())].remove_term(self.term[0],self.term[1])
self.update_tlist()
def add_term(self):
"""Opens up a dialog for term adding"""
self.term = tkSimpleDialog.askstring("Term hinzufuegen", "Rel Term:")
if self.term is not None:
self.term=self.term.split(" ")
self.t1.add(self.deslistbox.get(self.deslistbox.curselection()), self.term[1], self.term[0])
self.update_dlist()
self.update_tlist()
def edit_term(self):
"""Opens up a dialog for term/rel editing"""
self.rel_term=tkSimpleDialog.askstring("Term bearbeiten", "Rel Term")
if self.rel_term is not None:
self.rel_term=self.rel_term.split(" ")
self.rel_term_old=self.termlistbox.get(self.termlistbox.curselection()).split(" ")
if self.rel_term[0]!=self.rel_term_old[0]:
self.t1.entries[self.deslistbox.get(self.deslistbox.curselection())].edit_rel(str(self.rel_term_old[0]), str(self.rel_term_old[1]), str(self.rel_term[0]))
if self.rel_term[1]!=self.rel_term_old[1]:
self.t1.entries[self.deslistbox.get(self.deslistbox.curselection())].edit_term(str(self.rel_term_old[0]), str(self.rel_term_old[1]), str(self.rel_term[1]))
self.update_dlist()
self.update_tlist()
def exit_prog(self):
"""Exits the program"""
self.MyParent.destroy()
def new_thes(self):
"""Clears all entries of the thesaurus"""
self.t1.entries={}
self.update_dlist()
self.update_tlist()
self.t1.name="Neuer Thesaurus"
self.MyParent.title("uberthesaurus - %s"% self.t1.name)
def export(self):
"""Extracts the filetype and calls the real export method if a valid filename is given"""
self.formats = [
('Comma-separated values','*.csv'),
('JavaScript Object Notation','*.json'),
('Extensible Markup Language','*.xml'),
]
self.filename = asksaveasfilename(filetypes=self.formats, title="Den Thesaurus exportieren", defaultextension=".xml")
if len(self.filename)>0:
self.t1.export_thesaurus(self.filename)
self.MyParent.title("uberthesaurus - %s"% self.t1.name)
else:
print "Keine Datei angegeben."
def importdatei(self):
"""Extracts the filetype and calls the real import method if a valid filename is given"""
self.filename = askopenfilename()
if len(self.filename)>0:
self.t1.import_thesaurus(self.filename)
self.update_dlist()
self.MyParent.title("uberthesaurus - %s"% self.t1.name)
else:
print "Keine Datei angegeben."
if __name__ == '__main__':
#t1=Thesaurus("Neuer Thesaurus")
root= Tk()
myapp = MyApp(root)#,t1)
root.mainloop()