5
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.

2
  • There is actually a way to put a pygame display inside a tkinter app, but it is way more trouble than it is worth. While I did eventually do this, there are so many errors that happened even after it worked fine for a few seconds that I gave up and decided to use EzText Commented May 14, 2014 at 4:28
  • @trevorKirkby And what is that way? Commented Jun 20, 2023 at 20:45

4 Answers 4

7

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

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Christian, I was going to use inputbox originally but I get an error when running it.... AttributeError: 'module' object has no attribute 'join' ... where it says string.join. I've looked through the code and all seems fine but I'm not fully familiar with everything that is happening in the inputbox py file. I am using python 3.3 and pygame 3.3, might that be an issue? Thanks for the reply.
so thats happening when you run the inputbox file or youre main game file
Both, the error occurred when I ran my main game file. I then ran the inputbox.py file and got the same error. I was told that a lot of old functions were removed form the 'string' module, but I'm not sure how I would fix it. I was surprised that looking around on the internet, no one else seemed to encounter the same problem. Any ideas?
it works for me so its definitely because its not compatible with python 3.3 i looked around for one and havent found anything yet ill tell you if i do
No problem wish i could help more Good luck!
2

You can use a small library i'm making, just download it and use it.

To make a simple Text box try:

from GUI import *

input_bow = InLineTextBox((0, 0), 200)  

You must give it at least a postion and the size Then in your input loop, update it :

for e in pygame.event.get():
    input_box.update(e)

and at the end, render it :

input_box.render(screen)

You can get the text with input_box.text at any moment

Comments

1

I recommend using EzText. It's a great way to add text inputs into your pygame programs. I have personally used it my self and it is nice and easy and works with python 3.3.2.

http://www.pygame.org/project-EzText-920-.html

Comments

1

Regarding what you said about the error with InputBox, all you have to do find and replace everywhere it says (without the quotation marks) string.join(current_string,"") with "".join(current_string), since I think this is new in python 3.4, anyway, hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.