NOTE: This post has several developments noted in the EDITs section at the bottom.
I can't figure out why my small program is throwing an error.
It seems like lot's of other people are having this same issue. I just read this article which basically says installing gtk is impossible. Surely that is not the case...
Also, this one: import gtk not working on mac
Let's figure this out.
I think the most relevant line from the console readout (printed below) is GtkWarning: Screen for GtkWindow not set; you must always set a screen for a GtkWindow before using the window
. I've googled the hell out of this, but I can't figure out how to "set a screen..." before using the window
which python
give /opt/local/bin/python
, which is what I want, I think, as I am using libraries installed through macports.
I've followed the procedure given in this post, so I am sure I'm running py27-pygtk
.
#!/opt/local/bin/python2.7
import gtk, os
window = gtk.Window()
window.connect("destroy", gtk.main_quit)
image = gtk.Image()
window.add(image)
image.set_from_file(os.path.join("data", "image1.jpg"))
window.show_all()
gtk.main()
This is the console readout:
$ python imageHello.py
** (process:691): WARNING **: Trying to register gtype 'GMountMountFlags' as enum when in fact it is of type 'GFlags'
** (process:691): WARNING **: Trying to register gtype 'GDriveStartFlags' as enum when in fact it is of type 'GFlags'
** (process:691): WARNING **: Trying to register gtype 'GSocketMsgFlags' as enum when in fact it is of type 'GFlags'
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/gtk-2.0/gtk/__init__.py:57: GtkWarning: could not open display
warnings.warn(str(e), _gtk.Warning)
imageHello.py:4: Warning: invalid (NULL) pointer instance
window = gtk.Window()
imageHello.py:4: Warning: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed
window = gtk.Window()
imageHello.py:11: GtkWarning: Screen for GtkWindow not set; you must always set
a screen for a GtkWindow before using the window
window.show_all()
imageHello.py:11: GtkWarning: gdk_screen_get_default_colormap: assertion 'GDK_IS_SCREEN (screen)' failed
window.show_all()
imageHello.py:11: GtkWarning: gdk_colormap_get_visual: assertion 'GDK_IS_COLORMAP (colormap)' failed
window.show_all()
imageHello.py:11: GtkWarning: gdk_screen_get_root_window: assertion 'GDK_IS_SCREEN (screen)' failed
window.show_all()
imageHello.py:11: GtkWarning: gdk_window_new: assertion 'GDK_IS_WINDOW (parent)' failed
window.show_all()
Segmentation fault: 11
EDIT: I read somewhere (will post link as soon as I find it) that you need something called x11 in order to use gtk. So I just ran sudo port -v install xorg-server
. This did not help.
EDIT2: Just downloaded xquartz from their website. But then, and here's the key I think, I restarted my computer. Now I am able to get my program to run properly. However, I am still getting the following warning. I have no idea what it means. Any ideas?
$ python imageHello.py
** (process:312): WARNING **: Trying to register gtype 'GMountMountFlags' as enum when in fact it is of type 'GFlags'
** (process:312): WARNING **: Trying to register gtype 'GDriveStartFlags' as enum when in fact it is of type 'GFlags'
** (process:312): WARNING **: Trying to register gtype 'GSocketMsgFlags' as enum when in fact it is of type 'GFlags'