-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pyw
69 lines (63 loc) · 2.97 KB
/
main.pyw
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
import PySimpleGUI as sg
from urban_dictionary import UrbanDictionary
# urban dictionary init
ud = UrbanDictionary()
# GUI layout
sg.theme("Dark Blue 3")
layout = [
[sg.Multiline(key="query", size=(101,1), tooltip="Type any word here...", pad=((5,5),(5,15))),
sg.Button("🔎", key="search", size=(6,2), font=(None,11), tooltip="Search", pad=((10,5),(0,7))),
sg.Button("🔁",key="random", size=(6,2), font=(None,11), tooltip="Random word", pad=((5,5),(0,7)))
],
[sg.Button("<<", key="previousPage", tooltip="previous page", size=(20,1), disabled = not ud.has_previous_page),
sg.Button("<", key="previousWord", tooltip="previous word", size=(20,1), disabled = not ud.has_previous_word),
sg.Button("⌂", key="homePage", tooltip="home page", size=(20,1), focus=True),
sg.Button(">", key="nextWord", tooltip="next word", size=(20,1), disabled = not ud.has_next_word),
sg.Button(">>", key="nextPage", tooltip="next page", size=(20,1), disabled = not ud.has_next_page),
],
[sg.Text(ud.word.name,key="name",font=(None,12,"bold"),size=(50,None), tooltip="word")],
[sg.Text(ud.word.meaning,key="meaning",size=(100,None), tooltip="meaning")],
[sg.Text(ud.word.example,key="example",font=(None,10,"italic"),size=(100,None), tooltip="example")],
[sg.Text(ud.word.contributor,key="contributor",size=(100,None), tooltip="contributor", pad=((5,5),(5,15)))]
]
window = sg.Window('Urban Dictionary', layout, finalize=True)
# gui logics
while True:
event, values = window.read()
# manage events
if event is None:
break
elif event == "random":
ud = UrbanDictionary(random=True)
window["query"].update("")
elif event == "search":
if values.get("query").replace("\n"," "):
query = UrbanDictionary(values.get("query").replace("\n"," "))
if query.word:
ud = query
else:
sg.popup("Sorry, we couldn't find: " + values.get('query').replace('\n', ' '), title="¯\\_(ツ)_/¯")
else:
sg.popup(f"Insert some text first", title="Error")
continue
elif event == "previousPage":
ud.go_to_previous_page()
elif event == "previousWord":
ud.go_to_previous_word()
elif event == "homePage":
ud = UrbanDictionary()
window["query"].update("")
elif event == "nextWord":
ud.go_to_next_word()
elif event == "nextPage":
ud.go_to_next_page()
# update UI elements
word = ud.word
window["name"].update(word.name)
window["meaning"].update(word.meaning)
window["example"].update(word.example)
window["contributor"].update(word.contributor)
window["previousPage"].update(disabled = not ud.has_previous_page)
window["previousWord"].update(disabled = not ud.has_previous_word)
window["nextWord"].update(disabled = not ud.has_next_word)
window["nextPage"].update(disabled = not ud.has_next_page)