I have some code that works perfectly, but am now trying to move it into a GUI. The issue so far is that I'm using pygame, and I have so far only managed to show text from a single assigned string.
# --- calculate all variables
for i in range(0, num_rows):
ticker['ticker{0}'.format(i+1)] = df['Ticker'][i] #could also use 'ticker'+str(i) instead of .format
tickerci['tickerci{0}'.format(i+1)] = df['Carbon Intensity'][i]
tickerobject['tickerobject{0}'.format(i+1)] = yf.Ticker(ticker['ticker{0}'.format(i+1)])
tickervalue['tickervalue{0}'.format(i+1)] = (tickerobject['tickerobject{0}'.format(i+1)]).info['marketCap']
tickerprice['tickerprice{0}'.format(i+1)] = (tickerobject['tickerobject{0}'.format(i+1)]).info['regularMarketPrice']
socialcost['socialcost{0}'.format(i+1)] = round((tickervalue['tickervalue{0}'.format(i+1)])*(tickerci['tickerci{0}'.format(i+1)])*carbonprice)
print ("the current price of ", ticker['ticker'+str(i+1)]," is €", tickerprice['tickerprice'+str(i+1)], ".")
print ("the current market cap of ", ticker['ticker'+str(i+1)]," is €", tickervalue['tickervalue'+str(i+1)], ".")
print ("the current social cost of ", ticker['ticker'+str(i+1)],"'s CO2 emissions is €", socialcost['socialcost'+str(i+1)])
print ()
# --- main ---
pygame.init()
screen = pygame.display.set_mode((960, 720))#, pygame.FULLSCREEN, vsync=1)
screen_rect = screen.get_rect()
font = pygame.font.SysFont(None, 50)
description = "put the text to display here"
desc_text = font.render(description, True, (255, 255, 255))
desc_rect = desc_text.get_rect(left=screen_rect.right)
Basically I want to assign the outputs of the print command in the loop to a string (or a string dictionary), which can then be called in the "description" part of the pygame module.
The end goal here is to have the strings that are currently printed, shown scrolling endlessly like a marquee.
There should be some way of modifying this basic bit of string io to work, I think, but I haven't been able to figure it out yet.
import io
def print_to_string(*args, **kwargs):
output = io.StringIO()
print(*args, file=output, **kwargs)
contents = output.getvalue()
output.close()
return contents