在 pygame 中显示文本框
displaying text box in pygame
我正在尝试为 pygame 中的一个 windows 创建一个输入框。我已经为它创建了 class 但是当我试图显示输入框时它给了我一个错误。
下面是输入框的代码:
class inp_box:
def __init__(self, xpos, ypos, width, height, text = ""):
self.rect = pygame.Rect(xpos, ypos, width, height)
self.colour = [200, 200, 200]
self.text = text
self.text_surf = FONT.render(text , True, self.colour)
self.current = False
self.colourAc = False
def enter_text(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if self.rect.collidepoint(event.pos):
self.colourAc = not self.colourAc
else:
self.colourAc = False
if self.colourAc:
self.color = [150, 150, 150]
else:
self.color = [200, 200, 200]
if event.type == pygame.K_KEYDOWN:
if self.colourAc:
if event.key == pygame.K_RETURN:
print(self.text)
self.text = ""
elif event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
self.text_surf = FONT.render(self.text, True, self.colour)
def Updtext(self):
widt = max(200, self.text_surf.get_width() + 10)
self.rect.width = widt
def showBox(self, display):
display.blit(self.text_surf, (self.rect.xpos + 5, self.rect.ypos + 5))
pygame.draw.rect(display, self.colour, self.rect)
这是显示输入框的主循环的一部分:
elif set_wind.checkUp():
main_wind_but = return_but.hover(mouse_pos, mouse_click)
return_but.showBut(set_wind.reTitle())
enter_wind_but = enter_but.hover(mouse_pos, mouse_click)
enter_but.showBut(set_wind.reTitle())
title1_box.showBut(set_wind.reTitle())
desc1_box.showBut(set_wind.reTitle())
input_box.showBox(set_wind)
if main_wind_but:
Main = main_wind.setCurrent()
set_wind.endCurrent()
这是我得到的错误:
Traceback (most recent call last):
File "main.py", line 282, in <module>
input_box.showBox(set_wind)
File "main.py", line 128, in showBox
display.blit(self.text_surf, (self.rect.xpos + 5, self.rect.ypos + 5))
AttributeError: 'window' object has no attribute 'blit'
我试图找到解决此问题的方法,但 none 似乎解决了我的问题。
感谢您的帮助,并提前致谢。
我在网上找到的
https://www.codegrepper.com/code-examples/python/how+to+make+a+text+input+box+python+pygame
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
COLOR_INACTIVE = pg.Color('lightskyblue3')
COLOR_ACTIVE = pg.Color('dodgerblue2')
FONT = pg.font.Font(None, 32)
class InputBox:
def __init__(self, x, y, w, h, text=''):
self.rect = pg.Rect(x, y, w, h)
self.color = COLOR_INACTIVE
self.text = text
self.txt_surface = FONT.render(text, True, self.color)
self.active = False
def handle_event(self, event):
if event.type == pg.MOUSEBUTTONDOWN:
# If the user clicked on the input_box rect.
if self.rect.collidepoint(event.pos):
# Toggle the active variable.
self.active = not self.active
else:
self.active = False
# Change the current color of the input box.
self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
if event.type == pg.KEYDOWN:
if self.active:
if event.key == pg.K_RETURN:
print(self.text)
self.text = ''
elif event.key == pg.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
# Re-render the text.
self.txt_surface = FONT.render(self.text, True, self.color)
def update(self):
# Resize the box if the text is too long.
width = max(200, self.txt_surface.get_width()+10)
self.rect.w = width
def draw(self, screen):
# Blit the text.
screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5))
# Blit the rect.
pg.draw.rect(screen, self.color, self.rect, 2)
def main():
clock = pg.time.Clock()
input_box1 = InputBox(100, 100, 140, 32)
input_box2 = InputBox(100, 300, 140, 32)
input_boxes = [input_box1, input_box2]
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
for box in input_boxes:
box.handle_event(event)
for box in input_boxes:
box.update()
screen.fill((30, 30, 30))
for box in input_boxes:
box.draw(screen)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
pg.quit()
希望对您有所帮助!
我正在尝试为 pygame 中的一个 windows 创建一个输入框。我已经为它创建了 class 但是当我试图显示输入框时它给了我一个错误。 下面是输入框的代码:
class inp_box:
def __init__(self, xpos, ypos, width, height, text = ""):
self.rect = pygame.Rect(xpos, ypos, width, height)
self.colour = [200, 200, 200]
self.text = text
self.text_surf = FONT.render(text , True, self.colour)
self.current = False
self.colourAc = False
def enter_text(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if self.rect.collidepoint(event.pos):
self.colourAc = not self.colourAc
else:
self.colourAc = False
if self.colourAc:
self.color = [150, 150, 150]
else:
self.color = [200, 200, 200]
if event.type == pygame.K_KEYDOWN:
if self.colourAc:
if event.key == pygame.K_RETURN:
print(self.text)
self.text = ""
elif event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
self.text_surf = FONT.render(self.text, True, self.colour)
def Updtext(self):
widt = max(200, self.text_surf.get_width() + 10)
self.rect.width = widt
def showBox(self, display):
display.blit(self.text_surf, (self.rect.xpos + 5, self.rect.ypos + 5))
pygame.draw.rect(display, self.colour, self.rect)
这是显示输入框的主循环的一部分:
elif set_wind.checkUp():
main_wind_but = return_but.hover(mouse_pos, mouse_click)
return_but.showBut(set_wind.reTitle())
enter_wind_but = enter_but.hover(mouse_pos, mouse_click)
enter_but.showBut(set_wind.reTitle())
title1_box.showBut(set_wind.reTitle())
desc1_box.showBut(set_wind.reTitle())
input_box.showBox(set_wind)
if main_wind_but:
Main = main_wind.setCurrent()
set_wind.endCurrent()
这是我得到的错误:
Traceback (most recent call last):
File "main.py", line 282, in <module>
input_box.showBox(set_wind)
File "main.py", line 128, in showBox
display.blit(self.text_surf, (self.rect.xpos + 5, self.rect.ypos + 5))
AttributeError: 'window' object has no attribute 'blit'
我试图找到解决此问题的方法,但 none 似乎解决了我的问题。 感谢您的帮助,并提前致谢。
我在网上找到的 https://www.codegrepper.com/code-examples/python/how+to+make+a+text+input+box+python+pygame
import pygame as pg
pg.init()
screen = pg.display.set_mode((640, 480))
COLOR_INACTIVE = pg.Color('lightskyblue3')
COLOR_ACTIVE = pg.Color('dodgerblue2')
FONT = pg.font.Font(None, 32)
class InputBox:
def __init__(self, x, y, w, h, text=''):
self.rect = pg.Rect(x, y, w, h)
self.color = COLOR_INACTIVE
self.text = text
self.txt_surface = FONT.render(text, True, self.color)
self.active = False
def handle_event(self, event):
if event.type == pg.MOUSEBUTTONDOWN:
# If the user clicked on the input_box rect.
if self.rect.collidepoint(event.pos):
# Toggle the active variable.
self.active = not self.active
else:
self.active = False
# Change the current color of the input box.
self.color = COLOR_ACTIVE if self.active else COLOR_INACTIVE
if event.type == pg.KEYDOWN:
if self.active:
if event.key == pg.K_RETURN:
print(self.text)
self.text = ''
elif event.key == pg.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
# Re-render the text.
self.txt_surface = FONT.render(self.text, True, self.color)
def update(self):
# Resize the box if the text is too long.
width = max(200, self.txt_surface.get_width()+10)
self.rect.w = width
def draw(self, screen):
# Blit the text.
screen.blit(self.txt_surface, (self.rect.x+5, self.rect.y+5))
# Blit the rect.
pg.draw.rect(screen, self.color, self.rect, 2)
def main():
clock = pg.time.Clock()
input_box1 = InputBox(100, 100, 140, 32)
input_box2 = InputBox(100, 300, 140, 32)
input_boxes = [input_box1, input_box2]
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
for box in input_boxes:
box.handle_event(event)
for box in input_boxes:
box.update()
screen.fill((30, 30, 30))
for box in input_boxes:
box.draw(screen)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
pg.quit()
希望对您有所帮助!