Posts: 63
Threads: 28
Joined: Jul 2020
I'm trying to make a pygame window where I can enter and edit text. I'm following a tutorial as a base for my code but I'm getting an the following error:
Error: File "c:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\print.py", line 39, in Print
text += event.unicode
AttributeError: 'Event' object has no attribute 'unicode'
this is my code:
import pygame
from pygame.locals import *
import time
class Print:
"""Draw text to the screen."""
# text is editable with the keyboard
# options for font and background colour
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
GRAY = (200, 200, 200)
pygame.init()
screen = pygame.display.set_mode((640, 240))
# creates a screen 640 x 240 pixels in size
sysfont = pygame.font.get_default_font()
print('system font :', sysfont)
text = 'This text is editable'
font = pygame.font.SysFont(None, 24)
img = font.render(text, True, RED)
rect = img.get_rect()
running = True
background = GRAY
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == KEYDOWN:
if event.key == K_BACKSPACE:
if len(text)>0: # if length of text is > 0
text = text[:-1] # delete the last letter in the string
else:
text += event.unicode
# else add the letter to the end of the string
img = font.render(text, True, RED)
rect.size=img.get_size()
cursor.topleft = rect.topright
screen.fill(background)
screen.blit(img, (20, 20))
pygame.display.update()
pygame.quit()
Posts: 12,051
Threads: 488
Joined: Sep 2016
Dec-14-2020, 10:04 PM
(This post was last modified: Dec-14-2020, 10:05 PM by Larz60+.)
this may or may not be the problem, but event is a python class name, and you may be getting the error because you are overriding it. Bad practice at any rate.
Try using another name, like game_event or something similar
Posts: 63
Threads: 28
Joined: Jul 2020
My code is now:
import pygame
from pygame.locals import *
import time
class Print:
"""Draw text to the screen."""
# text is editable with the keyboard
# options for font and background colour
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
GRAY = (200, 200, 200)
pygame.init()
screen = pygame.display.set_mode((640, 240))
# creates a screen 640 x 240 pixels in size
sysfont = pygame.font.get_default_font()
print('system font :', sysfont)
text = 'This text is editable'
font = pygame.font.SysFont(None, 24)
img = font.render(text, True, RED)
rect = img.get_rect()
running = True
background = GRAY
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == KEYDOWN:
if event.key == K_BACKSPACE:
if len(text)>0: # if length of text is > 0
text = text[:-1] # delete the last letter in the string
else:
text += event.unicode
# else add the letter to the end of the string
img = font.render(text, True, RED)
rect.size=img.get_size()
cursor.topleft = rect.topright
screen.fill(background)
screen.blit(img, (20, 20))
pygame.display.update()
pygame.quit() and I'm still getting this error:
Error: File "c:\Users\djwil\Documents\python\python crash course\Projects\Alien invasion\print.py", line 39, in Print
text += game_event.unicode
AttributeError: 'Event' object has no attribute 'unicode'
PS C:\Users\djwil\Documents\python\learning python>
Posts: 12,051
Threads: 488
Joined: Sep 2016
you didn't change the name!
Posts: 63
Threads: 28
Joined: Jul 2020
Dec-15-2020, 04:42 PM
(This post was last modified: Dec-15-2020, 04:43 PM by djwilson0495.)
Sorry I copied my previous code over. The code is currently:
import pygame
from pygame.locals import *
import time
class Print:
"""Draw text to the screen."""
# text is editable with the keyboard
# options for font and background colour
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
GRAY = (200, 200, 200)
pygame.init()
screen = pygame.display.set_mode((640, 240))
# creates a screen 640 x 240 pixels in size
sysfont = pygame.font.get_default_font()
print('system font :', sysfont)
text = 'This text is editable'
font = pygame.font.SysFont(None, 24)
img = font.render(text, True, RED)
rect = img.get_rect()
running = True
background = GRAY
while running:
for game_event in pygame.event.get():
if game_event.type == QUIT:
running = False
if game_event.type == KEYDOWN:
if game_event.key == K_BACKSPACE:
if len(text)>0: # if length of text is > 0
text = text[:-1] # delete the last letter in the string
else:
text += game_event.unicode
# else add the letter to the end of the string
img = font.render(text, True, RED)
rect.size=img.get_size()
cursor.topleft = rect.topright
screen.fill(background)
screen.blit(img, (20, 20))
pygame.display.update()
pygame.quit() This matches up with the error in my previous post
Posts: 5,151
Threads: 396
Joined: Sep 2016
Dec-20-2020, 12:20 PM
(This post was last modified: Dec-20-2020, 12:20 PM by metulburr.)
You need to make sure you are indenting correctly. As this is the entire issue.
from this:
while running:
for game_event in pygame.event.get():
if game_event.type == QUIT:
running = False
if game_event.type == KEYDOWN:
if game_event.key == K_BACKSPACE:
if len(text)>0: # if length of text is > 0
text = text[:-1] # delete the last letter in the string
else:
text += game_event.unicode
# else add the letter to the end of the string
img = font.render(text, True, RED)
rect.size=img.get_size()
cursor.topleft = rect.topright
screen.fill(background)
screen.blit(img, (20, 20))
pygame.display.update() to this:
while running:
for game_event in pygame.event.get():
if game_event.type == QUIT:
running = False
if game_event.type == KEYDOWN:
if game_event.key == K_BACKSPACE:
if len(text)>0: # if length of text is > 0
text = text[:-1] # delete the last letter in the string
else:
text += game_event.unicode
# else add the letter to the end of the string
img = font.render(text, True, RED)
rect.size=img.get_size()
cursor.topleft = rect.topright
screen.fill(background)
screen.blit(img, (20, 20))
pygame.display.update() Here is an example of a working textbox for comparison:
Recommended Tutorials:
Posts: 44
Threads: 4
Joined: Nov 2020
I think the indentation should be like this
while running:
for game_event in pygame.event.get():
if game_event.type == QUIT:
running = False
if game_event.type == KEYDOWN:
if game_event.key == K_BACKSPACE:
if len(text)>0: # if length of text is > 0
text = text[:-1] # delete the last letter in the string
else:
text += game_event.unicode
# else add the letter to the end of the string
img = font.render(text, True, RED)
rect.size=img.get_size()
cursor.topleft = rect.topright
screen.fill(background)
screen.blit(img, (20, 20))
pygame.display.update()
Posts: 12,051
Threads: 488
Joined: Sep 2016
MK_CodingSpace,
Your post is verbatim of what Metulburr posted 2 hours prior to your post.
Please read all posts prior to submitting a new one.
Thank You
Posts: 44
Threads: 4
Joined: Nov 2020
Larz60+,
If you look more closely, you will find it's different
(Dec-20-2020, 08:58 PM)Larz60+ Wrote: MK_CodingSpace,
Your post is verbatim of what Metulburr posted 2 hours prior to your post.
Please read all posts prior to submitting a new one.
Thank You
Posts: 12,051
Threads: 488
Joined: Sep 2016
|