From 1f0e1cc7ba619393dbaba49db8157851cd751414 Mon Sep 17 00:00:00 2001 From: Russell Standish Date: Fri, 4 Oct 2024 17:59:06 +1000 Subject: [PATCH] Added a start of an object browser. --- lib/objectBrowser.py | 49 ++++++++++++++++++++++++++++++++++++++ models/panmictic_ecolab.py | 3 +++ 2 files changed, 52 insertions(+) create mode 100644 lib/objectBrowser.py diff --git a/lib/objectBrowser.py b/lib/objectBrowser.py new file mode 100644 index 0000000..26392a8 --- /dev/null +++ b/lib/objectBrowser.py @@ -0,0 +1,49 @@ +import ecolab +from tkinter import Tk,ttk,Listbox + +# executed when mouse clicked +def doMethod(event): + listbox=event.widget + selectedItems=listbox.curselection() + if len(selectedItems)==0: return + index=selectedItems[0] + selection=listbox.get(index) + if selection[-1]=="▶": + # browse the compound object + member=getattr(listbox.object,selection[:-1]) + Browser(member,selection[:-1]) + else: + selection=selection.split('=')[0] + val=getattr(listbox.object,selection)() + listbox.delete(index) + listbox.insert(index,selection+'='+str(val)) + listbox.itemconfigure(index,foreground='blue') + +class Browser: + def __init__(self, object, title=''): + if str(type(object))!="": + raise RuntimeError("Cannot browse non-C++ wrapped objects") + + # separate attributes into members and methods + members=set() + methods=set() + for m in object._list(): + mm=m[1:].split('.') + if len(mm)==1: + methods.add(mm[0]) + else: + members.add(mm[0]+"▶") + + self.browser=Tk() + self.browser.wm_title(title) + listbox=Listbox(self.browser) + listbox.insert('end',*sorted(members)) + listbox.insert('end',*sorted(methods)) + for i in range(len(members)+len(methods)): + listbox.itemconfigure(i,foreground='red' if i',doMethod) + + diff --git a/models/panmictic_ecolab.py b/models/panmictic_ecolab.py index 9810193..c65ac29 100644 --- a/models/panmictic_ecolab.py +++ b/models/panmictic_ecolab.py @@ -49,6 +49,9 @@ def step(): statusBar.configure(text=f't={ecolab.tstep()} nsp:{nsp}') plot('No. species',ecolab.tstep(),nsp) plot('Density',ecolab.tstep(),ecolab.density(), pens=ecolab.species()) + +from objectBrowser import Browser +browser=Browser(ecolab,'ecolab') gui(step)