我需要在屏幕上打印我的计算机接口的 ips,我使用的库只让我打印 IPv6 和 IPv4 地址,我只希望打印 IPv4。
这段代码将在“OpenMediaVault”linux 服务器上运行(它没有 ifconfig 但没有 ifaddr 或类似的东西),我是一个完整的 python 菜鸟,而不是 linux 的高级用户。
import time
import pygame
import sys
import ifaddr
display_width = 480
display_height = 320
white = (255, 255, 255)
black = (0, 0, 0)
fontSize = 20
adapters = ifaddr.get_adapters()
pygame.init()
screen = pygame.display.set_mode((display_width,display_height),pygame.FULLSCREEN)
def text_objects(text, font):
textSurface = font.render(text, True, white)
return textSurface, textSurface.get_rect()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
for adapter in adapters:
#print (adapter.nice_name + ":")
for ip in adapter.ips:
#print (ip.ip)
IP = ip.ip
largeText = pygame.font.Font(pygame.font.get_default_font(),fontSize)
TextSurf,TextRect=text_objects((adapter.nice_name + ":"),largeText)
TextRect.center = ((display_width/2),(1*(display_height/4)))
screen.blit(TextSurf, TextRect)
largeText = pygame.font.Font(pygame.font.get_default_font(),fontSize)
TextSurf,TextRect=text_objects((IP), largeText)
TextRect.center = ((display_width/2),(3*(display_height/4)))
screen.blit(TextSurf, TextRect)
time.sleep(1)
screen.fill((0,0,0))
pygame.display.flip()
pygame.display.update()
我得到的错误是因为我尝试使用 pygames 在屏幕上打印:
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "d:\Desktop\***\file.py", line 45, in <module> TextSurf, TextRect = text_objects((IP), largeText)
File "d:\Desktop\***\file.py", line 17, in text_objects textSurface = font.render(text, True, white)
TypeError: text must be a unicode or bytes
慕工程0101907
相关分类