forked from bislara/code-n-stitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
177 lines (125 loc) · 4.91 KB
/
gui.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
import tkinter as tk
from tkinter import ttk
from PIL import ImageTk
from main import DETAILS
from functions import get_user_details, insert_new_line, get_user_image
info_rows = DETAILS[1:]
class Header(tk.Frame):
def __init__(self, parent, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.img = tk.PhotoImage(file='github.png')
self.label_img = tk.Label(self, image=self.img, bg='white')
self.label_img.grid(row=0, column=0, padx=10, pady=10)
font = ('Helvetica', 12, 'bold')
self.title = tk.Label(
self, text='Github User Details', bg='white', font=font
)
self.title.grid(row=0, column=1, sticky=tk.W)
class SearchBar(tk.Frame):
def __init__(self, parent, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.parent = parent
self.label = tk.Label(self, text='username', font='TkFixedFont')
self.label.grid(row=0, column=0)
style = ttk.Style()
style.configure('TEntry', padding=4)
self.username = tk.StringVar()
self.input = ttk.Entry(self, textvariable=self.username)
self.input.grid(row=0, column=1, padx=5)
self.input.focus()
self.img = tk.PhotoImage(name='search', file='search.png')
self.button = ttk.Button(
self, image=self.img, text='Search', command=self.search
)
self.button.grid(row=0, column=2)
self.bind_all('<Return>', lambda e: self.search())
def search(self):
return self.parent.search(self.username.get())
class UserContent(tk.Frame):
def __init__(self, parent, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.img_label = tk.Label(self, text='')
self.img_label.grid(
row=0, column=0, rowspan=len(info_rows), padx=10, pady=10
)
self.labels = {}
self.string_vars = {}
self.string_labels = {}
for idx, (key, label) in enumerate(info_rows):
l = tk.Label(self, text=label)
l.grid(row=idx, column=1, sticky=tk.W)
sv = tk.StringVar()
sl = tk.Label(self, textvariable=sv)
sl.grid_configure(row=idx, column=2, sticky=tk.W)
self.labels[key] = l
self.string_vars[key] = sv
self.string_labels[key] = sl
def update(self, user):
img = get_user_image(user['avatar_url']).resize((230, 230))
user_img = ImageTk.PhotoImage(img)
self.img_label.configure(image=user_img)
self.img_label.image = user_img
for idx, (key, label) in enumerate(info_rows):
text = user[key] if user[key] is not None else '(not provided)'
if key != 'bio':
self.string_vars[key].set(text)
else:
self.string_vars[key].set(insert_new_line(text))
class ErrorFrame(tk.Frame):
def __init__(self, parent, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.label = tk.Label(
self, text='Invalid User', fg='red', font=('Helvetica', 14, 'bold')
)
self.label.grid()
class Body(tk.Frame):
def __init__(self, parent, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.user_frame = UserContent(self)
self.error_frame = ErrorFrame(self)
def display_user(self, data):
self.user_frame.update(data)
self.user_frame.pack(fill='x', pady=10, padx=5)
def display_error(self):
self.error_frame.pack(pady=20)
def hide(self):
self.user_frame.pack_forget()
self.error_frame.pack_forget()
class Footer(tk.Frame):
def __init__(self, parent, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.parent = parent
self.button = ttk.Button(
self, text='Close', command=parent.close
)
self.button.grid(row=0, column=0)
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.parent = parent
self.header = Header(self, bg='white')
self.header.pack(fill='x')
self.searchbar = SearchBar(self)
self.searchbar.pack(anchor='ne', pady=10, padx=5)
self.body = Body(self)
self.body.pack(fill='x', pady=10, padx=5)
self.footer = Footer(self)
self.footer.pack(anchor='se', expand=True, pady=5, padx=5)
def close(self):
return self.parent.destroy()
def search(self, username):
self.body.hide()
user = get_user_details(username)
if user:
self.body.display_user(user)
else:
self.body.display_error()
def run_app():
root = tk.Tk()
app = MainApplication(root)
app.pack(side='top', fill='both', expand=True)
root.title('Github User Details')
root.minsize(500, 200)
root.mainloop()
if __name__ == '__main__':
run_app()