Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Tkinter import *
- from PIL import Image, ImageTk
- import time
- import json
- import urllib
- from datetime import datetime
- width = 640
- height = 480
- rss = 'http://www.reddit.com/user/[your username here]/about.json'
- rss_update = 300
- class Gui( Frame ):
- def __init__( self, parent ):
- Frame.__init__( self, parent )
- self.configure( bg='white' )
- self.parent = parent
- self.initUI()
- self.after( 5, self.update )
- def initUI( self ):
- self.parent.title( 'Reddit mon' )
- self.pack( fill=BOTH, expand=1 )
- border = 100
- self.karma = Label( self, bg='white', font=('Arial','150') )
- self.karma.place( x=0, y=border, width=width, height=height-border )
- #link karma
- self.comment = Label( self, bg='white', font=('Arial','70'), anchor='w' )
- self.comment.place( x=border*0.5, y=0, width=width*0.5, height=border )
- # cake day
- cake = Image.open( '/home/pi/reddit/reddit_cake.png' )
- no_cake = Image.open( '/home/pi/reddit/no_cake.png' )
- w, h = cake.size
- ratio = float(h) / float(w)
- w = int(border *0.8)
- h = int(w*ratio)
- self.cakeimg = ImageTk.PhotoImage( cake.resize( (w, h) ) )
- self.nocakeimg = ImageTk.PhotoImage( no_cake.resize( (w, h) ) )
- self.cake = Label( self, bg='white' )
- self.cake.configure( image = self.cakeimg )
- self.cake.place( x=width-(border*2.5), y=0, width=border, height=border )
- # mail
- have_mail = Image.open( '/home/pi/reddit/reddit_mail.png' )
- no_mail = Image.open( '/home/pi/reddit/reddit_nomail.png' )
- w, h = have_mail.size
- ratio = float(h) / float(w)
- w = int(border *0.8)
- h = int(w*ratio)
- self.have_mail = ImageTk.PhotoImage( have_mail.resize( ( w, h ) ) )
- self.no_mail = ImageTk.PhotoImage( no_mail.resize( ( w, h ) ) )
- self.mail = Label( self, bg='white' )
- self.mail.configure( image = self.no_mail )
- self.mail.place( x=width-(border*1.5), y=0, width=border, height=border )
- def update( self ):
- print 'update'
- u = urllib.urlopen( rss ).read()
- j = json.loads(u)
- if 'error' in j:
- print j
- pass
- else:
- # check if today is cake day
- now = datetime.utcnow()
- created = datetime.utcfromtimestamp( j['data']['created_utc'] )
- cake_day = ( created.month == now.month and created.day == now.day )
- self.setKarma( j['data']['link_karma'] )
- self.setComment( j['data']['comment_karma'] )
- self.setCake( cake_day )
- self.setMail( j['data']['has_mail'] )
- #self.karma.set( j['data']['link_karma'] )
- #self.link.set( j['data']['comment_karma'] )
- #self.cake = cake_day
- #self.mail = j['data']['has_mail']
- #time.sleep( rss_update )
- self.after( rss_update*1000, self.update )
- def fill( self, image, color ):
- """Fill image with a color=(r,b,g)."""
- r,g,b = color
- width = image.width()
- height = image.height()
- hexcode = "#%02x%02x%02x" % (r,g,b)
- horizontal_line = "{" + " ".join([hexcode]*width) + "}"
- image.put(" ".join([horizontal_line]*height))
- def setMail( self, have ):
- if have:
- self.mail.configure( image = self.have_mail )
- else:
- self.mail.configure( image = self.no_mail )
- def setKarma( self, karma ):
- self.karma.configure( text = karma )
- def setComment( self, karma ):
- self.comment.configure( text = karma )
- def setCake( self, have ):
- if have:
- self.cake.configure( image = self.cakeimg )
- else:
- self.cake.configure( image = self.nocakeimg )
- def main():
- root = Tk()
- #root.geometry( "%dx%d" % (width, height) )
- root.overrideredirect(True)
- #root.geometry( "%dx%d" % (width, height) )
- root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
- app = Gui( root )
- root.mainloop()
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment