I am trying to use an event box with a label inside to create a custom button. I found an example online to create a button using an event box but when I tried to load the event box button into a VBox I ran into errors. Does anyone see the error in my attempts? I am new to GTK. Thanks. Also, i have a callback function that I would like to attach to this button instead of the quit function. I plan on having several of these buttons and would like the callback for testing.

#!/usr/bin/env python

# example eventbox.py

import pygtk
pygtk.require('2.0')
import gtk

class EventBoxExample:
    
    def callback(self, widget, data=None):
        # Our callback.
        # The data passed to this method is printed to stdout
        print "Hello again - %s was pressed" % data
        
    def __init__(self):
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_title("Event Box")
        window.connect("destroy", lambda w: gtk.main_quit())
        window.set_border_width(10)
        
        main_box = gtk.VBox(True)
        main_box.show()

        # Create an EventBox and add it to our toplevel window
        event_box = gtk.EventBox()
        main_box.add(event_box)
        event_box.show()

        # Create a label
        label = gtk.Label("Exit")
        event_box.add(label)
        label.show()

        # And bind an action to it
        event_box.set_events(gtk.gdk.BUTTON_PRESS_MASK)
        event_box.connect("button_press_event", lambda w,e: gtk.main_quit())
        
        # This is where the error seems to stem from
        
        event_box.realize()
        event_box.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.HAND1))

        # Set background color to green
        event_box.modify_bg(gtk.STATE_NORMAL,
                            event_box.get_colormap().alloc_color("green"))

        window.add(main_box)
        window.show()

def main():
    gtk.main()
    return 0

if __name__ == "__main__":
    EventBoxExample()
    main()

Take a look at the first paragraph of the first hit here. You can just comment that line and keep the default cursor.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.