如何设置此TextBox显示位置的位置?

时间:2016-12-19 18:02:41

标签: python textbox position pygame

我对pygame很新,并且想知道如何选择此文本框的显示位置,因为现在它总是在同一位置显示文本框。 理想情况下,我会像这样调用函数:"function("Hi, how are you", (x,y)),虽然我不确定如何实现它。

(我环顾四周,无法在stackoverflowreddit找到任何答案,但一无所获。

def textBox(Question, Position):
    #screen.fill((255,255,255))# fill the screen w/ white
    question = eztext.Input(maxlength=100, color=black, prompt=Question)#Create an input with max length 45.
    clock = pygame.time.Clock() # create the pygame clock
    run = True
    while run == True:
        clock.tick(30) # make sure the program is running at 30 fps
        events = pygame.event.get() # events for txtbx
        for event in events:
            if event.type == QUIT: # close it if x button is pressed
                return
            if event.type == KEYDOWN:
                if event.key == K_RETURN:
                    textBox.userResponse = question.value
                    run = False
        screen.fill((255,255,255)) # clear the screen
        question.update(events) # update txtbx
        question.draw(screen) # blit txtbx on the sceen
        pygame.display.flip() # refresh the display

2 个答案:

答案 0 :(得分:0)

获取source code of eztext,在编辑器中打开,您将看到可以执行的操作:

question = eztext.Input(x=10, y=20, ...)

question.set_pos(10, 20)

源代码:

class Input:
    """ A text input for pygame apps """
    def __init__(self, **options):
        """ Options: x, y, font, color, restricted, maxlength, prompt """
        self.options = Config(options, ['x', '0'], ['y', '0'], ['font', 'pygame.font.Font(None, 32)'],
                              ['color', '(0,0,0)'], ['restricted', '\'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&\\\'()*+,-./:;<=>?@[\]^_`{|}~\''],
                              ['maxlength', '-1'], ['prompt', '\'\''])
        self.x = self.options.x; self.y = self.options.y
        self.font = self.options.font
        self.color = self.options.color
        self.restricted = self.options.restricted
        self.maxlength = self.options.maxlength
        self.prompt = self.options.prompt; self.value = ''
        self.shifted = False

    def set_pos(self, x, y):
        """ Set the position to x, y """
        self.x = x
        self.y = y

渲染文字

  • 选择字体和大小

    font = pygame.font.Font(None, 40)
    
  • 将文本渲染为pygame.surface.Surface()

    text = font.render("Hello World", 1, (255,0,0))
    
  • 显示在位置(10,20)

    surface.blit(text, (10, 20))
    

您可以使用pygame.Rect()来保持位置

  • 创建文字大小的矩形

    text_rect = text.get_rect()
    
  • 更改位置

    text_rect.x = 10
    text_rect.y = 20
    
  • text_rect.topleft = (10, 20)
    
  • 或屏幕中心(或按钮等其他表面)

    text_rect.cetner = screen.get_rect().center
    
  • 使用rect

    显示
    surface.blit(text, text_rect)
    

答案 1 :(得分:0)

这是一种创建文本渲染功能的非常简单的方法:

def RenderText(Text, Font, Target, X, Y, R, G, B): # The target is your screen
    """"Text , font, target surface, X, Y, 
    and color (RGB)"""
    RenderedText = Font.render(Text, True, (R, G, B))
    Target.blit(RenderedText, (X, Y))

使用它: 加载字体:

default_font = pygame.font.SysFont(None, 30) 

请参阅the wiki了解字体自定义

在你的循环中申请:

... # Loop Stuff

# I assume that your screen is named `screen`         X   Y   Color in RGB
RenderText('Hi, how are you?', default_font, screen, 50, 50, 255, 255, 255)
# Experiment with all the above args

我没有使用你的eztext课程,但尝试这种方法,这很简单。