Write Your Own Linux Twitter Client In Less Time Than It Takes To Find One! - page 2
A command-line Twitter client in about 20 lines
That's all very well, you say, but if you use a commandline client, your friends will laugh at you and won't let you play in their reindeer games?
No problem -- Python, as you've seen in previous articles, is fully capable of doing graphics. I'll use Tkinter here.
Let's pick a simple design: each tweet will have a label showing the author and a text area showing the contents of the tweet.
Start with an object representing the window. You'll need a few variables, including the username, password, and how many entries you want to show.
#! /usr/bin/env python import twitter, Tkinter class TwitWindow : """ Display Twitter output in a TkInter window.""" def __init__(self, _name, _location) : self.username = "your_username" self.password = "your_password" self.num_entries = 11
You'll also want lists where you can store the label and text area for each tweet. Still inside __init__:
# Lists to hold the labels and text areas self.labels =  self.texts = 
Next, write a function to create the main window:
def CreateWindow(self) : # Create the main window self.tkroot = Tkinter.Tk() self.tkroot.title("Twit")
Now you're ready to create the labels and text areas:
# Create all the labels and text widgets: for i in range(0, self.num_entries) : self.labels.append(Tkinter.Label(self.tkroot)) self.labels[i].pack(expand=False, fill=Tkinter.X) self.texts.append(Tkinter.Text()) self.texts[i].config(width=55, height=3) self.texts[i].config(wrap=Tkinter.WORD) self.texts[i].pack(expand=True)
For each tweet, append a Tkinter label and text to the appropriate list, then use TkInter's pack manager to arrange the widgets in a vertical stack. expand=False means the label won't get any bigger than one line even if you resize the window; expand=True on the text areas mean they'll take up the extra space. fill=Tkinter.X on the label makes it take up the full width of the window. The text widget is configured to be 55 characters wide and 3 lines high. Of course, you can adjust all of these to your liking.
Of course you need to initialize the Twitter API and show the window:
self.api = twitter.Api(username=self.username, password=self.passwd) self.tkroot.mainloop()
You can test the program now by adding a few lines at the end of your program to create the window (Figure 1):
# main win = TwitWindow() win.CreateWindow()
Ew, ugly colors! Fortunately, you can adjust the colors to your preference (Figure 2):
self.labels[i].config(bg="#07c", fg="white") self.texts[i].config(bg="#eff", fg="black")
Next, you need a function to get new tweets and update the window. That works a lot like in the command-line twitter, except that when looping over the list of statuses it should update the labels and text widgets instead of printing. One minor difference is that you have to clear each text widget first before adding the new text.
def UpdateWindow(self) : statuses = self.api.GetFriendsTimeline(self.username) for i in range(0, self.num_entries) : self.texts[i].delete(1.0, Tkinter.END) # Clear the old text if i < len(statuses) : # Update the label with the user's name and screen name user = statuses[i].user labeltext = user.name + " (" + user.screen_name + ")" self.labels[i].config(text=labeltext) # Display the text of the tweet self.texts[i].insert(Tkinter.END, statuses[i].text)
Notice anything missing? Right -- the program never calls UpdateWindow(). You can call it just before calling self.tkroot.mainloop() ... but you'll need to set a timer to call it again in five minutes. Do that from the end of UpdateWindow(), and remember, Tkinter timers are in milliseconds, not seconds, so multiply by 1000:
# Run this again five minutes from now: self.timer = self.tkroot.after(300000, self.UpdateWindow)
Be careful on the indentation of that timer line -- you don't want it
And there you have it -- your very own, fully customizable Twitter client. Happy tweeting!
Akkana Peck is a freelance programmer and writer and the author of Beginning GIMP: From Novice to Professional. Her website includes a more fully featured version of the Python Twit client, and you can follow her tweets here.
- Skip Ahead
- 1. A command-line Twitter client in about 20 lines
- 2. A command-line Twitter client in about 20 lines
Solid state disks (SSDs) made a splash in consumer technology, and now the technology has its eyes on the enterprise storage market. Download this eBook to see what SSDs can do for your infrastructure and review the pros and cons of this potentially game-changing storage technology.