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 5629
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: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
header("Location:".$urlback);
Is this the right layout the...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
| |