Пример #1
0
class Display(object):
    
    def __init__(self):
        
        self.ticker = Ticker()
        self.ticker.update_quote()
        
        pygame.init()
         
        # Set the width and height of the screen [width, height]
        self.size = (SCREEN_WIDTH, SCREEN_HEIGHT)
        self.screen = pygame.display.set_mode(self.size)
         
        pygame.display.set_caption("Ticker LLC")
        self.setup_font()
         
        # Loop until the user clicks the close button.
        self.done = False
         
        # Used to manage how fast the screen updates
        self.clock = pygame.time.Clock()
        self.time_elapsed = pygame.time.get_ticks()

        self.displayList = []
        
    def setup_font(self): 
        if pygame.font:
            
            self.fontBigSize = 34
            self.fontSmallSize = 24
            self.fontBig = pygame.font.Font(None, self.fontBigSize)
            self.fontSmall = pygame.font.Font(None, self.fontSmallSize)
            
 
            #self.bigText = fontBig.render("big font", 1, (10,10,10))
            #self.smallText = fontSmall.render("small font", 1, (10,10,10))
            
            
    def elapsed(self):
        current_time = pygame.time.get_ticks()
        if current_time - self.time_elapsed > 10000 :
            print "10sec elapsed"
            self.time_elapsed = current_time
            self.ticker.update_quote()
            
            self.displayList = []
            for elem in self.ticker.ticker_dict_list :
                name = elem["Name"]
                symbol = elem["Symbol"]
                price = elem["LastTradePriceOnly"]
                self.displayList.append((elem["Name"], elem["Symbol"], elem["LastTradePriceOnly"]))
                print symbol + ": " + price
                


                           
    def text_render(self):
        initX = 50
        initY = 50
        offsetX = 0
        offsetY = 0

        ticker_list = self.ticker.ticker_dict_list        
        for (name, symbol, price) in self.displayList:
 
            bigText = self.fontBig.render(name, 1, (10,10,10))
            self.screen.blit(bigText, (initX+offsetX, initY+offsetY))
            
            offsetX += 0
            offsetY += self.fontBigSize

            smallStr = symbol + "        " + price
            smallText = self.fontSmall.render(smallStr, 1, (10,10,10))
            self.screen.blit(smallText, (initX+offsetX, initY+offsetY))
            
            
            
            offsetX += 0
            offsetY += self.fontSmallSize + self.fontBigSize
                    

    def display_loop(self):
        
        # -------- Main Program Loop -----------
        while not self.done:
            # --- Main event loop
            for event in pygame.event.get(): # User did something
                if event.type == pygame.QUIT: # If user clicked close
                    self.done = True # Flag that we are done so we exit this loop
                    #print "done"
                       
                             
            # --- Game logic should go here
         
            # --- Drawing code should go here
         
            # First, clear the screen to white. Don't put other drawing commands
            # above this, or they will be erased with this command.
            self.screen.fill(WHITE)
            self.text_render()         
            self.elapsed()
         
            
            # --- Go ahead and update the screen with what we've drawn.
            pygame.display.flip()
         
            # --- Limit to 60 frames per second
            self.clock.tick(60)
         
        # Close the window and quit.
        # If you forget this line, the program will 'hang'
        # on exit if running from IDLE.
        pygame.quit()
Пример #2
0
class DisplayTk(object):
    
    def __init__(self):
        
        self.ticker = Ticker()
        if self.ticker.update_quote() == False :
            return
        
        self.rootWin = Tk()
        self.rootWin.protocol("WM_DELETE_WINDOW", self.tk_delete)
        
        self.quit = False
        self.displayDict = {}
        self.entry_count = 0
        self.timer_interval = 10000 #in ms
        

        rowVal = 0
        for elem in self.ticker.ticker_dict_list :

            labelStr = elem["Symbol"] + ": " + elem["LastTradePriceOnly"] 
            price = StringVar()
            symbolWidget = Label(self.rootWin, textvariable=price)
            symbolWidget.grid(row=rowVal, column=0)
            price.set(labelStr)
            
            labelStr = elem["ChangeinPercent"]
            percentChange = StringVar()
            percentWidget = Label(self.rootWin, textvariable=percentChange)
            percentWidget.grid(row=rowVal, column=1)
            percentChange.set(labelStr)
            rowVal += 1
            
            self.color_code(labelStr, symbolWidget, percentWidget)

            
            labelStr = "day: " + elem["DaysRange"]
            daysRange = StringVar()
            Label(self.rootWin, textvariable=daysRange).grid(row=rowVal)
            daysRange.set(labelStr)
            rowVal += 1
            
            labelStr = "52week: " + elem["YearRange"]
            yearRange = StringVar()
            Label(self.rootWin, textvariable=yearRange).grid(row=rowVal)
            yearRange.set(labelStr)
            rowVal += 1
            
            Label(self.rootWin, text="").grid(row=rowVal)
            rowVal += 1
            
            
            self.displayDict[elem["Symbol"]] = \
                (price, percentChange, daysRange, yearRange, symbolWidget, percentWidget)
            

        #threading.Timer(self.timer_interval, self.timer_callback).start()
        
            
    def tk_delete(self):
        print "deletion"
        self.quit = True
        self.rootWin.quit()
        
    def color_code(self, labelStr, symbolWidget, percentWidget):   
            if labelStr[0] == "+" :
                symbolWidget.config(fg = "blue")
                percentWidget.config(fg = "blue")
            else :
                symbolWidget.config(fg = "red")
                percentWidget.config(fg = "red")
                 
        
    def timer_callback(self):
        
        if self.ticker.update_quote() == False :
            self.rootWin.after(self.timer_interval, self.timer_callback)
            return 
        
        if self.quit == True :
            print "quit"
            return

        
        for elem in self.ticker.ticker_dict_list :
            entry = elem["Symbol"]
            (price, percentChange, daysRange, yearRange, symbolWidget, percentWidget) \
                = self.displayDict[entry]
 
            labelStr = elem["Symbol"] + ": " + elem["LastTradePriceOnly"]
            price.set(labelStr)
            
            labelStr = elem["ChangeinPercent"]
            percentChange.set(labelStr)
            self.color_code(labelStr, symbolWidget, percentWidget)
            
            labelStr = "day: " + elem["DaysRange"]
            daysRange.set(labelStr)
            
            labelStr = "52week: " + elem["YearRange"]
            yearRange.set(labelStr)
          
            
            
        #print "timer callback"
        #threading.Timer(self.timer_interval, self.timer_callback).start()                          
        self.rootWin.after(self.timer_interval, self.timer_callback)
        
    def display_loop(self):
        #print self.timer_interval
        self.rootWin.after(self.timer_interval, self.timer_callback)
        mainloop()