因此,我正在开发一个客户机-服务器应用程序,在该应用程序中,客户机向服务器请求一些信息,收到这些信息后,客户机会在Pygame屏幕上显示这些信息。问题是,我从服务器获得的信息是以列表的形式出现的(因为我使用pickle库发送它),并且我很难将此列表分成单独的行以适应屏幕
例如,这是来自服务器端的代码,用于发送有关服务器cpu使用率、内存等的信息:
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)
这是客户端:
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)
我试图呈现的列表如下所示:
[('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)]
我的问题是:如何将这些列表分成几行,或者至少让它们符合pygame的表面边界?从pygame网站上获得了这个功能,但我不确定我在这里哪里出错了
下面介绍如何将列表转换为文本行列表:
输出:
因为您没有提供MRE,所以我无法轻松测试它。一个潜在的问题可能是
drawText()
函数看起来好像假定字体中的字符宽度都相同,但并非所有字体都是如此相关问题 更多 >
编程相关推荐