469,648 Members | 1,468 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,648 developers. It's quick & easy.

pygtk and custom widgets

8
I've been trying to write a custom widget for a project, however when i try to run code that uses it i get the following error

[sbox-SDK_PC: ~/code/nokia/trunk] > ./nokiagui.py
Traceback (most recent call last):
File "/home/jubei/code/nokia/trunk/nokiagui.py", line 427, in ?
gui = NewGui()
File "/home/jubei/code/nokia/trunk/nokiagui.py", line 193, in __init__
button = pkylib.ComboButton(("foo","bar","baz"))
File "/home/jubei/code/nokia/trunk/pkylib.py", line 21, in __init__
gtk.Widget.__init__(self)
TypeError: cannot create instance of abstract (non-instantiable) type `GtkWidget'
[sbox-SDK_PC: ~/code/nokia/trunk] >

and here is the code that i have written:

Expand|Select|Wrap|Line Numbers
  1. import gobject
  2. import gtk
  3.  
  4. from gtk import gdk
  5.  
  6. if gtk.pygtk_version < (2,0):
  7.         print "PyGtk 2.0 or later required for this"
  8.         raise SystemExit
  9.  
  10.  
  11. """
  12. class ComboButtonException(Exception):
  13.         def __str__(self):
  14.                 return repr(
  15.                         "!!!Error!!!! combobutton must be create with a list or 
  16. tuple "  
  17. """
  18.  
  19. class ComboButton(gtk.Widget):
  20.  
  21.         def __init__(self,menuitems):
  22.                 gtk.Widget.__init__(self)
  23.                 """
  24.                 if( !(isinstance(menuitems,types.TupleType) or 
  25.                         isinstance(menuitems,type.ListType))):
  26.                                 raise(ComboButtonException())
  27.                 """
  28.                 box = gtk.HBox(False,0)
  29.                 box.set_border_width(2)
  30.                 menu = gtk.Menu()
  31.                 for i in menuitems:
  32.                         item = gtk.MenuItem(i)
  33.                         item.show()
  34.                         menu.append(item)
  35.                         item.connect( "activate",self.menu_response,i)
  36.  
  37.                 self.button = gtk.Button()
  38.                 arrow = gtk.Arrow(gtk.ARROW_DOWN,gtk.SHADOW_IN)
  39.                 self.button.add(arrow)
  40.                 box.add(self.button)
  41.  
  42.                 self.entry = gtk.Entry()
  43.                 box.add(self.entry)
  44.  
  45.                 box.show_all()
  46.  
  47.         def do_realize(self):
  48.                 self.set_flags(self.flags() | gtk.REALIZED)
  49.                 self.window = gtk.gdk.Window(
  50.                         self.get_parent_window(),
  51.                         width=self.allocation.width,
  52.                         height=self.allocation.height,
  53.                         window_type = gdk.WINDOW_CHILD,
  54.                         wclass=gdk.INPUT_OUTPUT,
  55.                         event_mask=self.get_events() | gtk.gdk.EXPOSURE_MASK|
  56.                                 gtk.gdk.BUTTON1_MOTION_MASK| gtk.gdk.BUTTON_PRES
  57. S_MASK|
  58.                                 gtk.gdk.POINTER_MOTION_MASK| gtk.gdk.POINTER_MOT
  59. ION_HINT_MASK)
  60.  
  61.                 self.window.set_user_data(self)
  62.                 self.style.attach(self.window)
  63.                 self.style.set_background(self.window,gtk.STATE_NORMAL)
  64.                 self.window.mone_resize(*self.allocation)
  65.  
  66.         def do_unrealize(self):
  67.                 self.window.destroy()
  68.  
  69.         def do_size_request(self,requisition):
  70.                 requisition.height = 14
  71.                 requisition.width = 30
  72.  
  73.         def do_size_allocate(self,allocation):
  74.                 self.allocation = allocation
  75.                 if self.flags() & gtk.REALIZED:
  76.                         self.window.move_resize(*allocation)
  77.  
  78.         def do_expose_event(self,event):
  79.                 self.button.do_expose_event(self,event)
  80.                 self.entry.do_expose_event(self,event)
  81.         def button_popup(self,widget,event,menu):
  82.                 if event.type == gtk.gdk.BUTTON_PRESS:
  83.                         menu.popup(None,None,None,event.button,event.time)
  84.                         return True
  85.                 return False
  86.  
  87.         def menu_response(self,widget,str):
  88.                 print "%s was clicked" % str
  89.                 buffer = self.entry.get_text()
  90.                 self.entry.set_text(buffer + str )
  91.  
  92.  
  93.         def entry_call(self,widget,event):
  94.                 print "event is of %s type" % event.type
  95.  
  96.         def set_active(self,number):
  97.                 pass
  98.         def get_text(self):
  99.                 return self.entry.get_text()
  100.         def set_text(self,text):
  101.                 self.entry.set_text(text)
  102.  
i've followed a few custom widget tutorials but none them have involved building widgets from the widgets provided by pygtk

Expand|Select|Wrap|Line Numbers
  1.  
  2.         button = pkylib.ComboButton(("foo","bar","baz"))                        
  3.         otherbox.add(button)      
  4.  
theres the code i have for declaring the widget
Dec 21 '06 #1
10 5404
bartonc
6,596 Expert 4TB
I can tell you that

Expand|Select|Wrap|Line Numbers
  1.               gtk.Widget.__init__(self)
should be higher in the class hierarchy. I don't know this package, but, sinse gtk.Widget is abstract, there must be a class (perhaps Button) that you should be inheriting from. That choise depends on what built in functionality you are after.
Dec 21 '06 #2
bartonc
6,596 Expert 4TB
I can tell you that

Expand|Select|Wrap|Line Numbers
  1.               gtk.Widget.__init__(self)
should be higher in the class hierarchy. I don't know this package, but, sinse gtk.Widget is abstract, there must be a class (perhaps Button) that you should be inheriting from. That choise depends on what built in functionality you are after.
If you don't need built functionality of a button (or anything else), don't __init__() the abstract class.
Dec 21 '06 #3
jubei
8
i just tried subclass HBox instead of widget, i dont get any compile or runtime errors now, but the button and entry dont show up
Dec 21 '06 #4
bartonc
6,596 Expert 4TB
i just tried subclass HBox instead of widget, i dont get any compile or runtime errors now, but the button and entry dont show up
Let's have a look at the new code...
Dec 21 '06 #5
bartonc
6,596 Expert 4TB
i just tried subclass HBox instead of widget, i dont get any compile or runtime errors now, but the button and entry dont show up
In some Gui Tool Kits, Add() is enough to get the widget to appear. In others, a geometry manager is called (in Tkinter it frame.grid(), etc.) to show all newly created widgets.
Dec 22 '06 #6
jubei
8
so after subclassing hbox i made the realization that i could just add widgets right into my subclass, so i now have the following for code

Expand|Select|Wrap|Line Numbers
  1. class ComboButton(gtk.HBox):
  2.  
  3.         def __init__(self,menuitems):
  4.                 gtk.HBox.__init__(self,False,0)
  5.                 """
  6.                 if( !(isinstance(menuitems,types.TupleType) or 
  7.                         isinstance(menuitems,type.ListType))):
  8.                                 raise(ComboButtonException())
  9.                 """
  10.                 self.set_border_width(2)
  11.                 self.entry = gtk.Entry()
  12.                 menu = gtk.Menu()
  13.                 for i in menuitems:
  14.                         item = gtk.MenuItem(i)
  15.                         item.show()
  16.                         menu.append(item)
  17.                         item.connect( "activate",self.menu_response,i)
  18.  
  19.                 button = gtk.Button()
  20.                 arrow = gtk.Arrow(gtk.ARROW_DOWN,gtk.SHADOW_IN)
  21.                 button.add(arrow)
  22.                 self.pack_start(button,False,False,0)
  23.                 self.pack_end(self.entry,True,True,0)
  24.  
  25.                 button.connect("event",self.button_popup,menu)
  26.  
  27.  
  28.                 self.show_all()
  29.         def button_popup(self,widget,event,menu):
  30.                 if event.type == gtk.gdk.BUTTON_PRESS:
  31.                         menu.popup(None,None,None,event.button,event.time)
  32.                         return True
  33.                 return False
  34.         def menu_response(self,widget,str):
  35.                 print "%s was clicked" % str
  36.                 buffer = self.entry.get_text()
  37.                 self.entry.set_text(buffer + str )
  38.  
  39.  
  40.         def entry_call(self,widget,event):
  41.                 print "event is of %s type" % event.type
  42.  
  43.         def set_active(self,number):
  44.                 pass
  45.         def get_text(self):
  46.                 return self.entry.get_text()
  47.  
  48.         def set_text(self,text):
  49.                 self.entry.set_text(text)
  50.  
Dec 22 '06 #7
bartonc
6,596 Expert 4TB
so after subclassing hbox i made the realization that i could just add widgets right into my subclass, so i now have the following for code

Expand|Select|Wrap|Line Numbers
  1. class ComboButton(gtk.HBox):
  2.  
  3.         def __init__(self,menuitems):
  4.                 gtk.HBox.__init__(self,False,0)
  5.                 """
  6.                 if( !(isinstance(menuitems,types.TupleType) or 
  7.                         isinstance(menuitems,type.ListType))):
  8.                                 raise(ComboButtonException())
  9.                 """
  10.                 self.set_border_width(2)
  11.                 self.entry = gtk.Entry()
  12.                 menu = gtk.Menu()
  13.                 for i in menuitems:
  14.                         item = gtk.MenuItem(i)
  15.                         item.show()
  16.                         menu.append(item)
  17.                         item.connect( "activate",self.menu_response,i)
  18.  
  19.                 button = gtk.Button()
  20.                 arrow = gtk.Arrow(gtk.ARROW_DOWN,gtk.SHADOW_IN)
  21.                 button.add(arrow)
  22.                 self.pack_start(button,False,False,0)
  23.                 self.pack_end(self.entry,True,True,0)
  24.  
  25.                 button.connect("event",self.button_popup,menu)
  26.  
  27.  
  28.                 self.show_all()
  29.         def button_popup(self,widget,event,menu):
  30.                 if event.type == gtk.gdk.BUTTON_PRESS:
  31.                         menu.popup(None,None,None,event.button,event.time)
  32.                         return True
  33.                 return False
  34.         def menu_response(self,widget,str):
  35.                 print "%s was clicked" % str
  36.                 buffer = self.entry.get_text()
  37.                 self.entry.set_text(buffer + str )
  38.  
  39.  
  40.         def entry_call(self,widget,event):
  41.                 print "event is of %s type" % event.type
  42.  
  43.         def set_active(self,number):
  44.                 pass
  45.         def get_text(self):
  46.                 return self.entry.get_text()
  47.  
  48.         def set_text(self,text):
  49.                 self.entry.set_text(text)
  50.  
Expand|Select|Wrap|Line Numbers
  1.                 button = gtk.Button()
  2.                 arrow = gtk.Arrow(gtk.ARROW_DOWN,gtk.SHADOW_IN)
  3.                 button.add(arrow)
  4.                 self.pack_start(button,False,False,0)
  5.                 self.pack_end(self.entry,True,True,0)
  6.  
  7.                 button.connect("event",self.button_popup,menu)
  8.  
  9.  
  10.                 self.show_all()
  11.  
Would work IF self is the button's parent, which should be possible because you can call show_all(). If not, you need to subclass the gtk equivalent to a Tk "Frame" that can be a parent to all your widgets and handle the geometry (drawing things where you want them).
Dec 22 '06 #8
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1.                 button = gtk.Button()
  2.                 arrow = gtk.Arrow(gtk.ARROW_DOWN,gtk.SHADOW_IN)
  3.                 button.add(arrow)
  4.                 self.pack_start(button,False,False,0)
  5.                 self.pack_end(self.entry,True,True,0)
  6.  
  7.                 button.connect("event",self.button_popup,menu)
  8.  
  9.  
  10.                 self.show_all()
  11.  
Would work IF self is the button's parent, which should be possible because you can call show_all(). If not, you need to subclass the gtk equivalent to a Tk "Frame" that can be a parent to all your widgets and handle the geometry (drawing things where you want them).
Possibly just doing
Expand|Select|Wrap|Line Numbers
  1. self.add(button)
will do the trick.
Dec 22 '06 #9
jubei
8
Possibly just doing
Expand|Select|Wrap|Line Numbers
  1. self.add(button)
will do the trick.
the chunk of code that i have up there is working exactly as i want it to.
Dec 22 '06 #10
bartonc
6,596 Expert 4TB
the chunk of code that i have up there is working exactly as i want it to.
Yippie! I'm glad that I could help. Thanks for the update. Keep posting,
Barton
Dec 22 '06 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by j_mckitrick | last post: by
reply views Thread by Michele Simionato | last post: by
1 post views Thread by Harlin Seritt | last post: by
22 posts views Thread by dcrespo | last post: by
1 post views Thread by Thomas Bartkus | last post: by
14 posts views Thread by Rod W | last post: by
1 post views Thread by manatlan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.