-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
51 lines (43 loc) · 1.42 KB
/
Copy pathmain.py
File metadata and controls
51 lines (43 loc) · 1.42 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
import json
import string
import webbrowser
import word_matrix as wma
from wikicrawler import get_link
import PySimpleGUI as sG
def create_window(matches=None):
head1 = [sG.Text("Welcome to ScienceSearch")]
head2 = [sG.Button("Search"), sG.Input('', key='Input1')]
layout = [head1, head2]
if matches is not None:
for name, link in matches:
layout.append([sG.Text(name, tooltip=link, enable_events=True, key=f'URL {link}')])
window = sG.Window('ScienceSearch', layout, finalize=True)
window['Input1'].bind("<Return>", "_Enter")
return window
def gui():
sG.theme('DarkGrey')
sG.set_options(font=("Montserrat", 16))
window = create_window()
while True:
event, values = window.read()
if event == "Search" or event == "Input1" + "_Enter":
text = values["Input1"]
matches = get_matches(text)
window.close()
window = create_window(matches)
if event == sG.WIN_CLOSED:
break
elif event.startswith("URL "):
url = event.split(' ')[1]
webbrowser.open(url)
window.close()
return
def get_matches(text: string):
best = wm.compare(text, 5)
return [(sites[i], get_link(sites[i])) for i in best]
if __name__ == '__main__':
wm = wma.WordMatrix()
wm.read()
with open('saved/sites.json', 'r') as read_file:
sites = json.load(read_file)
gui()