diff --git a/mytwitter.py b/mytwitter.py index 98768bb..c9ee053 100755 --- a/mytwitter.py +++ b/mytwitter.py @@ -2,7 +2,7 @@ # # Custom twitter client... mostly for learning Python -import sys, twitter, Tkinter, Pmw, ConfigParser, os +import sys, twitter, Tkinter, Pmw, ConfigParser, os, tkMessageBox class TwitWindow: @@ -54,17 +54,20 @@ class TwitWindow: 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) + self.update_count = Tkinter.Label(self.update_box, text="0/140") + self.update_count.grid(row=0, column=2, sticky=Tkinter.E) ### 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); + # Bind scrollwheel to move the tweets, as well as page up/down + self.tkroot.bind_all("", self.scroll_wheel); + self.tkroot.bind_all("", self.scroll_wheel); + self.tkroot.bind_all("", self.page_up); + self.tkroot.bind_all("", self.page_down); + # Bind any key in the update_entry to adjust the counter, except return + self.update_entry.bind('', self.char_counter) 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 @@ -91,6 +94,9 @@ class TwitWindow: def update_status(self): text = self.update_entry.get() + if len(text) > 140: + tkMessageBox.showerror('Tweet too long', 'Tweets can only be 140 characters. Write a shorter tweet and try again.') + return self.update_entry.delete(0, Tkinter.END) self.api.PostUpdate(text) self.update_window() @@ -112,9 +118,15 @@ class TwitWindow: def page_up(self, event): self.tweet_view.yview('scroll', -15, 'units'); + def page_down(self, event): self.tweet_view.yview('scroll', 15, 'units'); + + def char_counter(self, event): + new_count = str(len(self.update_entry.get())) + "/140" + self.update_count.config(text=new_count) + ### end class TwitWindow @@ -139,19 +151,6 @@ def print_formatted(statuses): 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()