Adds interval-updated list widgets

This commit is contained in:
LSaldyt
2017-10-25 19:19:25 -07:00
parent cc35cb9de2
commit 8143259170
2 changed files with 38 additions and 32 deletions

View File

@ -12,6 +12,7 @@ import matplotlib.pyplot as plt
from .status import Status, StatusFrame from .status import Status, StatusFrame
from .gridframe import GridFrame from .gridframe import GridFrame
from .primary import Primary from .primary import Primary
from .list import List
from .plot import plot_imbedded from .plot import plot_imbedded
@ -37,8 +38,6 @@ class MainApplication(GridFrame):
self.create_widgets() self.create_widgets()
GridFrame.configure(self) GridFrame.configure(self)
self.iterations = 0
self.messages = [] self.messages = []
def log(self, message): def log(self, message):
@ -46,51 +45,34 @@ class MainApplication(GridFrame):
def create_widgets(self): def create_widgets(self):
self.slipList = tk.Listbox(self, **style, bd=0) self.slipList = List(self, 10, **style)
self.add(self.slipList, 0, 1) self.add(self.slipList, 0, 1)
self.codeletList = tk.Listbox(self, **style, bd=0) self.codeletList = List(self, 10, **style)
self.add(self.codeletList, 1, 1) self.add(self.codeletList, 1, 1)
self.objectList = tk.Listbox(self, **style, bd=0) self.objectList = List(self, 10, **style)
self.add(self.objectList, 2, 1) self.add(self.objectList, 2, 1)
self.logBox = tk.Label(self, text='', **style, bd=1) self.logBox = List(self, 10, **style)
self.add(self.logBox, 1, 0) self.add(self.logBox, 1, 0)
self.graph2 = Status() self.graph2 = Status()
sframe2 = StatusFrame(self, self.graph2, 'graph 2') sframe2 = StatusFrame(self, self.graph2, 'graph 2')
self.add(sframe2, 2, 0) self.add(sframe2, 2, 0)
def update(self, copycat): def update(self, copycat):
self.iterations += 1
self.primary.update(copycat) self.primary.update(copycat)
slipnodes = copycat.slipnet.slipnodes slipnodes = copycat.slipnet.slipnodes
codelets = copycat.coderack.codelets codelets = copycat.coderack.codelets
objects = copycat.workspace.objects objects = copycat.workspace.objects
self.slipList.delete(0, self.slipList.size()) self.slipList.update(slipnodes, key=lambda s:s.activation, reverse=True,
slipnodes = sorted(slipnodes, key=lambda s:s.activation, reverse=True) formatter=lambda s : '{}: {}'.format(s.name, round(s.activation, 2)))
for item in slipnodes: self.codeletList.update(codelets, key=lambda c:c.urgency, reverse=True, formatter= lambda s : '{}: {}'.format(s.name, round(s.urgency, 2)))
listStr = '{}: {}'.format(item.name, round(item.activation, 2)) self.objectList.update(objects)
self.slipList.insert(tk.END, listStr) self.logBox.update(list(reversed(self.messages))[:10])
self.codeletList.delete(0, self.codeletList.size())
codelets = sorted(codelets, key=lambda c:c.urgency, reverse=True)
for codelet in codelets:
listStr = '{}: {}'.format(codelet.name, round(codelet.urgency, 2))
self.codeletList.insert(tk.END, listStr)
self.objectList.delete(0, self.objectList.size())
#objects = sorted(objects, key=lambda c:c.urgency, reverse=True)
for o in objects:
#listStr = '{}: {}'.format(o.name, round(o.urgency, 2))
listStr = str(o)
self.objectList.insert(tk.END, listStr)
self.logBox['text'] = '\n'.join(list(reversed(self.messages))[:10])
if len(self.messages) > 10:
self.logBox['text'] += '\n...'
def reset_with_strings(self, initial, modified, target): def reset_with_strings(self, initial, modified, target):
self.primary.reset_with_strings(initial, modified, target) self.primary.reset_with_strings(initial, modified, target)
@ -119,6 +101,5 @@ class GUI(object):
def update(self, copycat): def update(self, copycat):
current = time.time() current = time.time()
if current - self.lastUpdated > self.updateInterval:
self.app.update(copycat) self.app.update(copycat)
self.lastUpdated = current self.lastUpdated = current

25
copycat/gui/list.py Normal file
View File

@ -0,0 +1,25 @@
import tkinter as tk
import time
from .gridframe import GridFrame
class List(GridFrame):
def __init__(self, parent, columns, updateInterval=.1, **style):
GridFrame.__init__(self, parent)
self.text = tk.Label(self, **style, anchor='w', justify=tk.LEFT, width=30)
self.add(self.text, 0, 0)
self.columns = columns
self.lastUpdated = time.time()
self.updateInterval = updateInterval
def update(self, l, key=None, reverse=False, formatter=lambda s : str(s)):
current = time.time()
if current - self.lastUpdated > self.updateInterval:
l = l[:self.columns]
if key is not None:
l = sorted(l, key=key, reverse=False)
self.text['text'] = '\n'.join(map(formatter, l))