-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_detail_window.py
42 lines (37 loc) · 1.71 KB
/
api_detail_window.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
import tkinter as tk
import configparser
import os
script_dir = os.path.dirname(__file__)
config_file = os.path.join(script_dir, "config.ini")
class APIDetails(tk.Frame):
def __init__(self, parent, user=None, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.user = user
self.parent = parent
self.parent.protocol("WM_DELETE_WINDOW", self.on_closing)
self.label = tk.Label(self, text="Introduce your API details from your Habitica profile")
self.label.grid(row=0, column=0, columnspan=2)
tk.Label(self, text="User ID").grid(row=1, column=0)
tk.Label(self, text="API Key").grid(row=2, column=0)
self.user_entry = tk.Entry(self)
self.user_entry.grid(row=1, column=1)
self.key_entry = tk.Entry(self)
self.key_entry.grid(row=2, column=1)
tk.Button(self, text="Save details", command=self.on_button).grid(row=3, column=0, columnspan=2)
def on_button(self):
self.user.set_headers(self.user_entry.get(), self.key_entry.get())
success = self.user.update_profile()
if success:
config = configparser.ConfigParser()
config.read(config_file)
if not config.has_section("HabiticaAPI"):
config.add_section("HabiticaAPI")
config.set("HabiticaAPI", "UserID", self.user_entry.get())
config.set("HabiticaAPI", "APIKey", self.key_entry.get())
with open(config_file, 'w') as configfile:
config.write(configfile)
self.on_closing()
else:
self.label.config(text="Error: Your Habitica API details are incorrect", fg="red")
def on_closing(self):
self.parent.destroy()