#!/usr/bin/python # # Custom twitter client... mostly for learning Python import sys, twitter, Tkinter, Pmw, ConfigParser, os class TwitWindow: """ Display Tweets, post to twitter """ def __init__(self): config = ConfigParser.ConfigParser() config.read(os.path.expanduser("~/.mytwitter")) self.username = config.get('global', 'username') self.password = config.get('global', 'password') self.num_entries = int(config.get('global', 'entries')) self.refresh_time = int(config.get('global', 'refreshtime')) self.labels = [] self.texts = [] def create_window(self): self.tkroot = Tkinter.Tk() self.tkroot.title("MyTwitter") Pmw.initialise(self.tkroot) self.tkroot.grid_columnconfigure(0, weight=1) # Create the scrolled frame that will hold the tweets self.tweet_view = Pmw.ScrolledFrame(self.tkroot, hscrollmode='none', horizflex='elastic') self.tweet_view.grid(row=0) # Create labels and text widgets for i in range(0, self.num_entries): self.labels.append(Tkinter.Label(self.tweet_view.interior())) self.labels[i].pack(expand=False, fill=Tkinter.X) self.texts.append(Tkinter.Label(self.tweet_view.interior())) self.texts[i].pack(expand=False, fill=Tkinter.X) self.labels[i].config(bg="#07c", fg="white", anchor=Tkinter.W) self.texts[i].config(bg="#eff", fg="black", height=3, anchor=Tkinter.NW, justify=Tkinter.LEFT, wraplength=375) # A button to refresh manually self.button_box = Tkinter.Frame(self.tkroot) self.button_box.grid(row=1, sticky=Tkinter.W) self.refresh_button = Tkinter.Button(self.button_box, text="Refresh", command=self.update_window) self.refresh_button.grid(row=0, sticky=Tkinter.W) # Create an update box at the bottom of the window self.update_box = Tkinter.Frame(self.tkroot) self.update_box.grid(row=2, sticky=Tkinter.W) self.update_entry = Tkinter.Entry(self.update_box) self.update_entry.grid(row=0, sticky=Tkinter.W) self.update_box.grid_columnconfigure(0, weight=1) self.update_button = Tkinter.Button(self.update_box, text="Update", command=self.update_status) self.update_button.grid(row=0, column=1, sticky=Tkinter.W) ### Set up bindings # Bind scrollwheel to move the tweets. Might as well allow scrollwheel # from anywhere, so we'll bind to tkroot self.tkroot.bind("", self.scroll_wheel); self.tkroot.bind("", self.scroll_wheel); self.update_entry.bind('', self.update_status_callback) self.tkroot.bind("", self.page_up); self.tkroot.bind("", self.page_down); # Init the twitter API and start up the main loop self.api = twitter.Api(username=self.username, password=self.password) self.update_window() self.tkroot.mainloop() def update_window(self) : statuses = self.api.GetFriendsTimeline(self.username, count=self.num_entries) for i in range(0, self.num_entries): 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].config(text=statuses[i].text) self.timer = self.tkroot.after(self.refresh_time * 1000, self.update_window) def update_status(self): text = self.update_entry.get() self.update_entry.delete(0, Tkinter.END) self.api.PostUpdate(text) self.update_window() # Just calls update_status, here so that things # that pass an event can be used def update_status_callback(self, event): self.update_status() def scroll_wheel(self, event): if event.num == 4: self.tweet_view.yview('scroll', -5, 'units'); if event.num == 5: self.tweet_view.yview('scroll', 5, 'units'); def page_up(self, event): self.tweet_view.yview('scroll', -15, 'units'); def page_down(self, event): self.tweet_view.yview('scroll', 15, 'units'); ### end class TwitWindow def print_lists(): data = api.GetUserLists() for l in data['lists']: print l.name def print_statuses_list(): statuses = api.GetListStatuses(sys.argv[2]) print_formatted(statuses) def print_statuses(): statuses = api.GetFriendsTimeline(username) print_formatted(statuses) def print_formatted(statuses): for s in statuses: print s.user.name.encode("utf-8"), "(", s.user.screen_name, ") :" print s.text.encode("utf-8") print # Do the action! # arg = "" # if len(sys.argv) > 1: # arg = sys.argv[1] # actions = { # "show_lists": print_lists, # "list": print_statuses_list, # } # actions.get(arg, print_statuses)() # main win = TwitWindow() win.create_window()