Python Forum

Full Version: [pyGame] Text with Input, help needed !
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey Game Programmers and Software Developers...

I try to create tekst with an input, so you can typ any thing on your keyboard,
and it render into the pygame window... but it don't works...

This is my script:

import pygame
import sys

pygame.init()

WHITE = (255, 255, 255)
RED = (0, 255, 0)
BLACK = (0, 0, 0)

size = (640, 480)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("JCads Develop")


done = False
clock = pygame.time.Clock()
  
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                done = True

    
          
    screen.fill(WHITE)
    pygame.draw.line(screen, RED, [0, 0], [100, 100], 5)

    font = pygame.font.SysFont('Calibri', 25, True, False)
    text = font.render(input("Your Name: "), True, BLACK)
    screen.blit(text, [250, 250])
    
    pygame.display.flip()

    clock.tick(60)
pygame.quit()
sys.exit()
Can anyone help me to give an example, about how i can give text an input, just an player
or user of my program can input the text from the keyboard ?...

Thanks for help, Jamie.
Quote:I try to create tekst with an input, so you can typ any thing on your keyboard,
and it render into the pygame window... but it don't works...
What do you mean by it doesnt work?!? If i run your script i can change the text each time i press enter. What do you want it to do?

P.S. Taking input from terminal/command prompt in A GUI program is bad. Its as bad as have time.sleep() in a GUI program. None of that should be there as it halts the updates to drawing to the screen as well. It would be better to build an input box and button like a GUI toolkit.

This is basically a wrapper around mekires code, i just cant find his repo at the moment. But this has just the button and input box.
https://github.com/metulburr/pygooey