Python Forum
[PyGame] Help with Font size in Python-Raspi
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Help with Font size in Python-Raspi
#1
I managed to get a BTC ticker running on my raspi but the font is too small. Have no idea what I am doing, I am surprised I got this far.

This is the code I am running;


#!/usr/bin/env python

# Author Tom Fyuri.
# Pillaged by aybase ~07-2016 for use with Raspberry PI using 2.8" TFT display
# Simple bitcoin usd ticker (btc-e), you're free to modify or share following code however you like.
# Version 0.0.6.

import threading
import pygame
import httplib
import urllib
import json
import time
import sys, os
import copy
from pygame.locals import *

# setup some global stuff
pygame.init()
screen = pygame.display.set_mode((320,240),pygame.FULLSCREEN)
pygame.mouse.set_visible(False)
pygame.display.set_caption('BTC-e Live')
surface = pygame.Surface(screen.get_size())
surface = surface.convert()
font = pygame.font.SysFont(None, 55)
font2 = pygame.font.SysFont(None, 55)
font3 = pygame.font.SysFont(None, 16)
update_time = 1500
tickrate = 50

class currency_data():
  pass

ticker_data = [["btc_usd","BTC"," $"," B",currency_data(),0]
]

def quit():
    pygame.quit(); sys.exit()

def get_price(which): # 
  try:
    conn = httplib.HTTPSConnection("btc-e.com", timeout=4)
    conn.request("GET", "/api/2/"+which+"/ticker")
    response = conn.getresponse()
    j = json.load(response)
    conn.close()
    return j
  except StandardError:
    return None
  
def get_depth(which): # 
  try:
    conn = httplib.HTTPSConnection("btc-e.com")
    conn.request("GET", "/api/2/"+which+"/depth")
    response = conn.getresponse()
    j = json.load(response)
    conn.close()
    return j
  except StandardError:
    return None
  
def process_input():
  key = pygame.key.get_pressed()
  for event in pygame.event.get():
      if event.type == KEYDOWN:
      if event.key == K_ESCAPE: quit()
      
def update_data():
  # basic stuff
  for i in ticker_data:
    currency_name = i[0]
    i[5] = copy.deepcopy(i[4]) # save previous?
    pdata = i[5]
    data = i[4]
    data.name = i[1]
    data.nom = i[2]
    data.nom2 = i[3]
    json_data = get_price(currency_name)
    if json_data is None:
      data.error = True
    else:
      data.error = False
      data.last = json_data['ticker']['last']
      volume = 0
      #data.volume = json_data['ticker']['vol_cur']
      
      # more interesting thing, fattest walls
      json_data = get_depth(currency_name)
      if json_data is None:
    data.error = True
      else:
    data.error = False

    
    if ((hasattr(data,'error')) and (hasattr(pdata,'error')) and (data.error == False) and (pdata.error == False)):

    
      if hasattr(data,'last_color'):
    data.last_color = pdata.last_color
      else:
    # WHITE originally 255, 255, 255  
    data.last_color = (242, 204, 133)
      if (data.last > pdata.last):
    # GREEN originally 0, 255, 0
    data.last_color = (49, 181, 49)
      elif (data.last < pdata.last):
    # RED   originally 255, 0, 0
    data.last_color = (209, 29, 29)

    
def redraw():
  surface.fill((0, 0, 0))
  pos = 37; pos2 = 37;
  for i in ticker_data:
    data = i[4]
    if data.error:
      text = font.render("ERROR", 1, (44, 96, 209))
      text_pos = text.get_rect(); text_pos.y = pos; text_pos.x = 98
      surface.blit(text, text_pos)
    else:
      if (hasattr(data,'last_color')):
    color = data.last_color
      else:
    color = (242, 204, 133)
      text = font2.render("{0}{1}".format(round(data.last,5),data.nom), 1, color)
         
      text_pos = text.get_rect(); text_pos.y = pos2; text_pos.x = 118
      surface.blit(text, text_pos)
      
    # name
    text = font.render(data.name, 1, (242, 204, 133))
    text_pos = text.get_rect(); text_pos.y = pos
     
    text_pos.x = 15
    surface.blit(text, text_pos)
  
    pos+=65
    pos2+=65
    
  screen.blit(surface, (0, 0))
  pygame.display.flip()

def main():
  clock = pygame.time.Clock()
  update_delay = 0
  update_data()
  redraw()
  while True:
    process_input()
    update_delay = update_delay + tickrate
    if (update_delay >= update_time):
      update_delay = 0
      update_data()
    redraw()
    clock.tick(tickrate)

if __name__ == '__main__': main()
Any help is apreciated!
Reply
#2
your font sizes are the 55, 55, and 16 for each object.
Quote:
font = pygame.font.SysFont(None, 55)
font2 = pygame.font.SysFont(None, 55)
font3 = pygame.font.SysFont(None, 16)
Recommended Tutorials:
Reply
#3
(Jan-23-2017, 08:22 PM)metulburr Wrote: your font sizes are the 55, 55, and 16 for each object.
Quote:
font = pygame.font.SysFont(None, 55)
font2 = pygame.font.SysFont(None, 55)
font3 = pygame.font.SysFont(None, 16)

