I would like to clarify a couple things to help others reading/watching this tutorial for the first time:
1. All of the Python code that he shows on the first page will not run if you copy and paste it because it is lacking indentation. However, the newer code posted above and below does work (because it is indented).
2. When you click on the link to the tutorial, don't click Play because that is a different tutorial ("List Comprehension"), but go down to "Tutorial - A GUI application using Python". Also note that this tutorial references the revised GtkBuilder code (initially crafted above by FokkerCharlie, but simplified in the video by deleting some lines - see below).
3. It is a little confusing about "Glade": "Glade" as a GUI HTML-code generating tool has not been depreciated. Only the Glade library has been depreciated, which you only use if you choose "Libglade" as your format when you start a new project in Glade. Now choose "GtkBuilder" format because the Glade Libglade library is now included in gtk.'
So, here is the final code that results from the revisions in the tutorial (he refers to being able to download the code, but I don't see where to click to download it):
Code:
#!/usr/bin/env python
import sys
try:
import pygtk
pygtk.require("2.0")
except:
pass
try:
import gtk
except:
print("GTK Not Available")
sys.exit(1)
class adder:
result = 0
def __init__( self, number1, number2 ):
self.result = int( number1 ) + int( number2 )
def giveResult( self ):
return str(self.result)
class adderGui:
wTree = gtk.Builder()
def __init__( self ):
self.builder = gtk.Builder()
self.builder.add_from_file("adder.glade")
dic = {
"on_buttonQuit_clicked" : self.quit,
"on_buttonAdd_clicked" : self.add,
"on_windowMain_destroy" : self.quit,
}
self.builder.connect_signals( dic )
def add(self, widget):
entry1 = self.builder.get_object ("entry1")
entry2 = self.builder.get_object ("entry2")
try:
thistime = adder( entry1.get_text(), entry2.get_text() )
except ValueError:
self.builder.get_object("hboxWarning").show()
self.builder.get_object("entryResult").set_text("ERROR")
return 0
self.builder.get_object("hboxWarning").hide()
self.builder.get_object("entryResult").set_text(thistime.giveResult())
def quit(self, widget):
sys.exit(0)
adderGui = adderGui()
gtk.main()