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

Buttons and images

Posted by Judy on Nov 21, 2008

Hi! I'm wondering if anyone can help me with some Button functions. I want to press buttons and have text appear one the screen. Can anyone help me make that happen? The buttons are jpg images.
Thank you!!


 
Posted by Stefan G on Nov 24, 2008

Hello, maybe this code can be of help for you:

class ImageButton:
    def __init__(self, img, txt="", x=0, y=0, width=None, height=None):
        self.img = img
        self.text = txt
        self.pressed = False
        
        self.x = x
        self.y = y
        
        if width is None or height is None:
            sz = imagesize(img)
            self.width = int(sz.width)
            self.height = int(sz.height)
        else:
            self.width = width
            self.height = height
        
    def contains(self, x, y):
        return rect(self.x, self.y, self.width, self.height, draw=False).contains(x, y)
 
    def draw(self):
        offset = self.pressed and 1 or 0
        image(self.img, self.x + offset, self.y + offset, self.width, self.height)
 
buttons = []
buttons.append(ImageButton("/path/to/image1.jpg", "this", 10, 10))
buttons.append(ImageButton("/path/to/image2.jpg", "is", 75, 10))
buttons.append(ImageButton("/path/to/image3.jpg", "some", 140,10))
buttons.append(ImageButton("/path/to/image4.jpg", "text", 205, 10))
 
x = None
y = None
pressed = False
_pressed = False
txt = ""
 
speed(100)
 
def mouse():
    return (MOUSEX, MOUSEY)
 
def isdragging(x1, y1):
    return mousedown and \
        x is not None and \
        y is not None and \
        (x != x1 or y != y1)
 
def ispressed():
    global pressed, _pressed
    if mousedown:
        waspressed = pressed
        pressed = _pressed = True
        return mousedown and not waspressed
    else:
        pressed = False
    return False
 
def isreleased():
    global _pressed
    if not mousedown and _pressed:
        _pressed = False
        return True
    return False
 
button = None
            
def draw():
    global x, y, buttons, button, txt
 
    x1, y1 = mouse()
 
    pressed__ = ispressed()
    released__ = isreleased()
 
    if released__ and button is not None:
        button.pressed = False
        if button.contains(x1, y1):
            txt = button.text
            button = None
    elif pressed__:
        for r in buttons:
            if r.contains(x1, y1):
                button = r
                button.pressed = True
                
    for r in buttons:
        r.draw()
        
    text(txt, 10,80)



Posted by Judy on Dec 05, 2008

Thanks! One more thing, how do you add a color box for the background?



Posted by Judy on Dec 05, 2008

Oh, another thing, is it possible to make different text appear every time you push the same button? Thanks!