我需要在屏幕上打印我的计算机接口的 ips,我使用的库只让我打印 IPv6 和 IPv4 地址,我只希望打印 IPv4。这段代码将在“OpenMediaVault”linux 服务器上运行(它没有 ifconfig 但没有 ifaddr 或类似的东西),我是一个完整的 python 菜鸟,而不是 linux 的高级用户。import timeimport pygameimport sysimport ifaddrdisplay_width = 480display_height = 320white = (255, 255, 255)black = (0, 0, 0)fontSize = 20adapters = 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.4Hello from the pygame community. https://www.pygame.org/contribute.htmlTraceback (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
1 回答
慕工程0101907
TA贡献1887条经验 获得超5个赞
IPv4 地址是一个字符串,IPv6 地址是一个元组。所以只需检查是否ip是一个字符串,并跳过它。
for ip in adapter.ips:
if not isinstance(ip.ip, str):
continue
#rest of loop body goes here
添加回答
举报
0/150
提交
取消