Creates primary gui frame class
This commit is contained in:
1
copycat/gui/__init__.py
Normal file
1
copycat/gui/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from .gui import GUI
|
||||
102
copycat/gui/gui.py
Executable file
102
copycat/gui/gui.py
Executable file
@ -0,0 +1,102 @@
|
||||
import sys
|
||||
import time
|
||||
|
||||
import tkinter as tk
|
||||
import tkinter.ttk as ttk
|
||||
|
||||
from tkinter import scrolledtext
|
||||
from tkinter import filedialog
|
||||
|
||||
from .status import Status, StatusFrame
|
||||
from .primary import Primary
|
||||
|
||||
font1Size = 32
|
||||
font2Size = 16
|
||||
font1 = ('Helvetica', str(font1Size))
|
||||
font2 = ('Helvetica', str(font2Size))
|
||||
|
||||
style = dict(background='black',
|
||||
foreground='white',
|
||||
borderwidth=5,
|
||||
relief=tk.GROOVE,
|
||||
font=font2)
|
||||
|
||||
class MainApplication(ttk.Frame):
|
||||
|
||||
def __init__(self, parent, *args, **kwargs):
|
||||
ttk.Frame.__init__(self, parent, *args, **kwargs)
|
||||
self.widgets = dict()
|
||||
|
||||
self.parent = parent
|
||||
self.primary = Primary(self, *args, **kwargs)
|
||||
self.primary.grid(column=0, row=0, sticky=tk.N+tk.S+tk.E+tk.W)
|
||||
self.create_widgets()
|
||||
|
||||
def create_widgets(self):
|
||||
|
||||
tempLabel = ttk.Label(self, text='', **style, padding=30)
|
||||
#tempLabel.grid(column=1, row=0, sticky=tk.N+tk.S+tk.E+tk.W)
|
||||
self.widgets['temp'] = tempLabel
|
||||
|
||||
slipList = tk.Listbox(self, **style)
|
||||
slipList.grid(column=0, row=1, sticky=tk.N+tk.S+tk.E+tk.W)
|
||||
self.widgets['sliplist'] = slipList
|
||||
|
||||
codeletList = tk.Listbox(self, **style)
|
||||
codeletList.grid(column=1, row=1, sticky=tk.N+tk.S+tk.E+tk.W)
|
||||
self.widgets['codeletlist'] = codeletList
|
||||
|
||||
l = ttk.Label(self, text='temp', **style, padding=30)
|
||||
l.grid(column=2, row=1, sticky=tk.N+tk.S+tk.E+tk.W)
|
||||
|
||||
self.rowconfigure(0, weight=1)
|
||||
for i in range(4):
|
||||
self.columnconfigure(i, weight=1)
|
||||
|
||||
self.graph1 = Status()
|
||||
sframe = StatusFrame(self, self.graph1, 'graph 1')
|
||||
sframe.grid(column=1, row=0, sticky=tk.N+tk.S+tk.E+tk.W)
|
||||
self.graph2 = Status()
|
||||
sframe = StatusFrame(self, self.graph2, 'graph 2')
|
||||
sframe.grid(column=2, row=0, sticky=tk.N+tk.S+tk.E+tk.W)
|
||||
|
||||
def update(self, copycat):
|
||||
self.primary.update(copycat)
|
||||
temp = copycat.temperature.value()
|
||||
slipnodes = copycat.slipnet.slipnodes
|
||||
codelets = copycat.coderack.codelets
|
||||
|
||||
slipList = self.widgets['sliplist']
|
||||
slipList.delete(0, slipList.size())
|
||||
self.widgets['temp']['text'] = 'Temp:\n{}'.format(round(temp, 2))
|
||||
slipnodes = sorted(slipnodes, key=lambda s:s.activation, reverse=True)
|
||||
for item in slipnodes:
|
||||
listStr = '{}: {}'.format(item.name, round(item.activation, 2))
|
||||
slipList.insert(tk.END, listStr)
|
||||
|
||||
codeletList = self.widgets['codeletlist']
|
||||
codeletList.delete(0, codeletList.size())
|
||||
codelets = sorted(codelets, key=lambda c:c.urgency, reverse=True)
|
||||
for codelet in codelets:
|
||||
listStr = '{}: {}'.format(codelet.name, round(codelet.urgency, 2))
|
||||
codeletList.insert(tk.END, listStr)
|
||||
|
||||
class GUI(object):
|
||||
def __init__(self, title, updateInterval=.1):
|
||||
self.root = tk.Tk()
|
||||
self.root.title(title)
|
||||
tk.Grid.rowconfigure(self.root, 0, weight=1)
|
||||
tk.Grid.columnconfigure(self.root, 0, weight=1)
|
||||
self.app = MainApplication(self.root)
|
||||
self.app.grid(row=0, column=0, sticky=tk.N+tk.S+tk.E+tk.W)
|
||||
|
||||
self.lastUpdated = time.time()
|
||||
self.updateInterval = updateInterval
|
||||
|
||||
def update(self, copycat):
|
||||
self.root.update_idletasks()
|
||||
self.root.update()
|
||||
current = time.time()
|
||||
if current - self.lastUpdated > self.updateInterval:
|
||||
self.app.update(copycat)
|
||||
self.lastUpdated = current
|
||||
60
copycat/gui/primary.py
Normal file
60
copycat/gui/primary.py
Normal file
@ -0,0 +1,60 @@
|
||||
import tkinter as tk
|
||||
import tkinter.ttk as ttk
|
||||
|
||||
from tkinter import scrolledtext
|
||||
from tkinter import filedialog
|
||||
|
||||
font1Size = 32
|
||||
font2Size = 16
|
||||
font1 = ('Helvetica', str(font1Size))
|
||||
font2 = ('Helvetica', str(font2Size))
|
||||
|
||||
style = dict(background='black',
|
||||
foreground='white',
|
||||
borderwidth=5,
|
||||
relief=tk.GROOVE,
|
||||
font=font2)
|
||||
|
||||
def create_main_canvas(root, initial, final, new, guess):
|
||||
padding = 100
|
||||
|
||||
canvas = tk.Canvas(root, borderwidth=5, relief=tk.GROOVE, background='#70747a')
|
||||
|
||||
def add_sequences(sequences, x, y):
|
||||
for sequence in sequences:
|
||||
x += padding
|
||||
for char in sequence:
|
||||
canvas.create_text(x, y, text=char, anchor=tk.NW, font=font1, fill='white')
|
||||
x += font1Size
|
||||
return x, y
|
||||
|
||||
x = 0
|
||||
y = padding
|
||||
|
||||
add_sequences([initial, final], x, y)
|
||||
|
||||
x = 0
|
||||
y += padding
|
||||
|
||||
add_sequences([new, guess], x, y)
|
||||
|
||||
canvas['height'] = str(int(canvas['height']) + padding)
|
||||
canvas['width'] = str(int(canvas['width']) + padding)
|
||||
|
||||
return canvas
|
||||
|
||||
class Primary(ttk.Frame):
|
||||
|
||||
def __init__(self, parent, *args, **kwargs):
|
||||
ttk.Frame.__init__(self, parent, *args, **kwargs)
|
||||
|
||||
self.canvas = create_main_canvas(self, 'abc', 'abd', 'ijk', '?')
|
||||
self.canvas.grid(column=0, row=0, sticky=tk.N+tk.S+tk.E+tk.W)
|
||||
|
||||
self.rowconfigure(0, weight=1)
|
||||
self.columnconfigure(0, weight=1)
|
||||
|
||||
def update(self, copycat):
|
||||
answer = '' if copycat.workspace.rule is None else copycat.workspace.rule.buildTranslatedRule()
|
||||
self.canvas = create_main_canvas(self, 'abc', 'abd', 'ijk', answer)
|
||||
self.canvas.grid(column=0, row=0, sticky=tk.N+tk.S+tk.E+tk.W)
|
||||
60
copycat/gui/status.py
Normal file
60
copycat/gui/status.py
Normal file
@ -0,0 +1,60 @@
|
||||
import matplotlib
|
||||
matplotlib.use("TkAgg")
|
||||
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
|
||||
from matplotlib.figure import Figure
|
||||
|
||||
import tkinter as tk
|
||||
import tkinter.ttk as ttk
|
||||
|
||||
import time
|
||||
import matplotlib.animation as animation
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
plt.style.use('dark_background')
|
||||
|
||||
LARGE_FONT = ('Verdana', 20)
|
||||
|
||||
class StatusFrame(tk.Frame):
|
||||
def __init__(self, parent, status, title):
|
||||
self.status = status
|
||||
|
||||
tk.Frame.__init__(self, parent)
|
||||
label = tk.Label(self, text=title, font=LARGE_FONT)
|
||||
label.pack(pady=10,padx=10)
|
||||
|
||||
canvas = FigureCanvasTkAgg(status.figure, self)
|
||||
canvas.show()
|
||||
canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
|
||||
|
||||
toolbar = NavigationToolbar2TkAgg(canvas, self)
|
||||
toolbar.update()
|
||||
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
|
||||
|
||||
self.animation = animation.FuncAnimation(status.figure, lambda i : status.update_plots(i), interval=1000)
|
||||
|
||||
|
||||
class Status(object):
|
||||
def __init__(self):
|
||||
self.figure = Figure(figsize=(5,5), dpi=100)
|
||||
self.subplot = self.figure.add_subplot(111)
|
||||
self.x = []
|
||||
self.y = []
|
||||
|
||||
def update_plots(self, i):
|
||||
self.subplot.clear()
|
||||
self.subplot.plot(self.x, self.y)
|
||||
|
||||
if __name__ == '__main__':
|
||||
app = tk.Tk()
|
||||
status = Status()
|
||||
sframe = StatusFrame(app, status, 'x**2')
|
||||
sframe.pack()
|
||||
|
||||
i = 0
|
||||
while True:
|
||||
app.update()
|
||||
app.update_idletasks()
|
||||
time.sleep(.01)
|
||||
i += 1
|
||||
status.x += [i]
|
||||
status.y += [i**2]
|
||||
Reference in New Issue
Block a user