Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] text-to-screen help
#1
hey I want to blit user input to screen, I know how to blit text:

(example, not real code)

text = font.render("Enter Text Here", True, color)
screen.blit(text,(position))

So, i put in:

(example, not real code)

input_variable = input("prompt")

text = font.render(input_variable, True, color)
screen.blit(text,(position))

but then a message comes up saying
"TypeError: text must be string or unicode"
it's talking about the line where I define text because input_variable is not in quotes, but when I put it in quotes the text appears the literal words input_variable. how do I fix this
Quote
#2
What version of Python are you using? In 2.x, the input function tries to evaluate the input before returning it, so you could be getting an integer instead of a string.

If that's the problem, use raw_input.
Craig "Ichabod" O'Brien - xenomind.com
Buddhist, biker, poet, coder, theist
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
i'm using 2.7
the input im getting is an supposed to be an integer
Quote
#4
Im not sure why you are using the terminal to take input on a game using a window?

https://github.com/Mekire/pygame-textbox
Mekire likes this post
*Describe the environment in which it occurs.                                    *Describe the symptoms of your problem clearly.
*Describe the research you did to try and understand the problem.      *Describe the goal, not the step. 
*Use meaningful, specific subject headers                                          *Write in clear, grammatical, correctly-spelled language
*Describe the problem's symptoms, not your guesses                         *Describe your problem's symptoms in chronological order
*Describe the diagnostic steps you took to try and pin down the problem yourself.
*Describe any possibly relevant recent changes in your computer or software configuration. 
*Provide a way to reproduce the problem in a controlled environment.
Quote
#5
ok, how would I take input another way?
Quote
#6
graphically

https://github.com/Mekire/pygame-textbox...example.py
*Describe the environment in which it occurs.                                    *Describe the symptoms of your problem clearly.
*Describe the research you did to try and understand the problem.      *Describe the goal, not the step. 
*Use meaningful, specific subject headers                                          *Write in clear, grammatical, correctly-spelled language
*Describe the problem's symptoms, not your guesses                         *Describe your problem's symptoms in chronological order
*Describe the diagnostic steps you took to try and pin down the problem yourself.
*Describe any possibly relevant recent changes in your computer or software configuration. 
*Provide a way to reproduce the problem in a controlled environment.
Quote
#7
can u give an example
Quote
#8
the link i provided is an example.
Mekire likes this post
*Describe the environment in which it occurs.                                    *Describe the symptoms of your problem clearly.
*Describe the research you did to try and understand the problem.      *Describe the goal, not the step. 
*Use meaningful, specific subject headers                                          *Write in clear, grammatical, correctly-spelled language
*Describe the problem's symptoms, not your guesses                         *Describe your problem's symptoms in chronological order
*Describe the diagnostic steps you took to try and pin down the problem yourself.
*Describe any possibly relevant recent changes in your computer or software configuration. 
*Provide a way to reproduce the problem in a controlled environment.
Quote
#9
Here a very simple example without cursor. Tinker around with both and make your own.
import pygame

class Textbox(object):
    def __init__(self, rect, font):
        self.text = ""
        self.font = font
        self.rect = rect
        self.image = None
        self.adjust = font.size("Iy") # use for centering
        self.height = rect.y + rect.h - (self.adjust[1] + rect.h) / 2

        self.box_image = pygame.Surface(rect.size)
        self.box_image.fill((100,100,150))

        self.text_color = (255,255,255)

        self.selected = False

    def keydown(self, event):
        if self.selected:
            if 32 <= event.key < 123:
                self.text += str(event.unicode)
            elif event.key == pygame.K_BACKSPACE:
                if len(self.text) > 1:
                    self.text = self.text[:-1]
                else:
                    self.text = ""
            elif event.key == pygame.K_DELETE:
                self.text = ""

            self.build()

    def mouse_button_down(self, event):
        if event.button == 1:
            if self.rect.collidepoint(event.pos):
                self.selected = True
                self.box_image.fill((100,150,100))
            else:
                self.selected = False
                self.box_image.fill((100,100,150))

    # have it fit inside box
    def build(self):
        length = len(self.text)
        if length == 0:
            self.image = None
        else:
            minus = self.adjust[0]
            for n in xrange(length):
                if self.font.size(self.text[n:])[0] < self.rect.w - minus:
                    self.image = self.font.render(self.text[n:], 1, self.text_color)
                    break

    def blit(self, surface):
        surface.blit(self.box_image, self.rect.topleft)
        if self.image:
            surface.blit(self.image, (self.rect.x + self.adjust[0] / 2, self.height))


def main():
    pygame.init()

    pygame.display.set_caption("Textbox Example")
    screen = pygame.display.set_mode((800, 600), 0, 0)
    clock = pygame.time.Clock()

    text = Textbox(pygame.Rect(10,10,150,35), pygame.font.SysFont("arial", 24))
    text2 = Textbox(pygame.Rect(10,60,150,35), pygame.font.Font(None, 30))

    running = True
    while(running):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                text.keydown(event)
                text2.keydown(event)
            elif event.type == pygame.MOUSEBUTTONDOWN:
                text.mouse_button_down(event)
                text2.mouse_button_down(event)

        screen.fill((25,100,150))

        text.blit(screen)
        text2.blit(screen)

        pygame.display.flip()
        clock.tick(60)

    pygame.quit()

main()
Quote
#10
Smile
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [PyGame] Copying the Screen (PyGame) Zman350x 4 326 Feb-11-2017, 08:26 PM
Last Post: Zman350x

Forum Jump:


Users browsing this thread: 1 Guest(s)