Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using text inputs in pygame

import tkinter as tk    
import pygame

pygame.init()
ss = width, height = 1024, 600
screen = pygame.display.set_mode(ss)
tkinput_1 = True    

while True:

    event = pygame.event.poll()
    keys = pygame.key.get_pressed()

    if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()

    screen.fill((0,0,0))

    if tkinput_1 == True:
         tkinput_root1 = tk.Tk()
         tkinput_root1.geometry("200x200")
         tkinput_root1.title("How many teams?")
         tkinput_input1 = tk.Entry(tkinput_root1)
         tkinput_1 = False

    pygame.display.update()

tkinput_root1.mainloop()

This was just me giving the tkinter text input box a shot in pygame. I was fairly sure it wouldn't work out properly but decided I'd try it anyway cause I had nothing to lose. The tkinter screen does not show up till you've exited pygame. So, I'm not asking if anyone knows how to fix the code, but instead if anyone knows the simplest way to create a text box in pygame. I know there is a textinput class module that can be imported. If you believe that is the easiest way to do it then can you walk me through it. If not could you please let me know what you think the easiest way is. It's a small program so I want to avoid a lot of heavy lines of code for a simple text box. Let me know what you guys think. Thanks a lot in advance.

like image 745
Greg Peckory Avatar asked Jul 03 '13 14:07

Greg Peckory


People also ask

How do you write text in pygame?

Pygame does not provide a direct way to write text onto a Surface object. The method render() must be used to create a Surface object from the text, which then can be blit to the screen. The method render() can only render single lines. A newline character is not rendered.

How do you input a text box in Python?

The simplest way to do it is to set an input equal to a variable for later use, and then call the variable later in the program. variable = str(input("Type into the text box."))

Does pygame have a GUI?

Pygame GUI is a module to help you make graphical user interfaces for games written in pygame.


1 Answers

I've tryed using Tk but the thing about using is it stops the whole pygame program when the Tk window pops up and its just not good

this is what i use Pygame InputBox its not the prettiest but it works great just download it and its really easy to use

just import inputbox

then do something like this:

inp = int(inputbox.ask(screen, 'Message')) #inp will equal whatever the input is

this is pretty much like raw_input but for pygame

its not the most aesthetically pleasing but im sure if you search around you can find maybe a nicer one

like image 118
Serial Avatar answered Sep 18 '22 22:09

Serial