Bound global bindings in a saner fashion, added tweet character counter and error checker for tweets > 140 characters
This commit is contained in:
parent
c84fcb19fe
commit
4906fa7fe7
39
mytwitter.py
39
mytwitter.py
|
@ -2,7 +2,7 @@
|
||||||
#
|
#
|
||||||
# Custom twitter client... mostly for learning Python
|
# Custom twitter client... mostly for learning Python
|
||||||
|
|
||||||
import sys, twitter, Tkinter, Pmw, ConfigParser, os
|
import sys, twitter, Tkinter, Pmw, ConfigParser, os, tkMessageBox
|
||||||
|
|
||||||
|
|
||||||
class TwitWindow:
|
class TwitWindow:
|
||||||
|
@ -54,17 +54,20 @@ class TwitWindow:
|
||||||
self.update_box.grid_columnconfigure(0, weight=1)
|
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 = Tkinter.Button(self.update_box, text="Update", command=self.update_status)
|
||||||
self.update_button.grid(row=0, column=1, sticky=Tkinter.W)
|
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
|
### Set up bindings
|
||||||
|
|
||||||
# Bind scrollwheel to move the tweets. Might as well allow scrollwheel
|
# Bind scrollwheel to move the tweets, as well as page up/down
|
||||||
# from anywhere, so we'll bind to tkroot
|
self.tkroot.bind_all("<Button-4>", self.scroll_wheel);
|
||||||
self.tkroot.bind("<Button-4>", self.scroll_wheel);
|
self.tkroot.bind_all("<Button-5>", self.scroll_wheel);
|
||||||
self.tkroot.bind("<Button-5>", self.scroll_wheel);
|
self.tkroot.bind_all("<Prior>", self.page_up);
|
||||||
|
self.tkroot.bind_all("<Next>", self.page_down);
|
||||||
|
|
||||||
|
# Bind any key in the update_entry to adjust the counter, except return
|
||||||
|
self.update_entry.bind('<KeyRelease>', self.char_counter)
|
||||||
self.update_entry.bind('<Return>', self.update_status_callback)
|
self.update_entry.bind('<Return>', self.update_status_callback)
|
||||||
self.tkroot.bind("<Prior>", self.page_up);
|
|
||||||
self.tkroot.bind("<Next>", self.page_down);
|
|
||||||
|
|
||||||
|
|
||||||
# Init the twitter API and start up the main loop
|
# Init the twitter API and start up the main loop
|
||||||
|
@ -91,6 +94,9 @@ class TwitWindow:
|
||||||
|
|
||||||
def update_status(self):
|
def update_status(self):
|
||||||
text = self.update_entry.get()
|
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.update_entry.delete(0, Tkinter.END)
|
||||||
self.api.PostUpdate(text)
|
self.api.PostUpdate(text)
|
||||||
self.update_window()
|
self.update_window()
|
||||||
|
@ -112,9 +118,15 @@ class TwitWindow:
|
||||||
def page_up(self, event):
|
def page_up(self, event):
|
||||||
self.tweet_view.yview('scroll', -15, 'units');
|
self.tweet_view.yview('scroll', -15, 'units');
|
||||||
|
|
||||||
|
|
||||||
def page_down(self, event):
|
def page_down(self, event):
|
||||||
self.tweet_view.yview('scroll', 15, 'units');
|
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
|
### end class TwitWindow
|
||||||
|
|
||||||
|
|
||||||
|
@ -139,19 +151,6 @@ def print_formatted(statuses):
|
||||||
print
|
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
|
# main
|
||||||
win = TwitWindow()
|
win = TwitWindow()
|
||||||
win.create_window()
|
win.create_window()
|
||||||
|
|
Reference in New Issue
Block a user