IdeaMonk

thoughts, ideas, code and other things...

Wednesday, August 12, 2009

Glade3, PyGTK, urllib2 and a simple app

I've started exploring PyGTK to create GUI apps for linux. Today, I just made a simple app which fetches your last tweet from twitter. I used glade 3.6.3 for designing the UI and programming was done in Python. Gedit is becoming my default editor day by day. Though emacs might be glorious and vim, powerful, but at end of the day what matters is how good am I at using them.
The source code is fairly easy to understand if you've followed some PyGTK tutorials. I recommend Micah Carrick's tutorial - http://www.micahcarrick.com/01-01-2008/gtk-glade-tutorial-part-3.html for anyone who wishes to begin. Also asking questions on #pygtk, #gtk on irc.gnome.org is very helpful when you're lost in the not so obvious API.


twitterFetch.py

#!/usr/bin/env python

import sys
import urllib2
import string
import re

try:
import pygtk
pygtk.require ("2.0")
except:
pass

try:
import gtk
import gtk.glade
except:
print "You need to install pyGTK or GTKv2"
print "or set your PYTHONPATH correctly."
sys.exit(1)

def remove_html_tags(data):
''' removes unnecessary html tags from the tweet '''
p = re.compile(r'<.*?>')
return p.sub('', data)

class newapp:
builder = gtk.Builder()

def __init__(self):
windowname = "MainWindow"
gladefile = "tweetui.xml"

# Loads the UI from GtkBuilder XML file
self.builder.add_from_file(gladefile)

# Lets extract a reference to window object to use later
self.window = self.builder.get_object(windowname)

# Sets up event handlers
dic = {
"on_btnFetch_clicked" : self.fetchTweet,
"on_MainWindow_destroy" : gtk.main_quit
}
self.builder.connect_signals (dic)

return

def fetchTweet(self,widget):
widget.set_label("loading...")

# disable's the fetch tweet button
widget.set_sensitive(False)
# man, we got 3 ways to do the same thing
# widget.set_state(False)
# widget.props.sensitive = False
# widget.set_sensitive(False)

username = self.builder.get_object("txtUsername").props.text
lblTweet = self.builder.get_object("lblTweet")

tweet = ""

try:
# grab html of twitter page
response = urllib2.urlopen ("http://twitter.com/" + username)
tweet = response.read()
except:
print "Error loading twitter url"
gtk.main_quit()

try:
# parse the first tweet, split with html on edges as markers
# I know I could use twitter API and parse the json...
crap,tweet = tweet.split("<span class=\"status-body\"><span class=\"entry-content\">",1)
tweet,crap = tweet.split ("</span>",1)
except:
print "Error parsing result"
gtk.main_quit()

# set the tweet into label
lblTweet.props.label = remove_html_tags(tweet)

# reset the fetch tweet button
widget.set_label("fetch tweet")
widget.set_sensitive(True)

return

myapp = newapp()
gtk.main()


tweetui.xml
<?xml version="1.0"?>
<interface>
<requires lib="gtk+" version="2.16"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="MainWindow">
<property name="visible">True</property>
<property name="title" translatable="yes">Hello World</property>
<property name="default_width">440</property>
<property name="default_height">250</property>
<signal name="destroy" handler="on_MainWindow_destroy"/>
<child>
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="lblTweet">
<property name="visible">True</property>
<property name="xpad">7</property>
<property name="label" translatable="yes">label</property>
<property name="wrap">True</property>
<property name="selectable">True</property>
</object>
<packing>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="txtUsername">
<property name="height_request">47</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">•</property>
<property name="text" translatable="yes">ideamonk</property>
<property name="xalign">0.5</property>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btnFetch">
<property name="label" translatable="yes">fetch tweet</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_btnFetch_clicked"/>
</object>
<packing>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</object>
</interface>


Labels: , , , , ,

1 Comments:

At December 29, 2012 at 3:51 PM , Blogger gunma said...

this is a good one..say, i'm doing this on windows 7, can you please elaborate on where the python code and the xml file should be placed etc..

Thanks :))

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home