Thanks for the assistance. Now we are getting somewhere; this is what it looks like now though, the text is all squished about.

[Image: K15Q7fH.jpg]
Reply
#4
what did you set your text sizes to so i can replicate?

I am not sure what all the possible outcome text could be from that...but your positioning of your text objects is over complex than what it should be. A proper text object position to the center would be using the screen's rect center attribute. And if you had too much you would need to account for line wrapping, or always use two lines for simplicity.

I have more of a feeling its hard coding text positions when you dont know the size dimensions yet such as:
Quote:
      text_pos = text.get_rect(); text_pos.y = pos2; text_pos.x = 118
...
    pos+=65
    pos2+=65
...
      text_pos = text.get_rect(); text_pos.y = pos; text_pos.x = 98
...
    text_pos.x = 15
This first line places the drawing x position at 118 pixels...but you dont account for the width of the text. So you are just arbitrarily drawing it on the screen somewhere if you change the text sizes.

The proper way to handle text objects positioning. You dont need to do anyhting other than text_rect.center = screen_rect.center
have all your string as text, text_rect is your rect of the current text updated every frame, and draw that object to the center of the screen. Then it wouldnt matter what size the text is.

This doesnt change the size of the text,  but it does change the text string itself. And it would do the same if the size was changed each time. The text object is always centered on the screen. Regardless of whether the string is 1 digit or 10 digits, size of text, font style, etc.
https://github.com/metulburr/pygame_code...r_timed.py

To be honest your whole code is quite sloppy. If i did it i would replace 90% of your code. You should use format method instead of concatenating strings. I would use a class for the number to clean up some of the code.
Recommended Tutorials:
Reply
#5
Thanks for the help, I am quite new to this. I found the code through a Youtube video. Just trying to edit it to fit my needs as best as possible.

Text sizes are 105, 105, 65
Reply
#6
Any ideas?

Maybe someone can help me write some simple code to display Bitcoin ticker and btc balance of a selected wallet?

Would be willing to donate for the troubles :D
Reply
#7
I know pygame...but i dont know anything about bitcoins (other than the fact that it is a currency). Like i have no idea what a bitcoin ticker is, nor what BTC means. If all you are doing is displaying a number you dont really need pygame....you could easily use any GUI library...not a gaming library. Does this program include graphs? If you just want a value from a website, inlcude the website and the data you want from it. If you have to be logged in, take the html and tell us what you want from it.

Also i dont have a resp pi to test it on. 99% of the time it wouldnt matter, it should be the same...but i know from experience there was once a problem with pygame and using RGB values VS hex values....but the problem only existed on rasp pi.
Recommended Tutorials:
Reply
#8
(Jan-30-2017, 08:59 PM)metulburr Wrote: I know pygame...but i dont know anything about bitcoins (other than the fact that it is a currency). Like i have no idea what a bitcoin ticker is, nor what BTC means. If all you are doing is displaying a number you dont really need pygame....you could easily use any GUI library...not a gaming library. Does this program include graphs? If you just want a value from a website, inlcude the website and the data you want from it. If you have to be logged in, take the html and tell us what you want from it.

Also i dont have a resp pi to test it on. 99% of the time it wouldnt matter, it should be the same...but i know from experience there was once a problem with pygame and using RGB values VS hex values....but the problem only existed on rasp pi.

Like this would need to show the current BTC price and then somehow point to a BTC wallet balance. Perhaps you can PM me.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code for alignment and font size 1418 0 275 Jan-14-2024, 03:56 AM
Last Post: 1418
  How to do "fixed size" (wrapping) math in Python? AlexanderWulf 13 1,733 Jul-19-2023, 04:13 PM
Last Post: deanhystad
Question Opening small size browser with python selenium not work, need help greenpine 0 1,587 Feb-07-2022, 11:36 AM
Last Post: greenpine
  How to set Tab size to 4 in Python interpreter? zzzhhh 1 1,796 Jan-18-2022, 12:11 PM
Last Post: snippsat
  Load external font and does not show font in the window ATARI_LIVE 16 7,982 Feb-05-2021, 10:36 PM
Last Post: EthanPayne
  Raspi Commands via Python (VS Code) Script AS4188 5 2,740 Oct-21-2020, 03:49 PM
Last Post: AS4188
  how to compare two different size images in python and find corresponding pixel value squidsirymchenry 1 4,514 Feb-03-2020, 06:48 AM
Last Post: michael1789
  size of set vs size of dict zweb 0 2,118 Oct-11-2019, 01:32 AM
Last Post: zweb
  Python function call font color JeffR1992 1 3,372 Apr-17-2019, 06:38 AM
Last Post: perfringo
  Is there a way to detect the text font, size and color from an image in python? Maia07 2 8,582 Aug-23-2018, 01:16 PM
Last Post: Maia07

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020