显示用户的计算机名称

Displaying the user's computer name

我想在 Fourhundredone() 中显示用户的计算机名称,但错误一直告诉我我需要一个整数? (TypeError: an integer is required (got type str))有什么方法可以更正确地显示它吗?

import socket
computer_name = socket.gethostname()
    def draw_text2(text, computer_name, text2, font, color, surface, x, y):
        font = pygame.font.Font("PixelDigivolve-mOm9.ttf",100)
        text = font.render(text + str(computer_name), text2, 1, color)
        text2= font.render(text2, 1, color)
        textRect = text.get_rect()
        textrect = textobj.get_rect()
        textrect.topleft = (x, y)
        surface.blit(textobj, textrect)

    def Fourhundredone():
        running = True
        while running:
             display_background(screen, clubroom)
             display_mai(screen,character_Mai)
             display_textbox()
             draw_text("Mai", font2, blood_red, screen, 2, 522)
             draw_text2("\"We can't be like you", computer_name, "!\"", font3, color, screen, 15, 580)

             for event in pygame.event.get():
                if event.type == QUIT:
                    running = False
                    pygame.quit()
                    sys.exit()
                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        running = False
                    if event.key == K_SPACE:
                        Fourhundredtwo()

             pygame.display.update()
             mainClock.tick(10)

您的问题似乎不在于计算机名。

text = font.render(text + str(computer_name), text2, 1, color)

看起来不对。你想做 (+ 而不是 ,):

text = font.render(text + str(computer_name) + text2, 1, color)

在问题范围之外,我建议传递一个已预先格式化的字符串,而不是根据您要打印的字符串定制您的函数。

类似

def draw_text2(text, font, color, surface, x, y):

并调用:

draw_text2(f"\"We can't be like you {computer_name}!\"", font3, color, screen, 15, 580)

甚至

draw_text2("\"We can't be like you" + computer_name + "!\"", font3, color, screen, 15, 580)

如果您不想为字符串格式设置而烦恼(尽管您应该这样做,但它更简洁)。

以任何方式组成字符串,将其作为预先格式化的单个参数传递将使您的函数更易于重用。