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 5695
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: 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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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=()=>{
|
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...
| |