Reputation: 183
So, I'm developing a client-server application, in which the client request some information for the server, and upon receiving it, the client displays the information on a Pygame screen. The problem is that the information I get from the server comes in the form of a list (because I used the pickle library to send it), and I'm having trouble breaking this list into separate lines to fit the screen.
For instance, this is a code from the server side to send information about the server's cpu usage, memory, and such:
def mostra_uso_cpu_e_ram(socket_cliente):
info1 =('Usuario solicitou Informações de uso de processamento e Memória')
resposta = []
cpu = ('Porcentagem: ',psutil.cpu_percent())
resposta.append(cpu)
processador = ('Nome e modelo do processador: ',cpuinfo.get_cpu_info()['brand'])
resposta.append(processador)
arquitetura = ('Arquitetura: ',cpuinfo.get_cpu_info()['arch'])
resposta.append(arquitetura)
palavra = ('Palavra do processador:', cpuinfo.get_cpu_info()['bits'])
resposta.append(palavra)
ftotal = ('Frequência total do processador:', cpuinfo.get_cpu_info()['hz_actual'])
resposta.append(ftotal)
fuso = ('Frequência de uso:', psutil.cpu_freq()[0])
resposta.append(fuso)
disk = ('Porcentagem de uso do disco:', psutil.disk_usage('/')[3])
resposta.append(disk)
mem = psutil.virtual_memory()
mem_percent = mem.used/mem.total
memoria = ('Porcentagem de memória usada:', mem_percent)
resposta.append(memoria)
bytes_resp = pickle.dumps(resposta)
socket_cliente.send(bytes_resp)
print(info1)
And this is the client side:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((socket.gethostname(), 9999))
bytes_resp = s.recv(1024)
lista = pickle.loads(bytes_resp)
# Pygame screen and surface settings
largura_tela = 800
altura_tela = 600
tela = pygame.display.set_mode((largura_tela, altura_tela))
background = pygame.Surface(tela.get_size())
tela.blit(background, (0,0))
#function used to break lines but shows nothing
#indentation seems a little lot to the right because it's inside another block of text
def drawText(surface, text, rect, color, font, aa=False, bkg=None):
rect = pygame.Rect(rect)
text = str(lista)
y = rect.top
lineSpacing = -2
fontHeight = font.size("Tg")[1]
while text:
i = 1
if y + fontHeight > rect.bottom:
break
# determine maximum width of line
while font.size(text[:i])[0] < rect.width and i < len(text):
i += 1
# if we've wrapped the text, then adjust the wrap to the last word
if i < len(text):
i = text.rfind(" ", 0, i) + 1
# render the line and blit it to the surface
if bkg:
image = font.render(text[:i], 1, color, bkg)
image.set_colorkey(bkg)
else:
image = font.render(text[:i], aa, color)
surface.blit(image, (rect.left, y))
y += fontHeight + lineSpacing
text = text[i:]
return text
drawText(background, str(lista), (30,400,1300,350), verde, font, aa=True, bkg=None)
The list I'm trying to render is this one:
[('Porcentagem: ', 15.2), ('Nome e modelo do processador: ', 'Intel(R) Core(TM) i5-4288U CPU @ 2.60GHz'), ('Arquitetura: ', 'X86_64'), ('Palavra do processador:', 64), ('Frequência total do processador:', '2.6000 GHz'), ('Frequência de uso:', 2600), ('Porcentagem de uso do disco:', 11.1), ('Porcentagem de memória usada:', 0.48642539978027344)]
My question is: how can I break these lists into lines, or at the very least make them fit the pygame surface borders? Got this function from the pygame site but I'm not sure where am I going wrong here?
Upvotes: 0
Views: 91
Reputation: 123473
Here's how to convert the list into a list of lines of text:
lst = [('Porcentagem: ', 15.2), ('Nome e modelo do processador: ', 'Intel(R) Core(TM) i5-4288U CPU @ 2.60GHz'), ('Arquitetura: ', 'X86_64'), ('Palavra do processador:', 64), ('Frequência total do processador:', '2.6000 GHz'), ('Frequência de uso:', 2600), ('Porcentagem de uso do disco:', 11.1), ('Porcentagem de memória usada:', 0.48642539978027344)]
lines = [''.join(map(str, items)) for items in lst]
print(lines)
Output:
['Porcentagem: 15.2', 'Nome e modelo do processador: Intel(R) Core(TM) i5-4288U CPU @ 2.60GHz', 'Arquitetura: X86_64', 'Palavra do processador:64', 'Frequência total do processador:2.6000 GHz', 'Frequência de uso:2600', 'Porcentagem de uso do disco:11.1', 'Porcentagem de memória usada:0.48642539978027344']
I can't easily test it since you haven't provided a MRE. One potential problem might be that the drawText()
function looks like it's assuming the characters in the font are all the same width, which isn't true for all fonts.
Upvotes: 1