Use Tkinter instead of easygui for the settings dialog.

This commit is contained in:
Anna Rose 2016-01-23 22:14:41 -05:00
parent 5b959aa87f
commit c93cf8e1e6
2 changed files with 73 additions and 41 deletions

View File

@ -6,7 +6,6 @@ from elite_api import companion
from elite_api.inara import InaraSession
import Tkinter as tk
import utils
import easygui
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("--no-gui",
@ -26,23 +25,24 @@ def update_inara(inara_session):
inara_session.update_location(data['lastSystem']['name'])
class UpdateWindow:
class UpdateWindow(object):
def __init__(self, parent, settings):
frame = tk.Frame(parent)
frame.pack(expand=True, fill=tk.BOTH)
self.parent = parent
self.frame = tk.Frame(parent)
self.frame.pack(expand=True, fill=tk.BOTH)
self.message = tk.StringVar()
self.message.set("Click Update to update!")
message_label = tk.Label(frame, textvariable=self.message)
message_label.grid(columnspan=3, padx=20, pady=20)
message_label = tk.Label(self.frame, textvariable=self.message)
message_label.grid(columnspan=2, padx=20, pady=20)
self.update_button = tk.Button(frame, text="Update", height=2, width=4,
self.update_button = tk.Button(self.frame, text="Update", height=2, width=4,
command=self._update_inara)
self.update_button.grid(row=1, column=0, columnspan=2, pady=10)
self.update_button.grid(row=1, column=0, pady=10)
config_button = tk.Button(frame, text="Config", height=1, width=2,
command=utils.init_settings)
config_button.grid(row=1, column=2)
config_button = tk.Button(self.frame, text="Config", height=1, width=2,
command=lambda: utils.update_settings(True, parent, settings))
config_button.grid(row=1, column=1, sticky=tk.E+tk.S, padx=5, pady=5)
try:
self.session = do_logins(settings)
@ -52,6 +52,7 @@ class UpdateWindow:
def _update_inara(self):
self.message.set("Updating, please wait...")
self.parent.update()
update_inara(self.session)
self.message.set("Update successful! (Last update: %s)" %
datetime.now().isoformat(' ')[:16])
@ -60,14 +61,14 @@ class UpdateWindow:
def main():
args = arg_parser.parse_args()
settings = utils.get_settings(args.gui)
if args.gui:
root = tk.Tk()
root.wm_title("Inara Updater")
settings = utils.get_settings(True, root)
app = UpdateWindow(root, settings)
root.mainloop()
else:
settings = utils.get_settings(False)
inara_session = do_logins(settings)
update_inara(inara_session)
print("Inara updated!")

View File

@ -1,6 +1,7 @@
from ConfigParser import ConfigParser
import os
import easygui
import Tkinter as tk
import tkSimpleDialog, tkMessageBox
import platform
def get_config_dir(make=False):
@ -11,7 +12,7 @@ def get_config_dir(make=False):
return os.path.join(os.path.expanduser('~'), config_suffix)
def get_settings(use_gui=True):
def get_settings(use_gui=True, parent=None):
"""
Try to read the settings from file into ConfigParser object.
If the config file isn't found, initialize it.
@ -27,16 +28,18 @@ def get_settings(use_gui=True):
except:
pass
settings = init_settings(use_gui)
settings = update_settings(use_gui, parent)
return settings
def init_settings(gui=True):
settings = ConfigParser()
settings.add_section('ed_companion')
settings.add_section('inara')
def update_settings(gui=True, parent=None, settings=None):
if settings is None:
settings = ConfigParser()
settings.add_section('ed_companion')
settings.add_section('inara')
if gui:
_settings_prompt_gui(settings)
dialog = ConfigDialog(parent, settings)
else:
_settings_prompt_cli(settings)
print "To change these settings later, edit " + filename
@ -46,29 +49,57 @@ def init_settings(gui=True):
return settings
def _settings_prompt_gui(settings):
data = []
data = easygui.multenterbox(
"Enter your E:D and Inara credentials. You only need to do this once.",
"Authentication Data",
["Elite Username (email address)", "Elite Password",
"Inara Username", "Inara Password"]
)
for i in range(4):
if data[i].strip() == '':
easygui.msgbox("You must provide data for all fields.")
_settings_prompt_gui(settings)
return
settings.set('ed_companion', 'username', data[0].strip())
settings.set('ed_companion', 'password', data[1].strip())
settings.set('inara', 'username', data[2].strip())
settings.set('inara', 'password', data[3].strip())
def _settings_prompt_cli(settings):
settings.set('ed_companion', 'username', raw_input("Elite Username (email address): "))
settings.set('ed_companion', 'password', raw_input("Elite Password: "))
settings.set('inara', 'username', raw_input("Inara Username: "))
settings.set('inara', 'password', raw_input("Inara Password: "))
class ConfigDialog(tkSimpleDialog.Dialog):
def __init__(self, parent, settings, title="Authentication Data"):
self.settings = settings
self.entries = []
self.data = []
tkSimpleDialog.Dialog.__init__(self, parent, title)
def body(self, parent):
i = 0
values = []
for section, value in (('ed_companion', 'username'),
('ed_companion', 'password'),
('inara', 'username'),
('inara', 'password')):
if self.settings.has_option(section, value):
values.append(self.settings.get(section, value))
else:
values.append("")
for field in ("Elite Username (email address):",
"Elite Password:",
"Inara Username:",
"Inara Password:"):
label = tk.Label(parent, text=field)
label.grid(row=i, column=0, sticky=tk.W)
entry = tk.Entry(parent, width=30)
entry.insert(0, values[i])
entry.grid(row=i, column=1, sticky=tk.E)
self.entries.append(entry)
i += 1
return self.entries[0]
def validate(self):
for entry in self.entries:
if entry.get().strip() == "":
tkMessageBox.showwarning("Missing Data",
"You must provide a value for every field.")
return False
return True
def apply(self):
self.settings.set('ed_companion', 'username', self.entries[0].get().strip())
self.settings.set('ed_companion', 'password', self.entries[1].get().strip())
self.settings.set('inara', 'username', self.entries[2].get().strip())
self.settings.set('inara', 'password', self.entries[3].get().strip())