Skip to content

Commit

Permalink
Add Functioning GUI
Browse files Browse the repository at this point in the history
requires tkinter
  • Loading branch information
0dm committed May 11, 2023
1 parent 796e35a commit c2db59d
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
104 changes: 104 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import os
import sys
import time
import tkinter as tk
from tkinter import simpledialog
from PIL import Image, ImageTk
from openadapt import record, visualize, replay, console


class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("OpenAdapt Alpha 0.1.0")
self.geometry("600x400")
self.resizable(False, False)
self.image = tk.PhotoImage(file="assets/logo.png")
self.title_label = tk.Label(
self, text="OpenAdapt Alpha 0.1.0", image=self.image, compound="left"
)

# center window
self.update_idletasks()
width = self.winfo_width()
height = self.winfo_height()
x = (self.winfo_screenwidth() // 2) - (width // 2)
y = (self.winfo_screenheight() // 2) - (height // 2)
self.geometry("{}x{}+{}+{}".format(width, height, x, y))

# display logo
self.logo = ImageTk.PhotoImage(
Image.open("assets/logo.png").resize((128, 128), Image.LANCZOS)
)
self.logo_label = tk.Label(self, image=self.logo, compound="left")
self.logo_label.place(x=75, y=0, width=128, height=128)

# display log
self.log = console.Console(self)
self.log.place(x=300, y=0, width=300, height=400)

# add scrollbars
self.vscrollbar = tk.Scrollbar(self, orient="vertical", command=self.log.yview)
self.vscrollbar.place(x=590, y=6, width=10, height=390)

self.hscrollbar = tk.Scrollbar(
self, orient="horizontal", command=self.log.xview
)
self.hscrollbar.place(x=300, y=390, width=290, height=10)

self.log.config(
yscrollcommand=self.vscrollbar.set, xscrollcommand=self.hscrollbar.set
)

# add buttons
self.record_button = tk.Button(
self,
text="Record",
command=lambda: on_record(
simpledialog.askstring("Record", "Enter a name for the recording: ")
),
)

self.record_button.place(x=50, y=140, width=200, height=50)

self.visualize_button = tk.Button(
self, text="Visualize", command=lambda: visualize.main()
)
self.visualize_button.place(x=50, y=204, width=200, height=50)

self.replay_button = tk.Button(
self, text="Replay", command=lambda: replay.replay("NaiveReplayStrategy")
)
self.replay_button.place(x=50, y=268, width=200, height=50)

self.clear_cache_button = tk.Button(self, text="Clear Data", command=clear_db)
self.clear_cache_button.place(x=50, y=332, width=200, height=50)


def on_record(name):
if name is not None:
# fails
# record.record(name)

# works
time.sleep(1)
print(
"Recording {}... Press CTRL/CMD + C in log window to cancel".format(name),
file=sys.stderr,
)
os.system("python3 -m openadapt.record {}".format(name))
print("Recording complete.", file=sys.stderr)

else:
print("Recording cancelled.", file=sys.stderr)


def clear_db():
os.remove("openadapt.db")
os.system("alembic upgrade head")
print("Database cleared.", file=sys.stderr)


if __name__ == "__main__":
app = App()
app.mainloop()
Binary file added assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions openadapt/console.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import sys
from tkinter import Text


class Console(Text):
def __init__(self, *args, **kwargs):
kwargs.update({"state": "disabled"})
Text.__init__(self, *args, **kwargs)
self.bind("<Destroy>", self.reset)
self.old_stderr = sys.stderr
sys.stderr = self

def delete(self, *args, **kwargs):
self.config(state="normal")
self.delete(*args, **kwargs)
self.config(state="disabled")

def write(self, content):
self.config(state="normal")
self.insert("end", content)
self.config(state="disabled")
self.update()

def flush(self):
self.update()

def reset(self, event):
sys.stderr = self.old_stderr

0 comments on commit c2db59d

Please sign in to comment.