472,783 Members | 992 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,783 software developers and data experts.

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 5695
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

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

Similar topics

4
by: j_mckitrick | last post by:
Hi all. Here is a tiny container for one of each combo box, along with the glade file. Just 2 widgets, so hopefully not too large. How the heck do I get the selection from the ComboBox, as...
0
by: Michele Simionato | last post by:
I am in the process of learning pygtk and I would like to port some custom made Tkinter widgets to pygtk, just an exercise. For instance I have this code: .. from Tkinter import * .. .. class...
1
by: Harlin Seritt | last post by:
I have the following code and I would like to know how to set the length and width of widgets like Buttons. When the window opens the button fills up the space even though I have told it not to....
22
by: dcrespo | last post by:
Hi all... I think wxPython is much better than PyGTK. First of all, PyGTK needs the GTK runtime installed, whereas wxPython is entirely Python's modules, so It facilitates the apps'...
1
by: Thomas Bartkus | last post by:
I am experimenting (flailing around?) with glade and python. Both under MS Windows and Linux. I understand why I want to "import gtk" It gives me access to the critical gui program loop...
25
by: TPJ | last post by:
GUI's etc: PyGtk on Windows "(...) So if someone develops mainly for X and just wants to make sure that it is not impossible to run on Windows, you can use PyGTK. (...)", July 2nd, 1999 pyGTK...
14
by: Rod W | last post by:
I'm just starting out on Python but my primary goal is to provide applications with some user interface (GUI). Can someone point me to a good comparison of whether I should use wxPython (with...
1
by: manatlan | last post by:
I was a fan of "SimpleGladeApp/tepache way" to build a pygtk app. I've build a new efficient/dynamic way to build a pygtk app ... Here is an example :...
1
by: stevemcc | last post by:
I am trying to make a game using pygtk. It requires that there be an image in the background and widgets that can go in front of the image. I have tried defining a background image for the Mainwindow...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.