将文本保存为类实例 pygame/python 上的变量 [英] Save text as variable on class's instances pygame/ python

查看:68
本文介绍了将文本保存为类实例 pygame/python 上的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一个待办事项清单.我可以输入文本,并且文本会在下次打开应用程序时保留在那里,或者直到我更改它为止.我想我应该将文本保存在 .txt 文件中,但有 16 个实例/输入框.如果我将文本保存在 txt 文件中,我如何按顺序调用文本(例如 input_box1 = 第 1 行,input_box2 = 第 2 行).或者如果我为每个实例创建一个 txt 文件,我如何将其放入我的类中.

I am trying to do a to-do list. That I can input text and the text stay there for the next time I open the app or until I change it. I figure that I should save the text in a .txt file but there are 16 instances/ input boxes. If I save the text in a txt file, how can I recall the text in order( like input_box1 = line 1, input_box2 = line 2). Or if I create a txt file for every instance, how can I put it in my class.

我希望在我点击回车时保存文本,并且在我打开应用程序时可以被 blitted.

I want the text to be saved when I click enter and can be blitted when I open the application.

代码如下:

import pygame
import datetime
import time

pygame.init()
clock = pygame.time.Clock()
pygame.font.init()

# Font
text_font = pygame.font.Font('dogicapixelbold.ttf', 16)
color = (233, 248, 215)
active = False
# screen resolution
Width = 800
Height = 600
screen = pygame.display.set_mode((Width, Height))

class InputBox:

    def __init__(self, x, y, w, h, text=''):
        self.rect = pygame.Rect(x, y, w, h)
        self.color = color
        self.text = text
        self.txt_surface = text_font.render(text, True, self.color)
        self.active = False
        self.txt_rect = self.txt_surface.get_rect()
        self.cursor = pygame.Rect(self.txt_rect.topright, (3, self.txt_rect.height + 5))
    def handle_event(self, event):
        if event.type == pygame.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
        if event.type == pygame.KEYDOWN:
            if self.active:
                if event.key == pygame.K_RETURN:
                    print(self.text)
                    self.text = ''
                    self.active = False

                elif event.key == pygame.K_BACKSPACE:
                    self.text = self.text[:-1]
                else:
                    self.text += event.unicode
                    # Cursor
                    self.txt_rect.size = self.txt_surface.get_size()
                    self.cursor.topleft = self.txt_rect.topright
                    # Limit characters           -20 for border width
                    if self.txt_surface.get_width() > self.rect.w - 15:
                        self.text = self.text[:-1]

    def draw(self, screen):
        # Blit the text.
        screen.blit(self.txt_surface, (self.rect.x + 5, self.rect.y + 10))
        # Blit the rect.
        pygame.draw.rect(screen, self.color, self.rect, 1)
        # Blit the  cursor
        if self.active:
            if time.time() % 1 > 0.5:
                # bounding rectangle of the text
                text_rect = self.txt_surface.get_rect(topleft=(self.rect.x + 5, self.rect.y + 10))
                # set cursor position
                self.cursor.midleft = text_rect.midright
                pygame.draw.rect(screen, self.color, self.cursor)

    def update(self):
        # Re-render the text.
        self.txt_surface = text_font.render(self.text, True, self.color)

def main():
    clock = pygame.time.Clock()
    input_box1 = InputBox(115, 170, 250, 36)
    input_box2 = InputBox(115, 224, 250, 36)
    input_box3 = InputBox(115, 278, 250, 36)
    input_box4 = InputBox(115, 333, 250, 36)
    input_box5 = InputBox(115, 386, 250, 36)
    input_box6 = InputBox(115, 440, 250, 36)
    input_box7 = InputBox(115, 494, 250, 36)
    input_box8 = InputBox(440, 170, 250, 36)
    input_box9 = InputBox(440, 224, 250, 36)
    input_box10 = InputBox(440, 278, 250, 36)
    input_box11 = InputBox(440, 333, 250, 36)
    input_box12 = InputBox(440, 386, 250, 36)
    input_box13 = InputBox(440, 440, 250, 36)
    input_box14 = InputBox(440, 494, 250, 36)
    input_box15 = InputBox(440, 115, 250, 36)
    input_box16 = InputBox(440, 61, 250, 36)
    input_boxes = [input_box1, input_box2, input_box3, input_box4, input_box5, input_box6, input_box7, input_box8,
                   input_box9, input_box10, input_box11, input_box12, input_box13, input_box14,
                   input_box15, input_box16]
    done = False

    while not done:
        # Background
        screen.fill((0, 0, 0))
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            for box in input_boxes:
                box.handle_event(event)

        for box in input_boxes:
            box.update()

        for box in input_boxes:
            box.draw(screen)
        pygame.display.update()
        clock.tick(120)


if __name__ == '__main__':
    main()
    pygame.quit()

推荐答案

这是个人喜好,但一个文件听起来比 16 个文件更易于维护.
我认为您正在寻找这样的东西来保存文件:

It's a personal preference but one file sounds more maintainable than 16 files.
I think you are looking for something like this to save the file:

with open("savefile.txt", "w") as savefile:
    for i in input_boxes:
        savefile.write(i.text + "\r\n")

并读取文件:

for line in open("savefile.txt", "r").readlines():
    #add text to input box

这篇关于将文本保存为类实例 pygame/python 上的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