Version 34 of gnocl or PyGtk?

Updated 2007-10-31 11:54:47 by wjg

WJG For the past ten years or so I've been a mixed Windows/Linux user. Jolted along by a presentation that I made at an OpenAdvantage [L1 ] seminar earlier this year, I decided to put windows on the back-burner and spend my time running linux. Putting OS considerations aside I needed to think about re-working my custom apps running under Tcl/Tk on windows. Windows and MacOS discourage us from fiddling about with the appearance of things whereas Linux, well... Now, that I've settled into things and got Gnome looking the way I like it, I want my tcl apps to fall in line. Not an easy thing to do in Tk - under Linux as everything still looks like Motif. So the dilema is GTk or Tk? Well -Gtk. But what about Tcl? If we refer to the Gtk+ developer site [L2 ] we do find bindings listed and, as seems par for everying else, Python is the preferred choice. But why should it be? The following piece of Python code puts a single button in the middle of a toplevel window. The link for PyGtk [L3 ].


 #!/usr/bin/env python

 # example helloworld.py

 import pygtk
 pygtk.require('2.0')
 import gtk

 class HelloWorld:


    def hello(self, widget, data=None):
        print "Hello World"

    def delete_event(self, widget, event, data=None):
        print "delete event occurred"
        return False

    def destroy(self, widget, data=None):
        print "destroy signal occurred"
        gtk.main_quit()

    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("delete_event", self.delete_event)
        self.window.connect("destroy", self.destroy)
        self.window.set_border_width(10)
        self.button = gtk.Button("Hello World")
        self.button.connect("clicked", self.hello, None)
        self.button.connect_object("clicked", gtk.Widget.destroy, self.window)
        self.window.add(self.button)
        self.button.show()
        self.window.show()

    def main(self):
        gtk.main()

 if __name__ == "__main__":
    hello = HelloWorld()
    hello.main()

Here's the equivalent in Gnocl, effectively one line of code. In PyGtk, the coder still needs to manually made connections between events and handlers which Gnocl does automatically. Isn't the purpose of scripting to reduce code-recreation the minimum, not merely to make recreation easier?


 #!/bin/sh
 # the next line restarts using tclsh \
 exec tclsh "$0" "$@"

 package require Gnocl

 gnocl::window -child [gnocl::button -text "Hello World" -onButtonPress exit] -onDelete exit

Which one would you choose? Find the Gtk C code here [L4 ].

LV Another consideration - check out Tile, which, as Ttk, comes with Tk starting in Tcl/Tk 8.5. Perhaps it would be worthwhile seeing how to improve the Ttk-Gtk cooperation?

LBarret Tile is very promising but the argument for gnocl is pretty weak : mainly better syntatic sugar. Furthemore, this is vanilla GTK, many people use autoconnect mecanisms that make the difference less important. Comparing the other example seen on the gnocl mainpage may show that the difference is not that important.

WJG Thanks for the comments -but. I'm not dealing with issues regardind the Tk-Tile debate, but how to get the best out of easy scripting with Gtk. If I want to run my apps on Windows, I'd leave them the way they are cause they look pretty good and the rendering of widgets is just fine to me. If I'm running Linux, then I want it to adhere to my desktop theme whatever that might be. Tk doesn't do that, and neither, do I suspect, will Tile. Lets keep comments salient to the content of the page which is the ease of building Gtk based UI applications in a tcl script. I would have thought this a cause for celebration in itself. Check the code by using it and not just look the screen shots, try the real alternatives: building Gtk apps in C!

WJG Fellow Wiki users, have the courtesy not to swap code which has been placed for a purpose. Someone changed the Python code to a clip which was not correct.I have restored it back to what it should be.

Nickname WJG, have the courtesy not to massively over-inflate code to prove a point. The Python code you posted contains additional behavior not found in the Gnocl code, and in addition is needlessly verbose.

WJG Nickname, please read the posting. This isn't my code but that from the PyGtk website. Besides, apart from the code that you post doesn't work (not on my machine anyway) its still more verbose than the gnocl code! So, reciprocate the courtesy. Leave postings be, if you wish to make a point with reasoned debate then add it, don't censure others! If I'm wrong, or there's a better view, fine I'll be the first to listen and fix any errors.

Here's a useful link on Wikiquette [L5 ]

For those who really need it, here's Nickame's posting...


 #!/usr/bin/env python

 # example helloworld.py

 import pygtk
 pygtk.require('2.0')
 import gtk

 window = gtk.Window (gtk.WINDOW_TOPLEVEL)
 window.connect ('destroy', gtk.main_quit)
 button = gtk.Button ('Hello World')
 button.connect ('clicked', lambda *_: window.destroy ())
 window.add (button)
 window.show_all ()
 gtk.main ()