Guest User

Untitled

a guest
Oct 5th, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.94 KB | None | 0 0
  1. from Tkinter import *
  2. from PIL import Image, ImageTk
  3. import time
  4. import json
  5. import urllib
  6. from datetime import datetime
  7.  
  8. width = 640
  9. height = 480
  10. rss = 'http://www.reddit.com/user/[your username here]/about.json'
  11. rss_update = 300
  12.  
  13. class Gui( Frame ):
  14.         def __init__( self, parent ):
  15.                 Frame.__init__( self, parent )
  16.                 self.configure( bg='white' )
  17.                 self.parent = parent
  18.                 self.initUI()
  19.  
  20.                 self.after( 5, self.update )
  21.  
  22.         def initUI( self ):
  23.                 self.parent.title( 'Reddit mon' )
  24.                 self.pack( fill=BOTH, expand=1 )
  25.  
  26.                 border = 100
  27.  
  28.                 self.karma = Label( self, bg='white', font=('Arial','150') )
  29.                 self.karma.place( x=0, y=border, width=width, height=height-border )
  30.  
  31.                 #link karma
  32.                 self.comment = Label( self, bg='white', font=('Arial','70'), anchor='w' )
  33.                 self.comment.place( x=border*0.5, y=0, width=width*0.5, height=border )
  34.  
  35.                 # cake day
  36.                 cake = Image.open( '/home/pi/reddit/reddit_cake.png' )
  37.                 no_cake = Image.open( '/home/pi/reddit/no_cake.png' )
  38.  
  39.                 w, h = cake.size
  40.                 ratio = float(h) / float(w)
  41.                 w = int(border *0.8)
  42.                 h = int(w*ratio)
  43.  
  44.                 self.cakeimg = ImageTk.PhotoImage( cake.resize( (w, h) ) )
  45.                 self.nocakeimg = ImageTk.PhotoImage( no_cake.resize( (w, h) ) )
  46.  
  47.                 self.cake = Label( self, bg='white' )
  48.                 self.cake.configure( image = self.cakeimg )
  49.                 self.cake.place( x=width-(border*2.5), y=0, width=border, height=border )
  50.  
  51.                 # mail
  52.                 have_mail = Image.open( '/home/pi/reddit/reddit_mail.png' )
  53.                 no_mail = Image.open( '/home/pi/reddit/reddit_nomail.png' )
  54.  
  55.                 w, h = have_mail.size
  56.                 ratio = float(h) / float(w)
  57.                 w = int(border *0.8)
  58.                 h = int(w*ratio)
  59.  
  60.                 self.have_mail = ImageTk.PhotoImage( have_mail.resize( ( w, h ) ) )
  61.                 self.no_mail = ImageTk.PhotoImage( no_mail.resize( ( w, h ) ) )
  62.  
  63.                 self.mail = Label( self, bg='white' )
  64.                 self.mail.configure( image = self.no_mail )
  65.                 self.mail.place( x=width-(border*1.5), y=0, width=border, height=border )
  66.  
  67.         def update( self ):
  68.                 print 'update'
  69.  
  70.  
  71.                 u = urllib.urlopen( rss ).read()
  72.                 j = json.loads(u)
  73.  
  74.                 if 'error' in j:
  75.                         print j
  76.                         pass
  77.  
  78.                 else:
  79.                         # check if today is cake day
  80.                         now = datetime.utcnow()
  81.                         created = datetime.utcfromtimestamp( j['data']['created_utc'] )
  82.                         cake_day = ( created.month == now.month and created.day == now.day )
  83.  
  84.                         self.setKarma( j['data']['link_karma'] )
  85.                         self.setComment( j['data']['comment_karma'] )
  86.                         self.setCake( cake_day )
  87.                         self.setMail( j['data']['has_mail'] )
  88.  
  89.                         #self.karma.set( j['data']['link_karma'] )
  90.                         #self.link.set( j['data']['comment_karma'] )
  91.                         #self.cake = cake_day
  92.                         #self.mail = j['data']['has_mail']
  93.  
  94.                 #time.sleep( rss_update )
  95.                 self.after( rss_update*1000, self.update )
  96.  
  97.         def fill( self, image, color ):
  98.                 """Fill image with a color=(r,b,g)."""
  99.                 r,g,b = color
  100.                 width = image.width()
  101.                 height = image.height()
  102.                 hexcode = "#%02x%02x%02x" % (r,g,b)
  103.                 horizontal_line = "{" + " ".join([hexcode]*width) + "}"
  104.                 image.put(" ".join([horizontal_line]*height))
  105.  
  106.         def setMail( self, have ):
  107.                 if have:
  108.                         self.mail.configure( image = self.have_mail )
  109.                 else:
  110.                         self.mail.configure( image = self.no_mail )
  111.  
  112.         def setKarma( self, karma ):
  113.                 self.karma.configure( text = karma )
  114.  
  115.         def setComment( self, karma ):
  116.                 self.comment.configure( text = karma )
  117.  
  118.         def setCake( self, have ):
  119.                 if have:
  120.                         self.cake.configure( image = self.cakeimg )
  121.                 else:
  122.                         self.cake.configure( image = self.nocakeimg )
  123.  
  124. def main():
  125.         root = Tk()
  126.         #root.geometry( "%dx%d" % (width, height) )
  127.  
  128.         root.overrideredirect(True)
  129.         #root.geometry( "%dx%d" % (width, height) )
  130.  
  131.         root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
  132.  
  133.         app = Gui( root )
  134.         root.mainloop()
  135.  
  136.  
  137. if __name__ == '__main__':
  138.         main()
Add Comment
Please, Sign In to add comment