1. NodeBox 1
    1. Homepage
    2. NodeBox 3Node-based app for generative design and data visualization
    3. NodeBox OpenGLHardware-accelerated cross-platform graphics library
    4. NodeBox 1Generate 2D visuals using Python code (Mac OS X only)
  2. Gallery
  3. Documentation
  4. Forum
  5. Blog

EasyDialogs or Tkinter or ?

Posted by Sheng-Wu Chang on Jan 15, 2010

#==========================================================================================
graph = ximport("graph")
print "Starting..."
g = graph.create()
#==========================================================================================
def Add_node():
    import EasyDialogs
    Name = EasyDialogs.AskString('New Node:', default='hello')
    print Name
    g.add_node(Name)
#==========================================================================================
# Define initial nodes and edges
g.add_node("jos")
g.add_node("jef")
g.add_edge("jef", "jos")
g.add_edge("jos", "louis")
g.add_edge("jef", "louis")
g.add_edge("jef", "francois")
g.add_edge("francois", "emile")
g.add_edge("francois", "jean-pierre")
g.add_node("franske")
#==========================================================================================
# METHODS
_add = False
# Draw the graph in its current state
def draw():
    g.draw()
    x = 20; y = 20
    fill(color(1, 1, 0))
    global _add
    if mousedown:
        if x < MOUSEX and MOUSEX < x+30 and y < MOUSEY and MOUSEY < y+20:
            _add = True
            fill(color(0, 1, 1))
    elif _add == True:
        Add_node()
        _add = False
    rect(x, y, 30, 20, 1)
    fill(color(1, 0, 1))
    text("ADD", x+5, y+15)
#==========================================================================================    
size(500, 500)
speed(30)
#===========================================================================
And you have to do one extra step:
cp -p -R /System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac [path to NodeBox]/NodeBox.app/Contents/Resources/lib/python2.5/

Above codes are adapted from http://nodebox.net/code/index.php/shared_2009-06-08-23-54-10
ps. I am using NodeBox 1.9.5 on Mac OS X 10.5

I try to find ways to do same effect as var under animation mode. I am wondering if anyone knows how could I integrate with Tkinter smoothly since it provides more controls (ex. Combo) ? or NSObject api calls ?


 
Posted by Sheng-Wu Chang on Jan 18, 2010

from AppKit import *
class ComboBox(NSObject):
    def init(self, title="Label", list=[], default=""):
        r = NSMakeRect(150, 150, 150, 100)
        p = NSPanel.alloc().initWithContentRect_styleMask_backing_defer_(r, NSTitledWindowMask, NSBackingStoreBuffered, False)
        r = NSMakeRect(25, 60, 100, 25)
        t = NSTextField.alloc().initWithFrame_(r);
        t.setStringValue_(title)
        t.setEditable_(False)
        t.setSelectable_(False)
        t.setDrawsBackground_(False)
        t.setBezeled_(False)
        p.contentView().addSubview_(t)
        r = NSMakeRect(25, 25, 100, 25)
        c = NSComboBox.alloc().initWithFrame_(r);
        for edge in list:
            c.addItemWithObjectValue_(edge)
        c.setStringValue_(default)
        c.setTarget_(self)
        c.setAction_(self.hit)
        p.contentView().addSubview_(c)
 
        self.k = NSApp().keyWindow()
        NSApp().runModalForWindow_(p)
 
    def hit(self, sender):
        self.value = sender.stringValue()
        NSApp().stopModal()
        self.k.makeKeyWindow()
 
"""
usage:
import ns
...
                        if self.current.find("Edge") >= 0:
                            self.node2 = EasyDialogs.AskString("Node2", default="[Node2]")
                            c = ns.ComboBox.alloc()
                            c.init(title="Edge", list=["type A", "type B", "type C", "type D"], default="type B")
                            print "Edge:", c.value
                            del c
...
 
"""
I found finally this way helps me to dynamically customize control(s) what I want by leveraging Cocoa NSObject. Of course Carbon's EasyDialogs works somewhere, too.