Build a Twitter client using PyGTK
This tutorial shows how to use Python to build GUI applications very quickly. After an introduction to PyGTK you will learn how to build a simple, but elegant Twitter post client…
Changed function: This function takes care of the character count in the GUI. It also disables or enables the post button depending upon the characters you have typed.
def changed(buffer, label, postbtn) :
count = buffer.get_char_count()
labelstr = str(count) + “ chars (“ + str(maxchars-count) + “ left)”
label.set_label(labelstr)
if count > 0 and count <= maxchars :
postbtn.set_sensitive(True)
else :
postbtn.set_sensitive(False)
Window and the VBox: We are not using Glade to design the UI, rather using pure code to create the UI. The following code sets up the main window and its UI components like the VBox (a container) and label.
win = gtk.Window()
win.connect(“destroy”, gtk.main_quit)
vbox = gtk.VBox(spacing=3)
win.add(vbox)
vbox.show()
label = gtk.Label(“0 chars (“ + str(maxchars) + “ left)”)
vbox.pack_start(label)
label.show()
The following code sets up the TextView inside the VBox. It also sets up the text buffer.
textview = gtk.TextView(buffer=None)
textview.set_wrap_mode(gtk.WRAP_WORD)
textview.set_size_request(420, 60)
vbox.pack_start(textview)
textview.show()
textbuffer = textview.get_buffer()
The following code sets up the buttons for our user interface.
buttonbox = gtk.HBox(spacing=3)
vbox.pack_start(buttonbox)
buttonbox.show()
postbtn = gtk.Button(“Post”)
postbtn.connect(“clicked”, post, textbuffer)
postbtn.set_sensitive(False)
buttonbox.pack_start(postbtn)
postbtn.show()
quitbtn = gtk.Button(“Quit”)
quitbtn.connect(“clicked”, gtk.main_quit)
buttonbox.pack_start(quitbtn)
quitbtn.show()
textbuffer.connect(“changed”, changed, label, postbtn)
Our program also reads the text from the command-line argument. The following code records the argv contents into the text buffer.
if len(sys.argv) > 1 :
textbuffer.insert_at_cursor(‘ ‘.join(sys.argv[1:]))
The following code makes the window visible and sets the code-entry point.
win.show()
gtk.main()
Running the program
Create a .twitter file in your home directory and populate it with your Twitter credentials as directed above. Start the program with the Python interpreter. Remember that we are still using stdout to print out standard error messages and confirmations. You may want to keep an eye on the terminal to find out how the program is doing.
$ python TwitPost.py
PyGTK is definitely a great way to build native applications if you like working with dynamic languages. It also offers great flexibility to run the program across all platforms, including embedded devices. Also, if you like Qt you can try out PyQT which is another Python language binding, this time for the Qt toolkit.
Advisor:
Kunal Deo is a veteran open source and KDE developer and has contributed to many open source projects, including KDE-Solaris and Openmoko. He has written numerous articles on open source, Solaris and Linux related technologies for various technical magazines around the globe.
















I’m python beginner and this post give me fine set of instructions to build tweet machine. Good luck and thanks for sharing