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: -
import gobject
-
import gtk
-
-
from gtk import gdk
-
-
if gtk.pygtk_version < (2,0):
-
print "PyGtk 2.0 or later required for this"
-
raise SystemExit
-
-
-
"""
-
class ComboButtonException(Exception):
-
def __str__(self):
-
return repr(
-
"!!!Error!!!! combobutton must be create with a list or
-
tuple "
-
"""
-
-
class ComboButton(gtk.Widget):
-
-
def __init__(self,menuitems):
-
gtk.Widget.__init__(self)
-
"""
-
if( !(isinstance(menuitems,types.TupleType) or
-
isinstance(menuitems,type.ListType))):
-
raise(ComboButtonException())
-
"""
-
box = gtk.HBox(False,0)
-
box.set_border_width(2)
-
menu = gtk.Menu()
-
for i in menuitems:
-
item = gtk.MenuItem(i)
-
item.show()
-
menu.append(item)
-
item.connect( "activate",self.menu_response,i)
-
-
self.button = gtk.Button()
-
arrow = gtk.Arrow(gtk.ARROW_DOWN,gtk.SHADOW_IN)
-
self.button.add(arrow)
-
box.add(self.button)
-
-
self.entry = gtk.Entry()
-
box.add(self.entry)
-
-
box.show_all()
-
-
def do_realize(self):
-
self.set_flags(self.flags() | gtk.REALIZED)
-
self.window = gtk.gdk.Window(
-
self.get_parent_window(),
-
width=self.allocation.width,
-
height=self.allocation.height,
-
window_type = gdk.WINDOW_CHILD,
-
wclass=gdk.INPUT_OUTPUT,
-
event_mask=self.get_events() | gtk.gdk.EXPOSURE_MASK|
-
gtk.gdk.BUTTON1_MOTION_MASK| gtk.gdk.BUTTON_PRES
-
S_MASK|
-
gtk.gdk.POINTER_MOTION_MASK| gtk.gdk.POINTER_MOT
-
ION_HINT_MASK)
-
-
self.window.set_user_data(self)
-
self.style.attach(self.window)
-
self.style.set_background(self.window,gtk.STATE_NORMAL)
-
self.window.mone_resize(*self.allocation)
-
-
def do_unrealize(self):
-
self.window.destroy()
-
-
def do_size_request(self,requisition):
-
requisition.height = 14
-
requisition.width = 30
-
-
def do_size_allocate(self,allocation):
-
self.allocation = allocation
-
if self.flags() & gtk.REALIZED:
-
self.window.move_resize(*allocation)
-
-
def do_expose_event(self,event):
-
self.button.do_expose_event(self,event)
-
self.entry.do_expose_event(self,event)
-
def button_popup(self,widget,event,menu):
-
if event.type == gtk.gdk.BUTTON_PRESS:
-
menu.popup(None,None,None,event.button,event.time)
-
return True
-
return False
-
-
def menu_response(self,widget,str):
-
print "%s was clicked" % str
-
buffer = self.entry.get_text()
-
self.entry.set_text(buffer + str )
-
-
-
def entry_call(self,widget,event):
-
print "event is of %s type" % event.type
-
-
def set_active(self,number):
-
pass
-
def get_text(self):
-
return self.entry.get_text()
-
def set_text(self,text):
-
self.entry.set_text(text)
-
i've followed a few custom widget tutorials but none them have involved building widgets from the widgets provided by pygtk -
-
button = pkylib.ComboButton(("foo","bar","baz"))
-
otherbox.add(button)
-
theres the code i have for declaring the widget
10 5748
I can tell you that - 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.
I can tell you that - 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.
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
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...
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.
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 -
class ComboButton(gtk.HBox):
-
-
def __init__(self,menuitems):
-
gtk.HBox.__init__(self,False,0)
-
"""
-
if( !(isinstance(menuitems,types.TupleType) or
-
isinstance(menuitems,type.ListType))):
-
raise(ComboButtonException())
-
"""
-
self.set_border_width(2)
-
self.entry = gtk.Entry()
-
menu = gtk.Menu()
-
for i in menuitems:
-
item = gtk.MenuItem(i)
-
item.show()
-
menu.append(item)
-
item.connect( "activate",self.menu_response,i)
-
-
button = gtk.Button()
-
arrow = gtk.Arrow(gtk.ARROW_DOWN,gtk.SHADOW_IN)
-
button.add(arrow)
-
self.pack_start(button,False,False,0)
-
self.pack_end(self.entry,True,True,0)
-
-
button.connect("event",self.button_popup,menu)
-
-
-
self.show_all()
-
def button_popup(self,widget,event,menu):
-
if event.type == gtk.gdk.BUTTON_PRESS:
-
menu.popup(None,None,None,event.button,event.time)
-
return True
-
return False
-
def menu_response(self,widget,str):
-
print "%s was clicked" % str
-
buffer = self.entry.get_text()
-
self.entry.set_text(buffer + str )
-
-
-
def entry_call(self,widget,event):
-
print "event is of %s type" % event.type
-
-
def set_active(self,number):
-
pass
-
def get_text(self):
-
return self.entry.get_text()
-
-
def set_text(self,text):
-
self.entry.set_text(text)
-
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 -
class ComboButton(gtk.HBox):
-
-
def __init__(self,menuitems):
-
gtk.HBox.__init__(self,False,0)
-
"""
-
if( !(isinstance(menuitems,types.TupleType) or
-
isinstance(menuitems,type.ListType))):
-
raise(ComboButtonException())
-
"""
-
self.set_border_width(2)
-
self.entry = gtk.Entry()
-
menu = gtk.Menu()
-
for i in menuitems:
-
item = gtk.MenuItem(i)
-
item.show()
-
menu.append(item)
-
item.connect( "activate",self.menu_response,i)
-
-
button = gtk.Button()
-
arrow = gtk.Arrow(gtk.ARROW_DOWN,gtk.SHADOW_IN)
-
button.add(arrow)
-
self.pack_start(button,False,False,0)
-
self.pack_end(self.entry,True,True,0)
-
-
button.connect("event",self.button_popup,menu)
-
-
-
self.show_all()
-
def button_popup(self,widget,event,menu):
-
if event.type == gtk.gdk.BUTTON_PRESS:
-
menu.popup(None,None,None,event.button,event.time)
-
return True
-
return False
-
def menu_response(self,widget,str):
-
print "%s was clicked" % str
-
buffer = self.entry.get_text()
-
self.entry.set_text(buffer + str )
-
-
-
def entry_call(self,widget,event):
-
print "event is of %s type" % event.type
-
-
def set_active(self,number):
-
pass
-
def get_text(self):
-
return self.entry.get_text()
-
-
def set_text(self,text):
-
self.entry.set_text(text)
-
-
button = gtk.Button()
-
arrow = gtk.Arrow(gtk.ARROW_DOWN,gtk.SHADOW_IN)
-
button.add(arrow)
-
self.pack_start(button,False,False,0)
-
self.pack_end(self.entry,True,True,0)
-
-
button.connect("event",self.button_popup,menu)
-
-
-
self.show_all()
-
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).
-
button = gtk.Button()
-
arrow = gtk.Arrow(gtk.ARROW_DOWN,gtk.SHADOW_IN)
-
button.add(arrow)
-
self.pack_start(button,False,False,0)
-
self.pack_end(self.entry,True,True,0)
-
-
button.connect("event",self.button_popup,menu)
-
-
-
self.show_all()
-
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
will do the trick.
Possibly just doing
will do the trick.
the chunk of code that i have up there is working exactly as i want it to.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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....
|
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'...
|
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...
|
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...
|
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...
|
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 :...
|
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...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |