Saw the error of my ways, now using pyGtk with libglade

This commit is contained in:
Anna 2010-04-08 16:39:56 -04:00
parent 50b9ad477b
commit 5dae37a260
3 changed files with 310 additions and 115 deletions

3
README
View File

@ -7,8 +7,7 @@ mytwitter is a simple python twitter application. I wrote it for two reasons:
While I doubt it will be terribly useful for anyone other than me, feel free to take it for a spin and let me know how it goes. You'll need the following python modules: While I doubt it will be terribly useful for anyone other than me, feel free to take it for a spin and let me know how it goes. You'll need the following python modules:
* Pmw * pyGTK
* Tkinter
* dateutil * dateutil
* twitter (the dev version, not 0.6), along with my patch (included here) * twitter (the dev version, not 0.6), along with my patch (included here)

261
default.glade Normal file
View File

@ -0,0 +1,261 @@
<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd">
<glade-interface>
<widget class="GtkWindow" id="window">
<property name="visible">True</property>
<property name="title" translatable="yes">MyTwitter</property>
<property name="type">GTK_WINDOW_TOPLEVEL</property>
<property name="window_position">GTK_WIN_POS_NONE</property>
<property name="modal">False</property>
<property name="default_width">400</property>
<property name="default_height">600</property>
<property name="resizable">True</property>
<property name="destroy_with_parent">False</property>
<property name="decorated">True</property>
<property name="skip_taskbar_hint">False</property>
<property name="skip_pager_hint">False</property>
<property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>
<property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
<property name="focus_on_map">True</property>
<property name="urgency_hint">False</property>
<child>
<widget class="GtkVBox" id="main_box">
<property name="visible">True</property>
<property name="homogeneous">False</property>
<property name="spacing">0</property>
<child>
<widget class="GtkMenuBar" id="menu">
<property name="visible">True</property>
<property name="pack_direction">GTK_PACK_DIRECTION_LTR</property>
<property name="child_pack_direction">GTK_PACK_DIRECTION_LTR</property>
<child>
<widget class="GtkMenuItem" id="menuitem7">
<property name="visible">True</property>
<property name="label" translatable="yes">_File</property>
<property name="use_underline">True</property>
<child>
<widget class="GtkMenu" id="menuitem7_menu">
<child>
<widget class="GtkImageMenuItem" id="quit1">
<property name="visible">True</property>
<property name="label">gtk-quit</property>
<property name="use_stock">True</property>
<signal name="activate" handler="gtk_main_quit" last_modification_time="Thu, 08 Apr 2010 19:41:37 GMT"/>
</widget>
</child>
</widget>
</child>
</widget>
</child>
<child>
<widget class="GtkMenuItem" id="menuitem10">
<property name="visible">True</property>
<property name="label" translatable="yes">_Help</property>
<property name="use_underline">True</property>
<child>
<widget class="GtkMenu" id="menuitem10_menu">
<child>
<widget class="GtkMenuItem" id="about1">
<property name="visible">True</property>
<property name="label" translatable="yes">_About</property>
<property name="use_underline">True</property>
<signal name="activate" handler="on_about" last_modification_time="Thu, 08 Apr 2010 19:41:47 GMT"/>
</widget>
</child>
</widget>
</child>
</widget>
</child>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
<child>
<widget class="GtkScrolledWindow" id="tweet_view">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hscrollbar_policy">GTK_POLICY_NEVER</property>
<property name="vscrollbar_policy">GTK_POLICY_ALWAYS</property>
<property name="shadow_type">GTK_SHADOW_NONE</property>
<property name="window_placement">GTK_CORNER_TOP_LEFT</property>
<child>
<widget class="GtkViewport" id="viewport1">
<property name="visible">True</property>
<property name="shadow_type">GTK_SHADOW_IN</property>
<child>
<widget class="GtkVBox" id="tweet_box">
<property name="visible">True</property>
<property name="homogeneous">False</property>
<property name="spacing">0</property>
<child>
<placeholder/>
</child>
</widget>
</child>
</widget>
</child>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">True</property>
<property name="fill">True</property>
</packing>
</child>
<child>
<widget class="GtkHBox" id="button_box">
<property name="visible">True</property>
<property name="homogeneous">False</property>
<property name="spacing">0</property>
<child>
<widget class="GtkButton" id="refresh_button">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="label" translatable="yes">Refresh</property>
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
<child>
<widget class="GtkComboBoxEntry" id="list_select">
<property name="visible">True</property>
<property name="items" translatable="yes">Friends</property>
<property name="add_tearoffs">False</property>
<property name="has_frame">True</property>
<property name="focus_on_click">True</property>
<signal name="changed" handler="on_list_select_changed" last_modification_time="Thu, 08 Apr 2010 20:38:10 GMT"/>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">True</property>
</packing>
</child>
<child>
<widget class="GtkHBox" id="update_box">
<property name="visible">True</property>
<property name="homogeneous">False</property>
<property name="spacing">0</property>
<child>
<widget class="GtkEntry" id="update_entry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="editable">True</property>
<property name="visibility">True</property>
<property name="max_length">140</property>
<property name="text" translatable="yes"></property>
<property name="has_frame">True</property>
<property name="invisible_char">•</property>
<property name="activates_default">False</property>
<signal name="changed" handler="char_counter" last_modification_time="Thu, 08 Apr 2010 19:43:47 GMT"/>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">True</property>
<property name="fill">True</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="update_button">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="label" translatable="yes">Update</property>
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="activate" handler="update_status_callback" last_modification_time="Thu, 08 Apr 2010 19:44:59 GMT"/>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
<child>
<widget class="GtkLabel" id="update_count">
<property name="visible">True</property>
<property name="label" translatable="yes">0/140</property>
<property name="use_underline">False</property>
<property name="use_markup">False</property>
<property name="justify">GTK_JUSTIFY_LEFT</property>
<property name="wrap">False</property>
<property name="selectable">False</property>
<property name="xalign">0.5</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
<property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
<property name="width_chars">-1</property>
<property name="single_line_mode">False</property>
<property name="angle">0</property>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">True</property>
</packing>
</child>
<child>
<widget class="GtkStatusbar" id="status_bar">
<property name="visible">True</property>
<property name="has_resize_grip">True</property>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
</widget>
</child>
</widget>
</glade-interface>

View File

@ -2,16 +2,14 @@
# #
# Custom twitter client... mostly for learning Python # Custom twitter client... mostly for learning Python
import sys, twitter, wx, ConfigParser, os, tkMessageBox, datetime, dateutil.tz import sys, twitter, ConfigParser, os, datetime, dateutil.tz, gtk, gtk.glade, gobject
class TwitWindow(wx.Frame): class MyTwitter():
""" Display Tweets, post to twitter """ """ Display Tweets, post to twitter """
def __init__(self, parent, title): def __init__(self):
wx.Frame.__init__(self, parent, title=title, size=(300,600))
config = ConfigParser.ConfigParser() config = ConfigParser.ConfigParser()
config.read(os.path.expanduser("~/.mytwitter")) config.read(os.path.expanduser("~/.mytwitter"))
self.username = config.get('global', 'username') self.username = config.get('global', 'username')
@ -22,101 +20,45 @@ class TwitWindow(wx.Frame):
self.labels = [] self.labels = []
self.texts = [] self.texts = []
# Authenticate with twitter, set up the API object
self.api = twitter.Api(username=self.username, password=self.password)
# Load up all the GUI stuff
self.init_user_interface('./default.glade')
self.init_widgets() self.init_widgets()
def init_user_interface(self, path_to_skin):
self.widget_tree=gtk.glade.XML(path_to_skin, "window")
self.widget_tree.signal_autoconnect(self)
def init_widgets(self): def init_widgets(self):
main_sizer = wx.BoxSizer(wx.VERTICAL) self.tweet_box = self.widget_tree.get_widget('tweet_box')
self.update_entry = self.widget_tree.get_widget('update_entry')
self.update_count = self.widget_tree.get_widget('update_count')
# Create the scrolled frame that will hold the tweets # Build us some labels...
tweet_view = wx.ScrolledWindow(self)
tweet_sizer = wx.BoxSizer(wx.VERTICAL)
# Status bar
self.CreateStatusBar()
# Menu bar
filemenu = wx.Menu()
filemenu.Append(wx.ID_ABOUT, "&About", "About MyTwitter")
menu_exit = filemenu.Append(wx.ID_EXIT, "E&xit", "Close MyTwitter")
self.Bind(wx.EVT_MENU, self.on_exit, menu_exit)
menu_bar = wx.MenuBar()
menu_bar.Append(filemenu, "&File")
self.SetMenuBar(menu_bar)
# Create labels and text widgets
for i in range(0, self.num_entries): for i in range(0, self.num_entries):
self.labels.append(wx.TextCtrl(tweet_view, style=wx.TE_READONLY)) self.labels.append(gtk.Label())
self.texts.append(wx.TextCtrl(tweet_view, style=wx.TE_MULTILINE | wx.TE_READONLY)) self.tweet_box.pack_start(self.labels[i])
self.labels[i].SetDefaultStyle(wx.TextAttr('DARK_BLUE')) self.labels[i].set_alignment(0.0, 0.0)
self.texts[i].SetDefaultStyle(wx.TextAttr('LIGHT_BLUE')) self.labels[i].set_selectable(True)
tweet_sizer.Add(self.labels[i]) self.labels[i].set_line_wrap(True)
tweet_sizer.Add(self.texts[i]) self.texts.append(gtk.Label())
self.tweet_box.pack_start(self.texts[i])
self.texts[i].set_alignment(0.0, 0.0)
self.texts[i].set_selectable(True)
self.texts[i].set_line_wrap(True)
# Layout the tweet view widget self.tweet_box.show_all()
width,height=tweet_view.GetSizeTuple()
tweet_view.SetSizerAndFit(tweet_sizer)
tweet_view.SetScrollbars(0, 20, 0, height/20)
tweet_view.SetSize((300,400))
# A button to refresh manually # Timer to update periodically
button_sizer = wx.BoxSizer(wx.HORIZONTAL)
refresh_button = wx.Button(self, label="Refresh")
refresh_button.Bind(wx.EVT_BUTTON, self.update_window)
button_sizer.Add(refresh_button)
# Create an update box at the bottom of the window
update_sizer = wx.BoxSizer(wx.HORIZONTAL)
self.update_entry = wx.TextCtrl(self)
self.update_entry.Bind(wx.EVT_TEXT, self.char_counter)
self.update_entry.Bind(wx.EVT_TEXT_ENTER, self.update_status_callback)
update_button = wx.Button(self, label="Update")
update_button.Bind(wx.EVT_BUTTON, self.update_status)
self.update_count = wx.StaticText(self, label="0/140")
update_sizer.Add(self.update_entry, 1, wx.EXPAND)
update_sizer.Add(update_button)
update_sizer.Add(self.update_count)
### Set up bindings
# Bind scrollwheel to move the tweets, as well as page up/down
# fixme: convert this to wxPython
# self.tkroot.bind_all("<Button-4>", self.scroll_wheel)
# self.tkroot.bind_all("<Button-5>", self.scroll_wheel)
# self.tkroot.bind_all("<Up>", self.line_up)
# self.tkroot.bind_all("<Down>", self.line_down)
# self.tkroot.bind_all("<Prior>", self.page_up)
# self.tkroot.bind_all("<Next>", self.page_down)
# Pack the top level together
main_sizer.Add(tweet_view, 1, wx.EXPAND)
main_sizer.Add(button_sizer)
main_sizer.Add(update_sizer)
self.SetSizerAndFit(main_sizer)
# Init the twitter API
self.api = twitter.Api(username=self.username, password=self.password)
# Updates!
self.timer = wx.Timer(self)
self.timer.Bind(wx.EVT_TIMER, self.update_window_callback)
self.timer.Start(self.refresh_time * 1000)
self.update_window() self.update_window()
gobject.timeout_add(self.refresh_time * 100, self.update_window)
# end of init_widgets
def update_window(self): def update_window(self):
print "DEBUG: update_window()"
timezone = dateutil.tz.gettz() timezone = dateutil.tz.gettz()
time_format = "%Y.%m.%d %H:%M:%S %Z" time_format = "%Y.%m.%d %H:%M:%S %Z"
@ -131,33 +73,22 @@ class TwitWindow(wx.Frame):
timestring = timestamp.astimezone(timezone).strftime(time_format) timestring = timestamp.astimezone(timezone).strftime(time_format)
labeltext = user.name + " (" + user.screen_name + ") " + timestring labeltext = user.name + " (" + user.screen_name + ") " + timestring
self.labels[i].Clear() self.labels[i].set_text(labeltext)
self.labels[i].AppendText(labeltext)
# Display the text of the tweet # Display the text of the tweet
self.texts[i].Clear() self.texts[i].set_text(statuses[i].text)
self.texts[i].AppendText(statuses[i].text)
def update_window_callback(self, event):
self.update_window()
def update_status(self): def update_status(self):
text = self.update_entry.GetValue() text = self.update_entry.get_text()
if len(text) > 140: self.update_entry.set_text(none)
d = wx.MessageDialog(self, 'Tweet too long', 'Tweets must be <= 140 characters');
d.ShowModal()
d.Destroy()
return
self.update_entry.Clear()
self.api.PostUpdate(text) self.api.PostUpdate(text)
self.update_window() self.update_window()
# Just calls update_status, here so that things # Just calls update_status, here so that things
# that pass an event can be used # that pass an event can be used
def update_status_callback(self, event): def update_status_callback(self, widget):
self.update_status() self.update_status()
@ -186,12 +117,18 @@ class TwitWindow(wx.Frame):
# self.tweet_view.yview('scroll', 5, 'units'); # self.tweet_view.yview('scroll', 5, 'units');
def char_counter(self, event): def char_counter(self, widget):
new_count = str(len(self.update_entry.GetValue())) + "/140" new_count = str(len(self.update_entry.get_text())) + "/140"
self.update_count.SetLabel(new_count) self.update_count.set_label(new_count)
def gtk_main_quit(self, widget):
gtk.main_quit()
def on_about(self, widget):
print "DEBUG: help->about not yet implemented"
def on_exit(self, event):
self.Close(True)
### end class TwitWindow ### end class TwitWindow
@ -218,7 +155,5 @@ def print_formatted(statuses):
# main # main
app = wx.App(False) my_twitter = MyTwitter()
root = TwitWindow(None, "MyTwitter") gtk.main()
root.Show(True)
app.MainLoop()