テキストを保存して好きなものをコピーできるAPP

AIに補助してもらいながら作るとあっという間にできますな

![スクリーンショット 2025-01-30 2.16.51.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/540879/0f1c4f90-3ab4-cea0-04a3-b6b46ad430d5.png)
テキストを保存して好きなものをコピーできるAPP


```diff_python
import sys
import json
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QGridLayout, QLabel, QLineEdit, QPushButton, QListWidget, QListWidgetItem
import subprocess

class TextStorageApp(QWidget):
    def __init__(self):
        super().__init__()

        self.texts = {}

        self.initUI()

        self.load_texts()
        self.update_list_widget()

    def initUI(self):
        self.setWindowTitle("Text Storage App")
        self.resize(200, 400)  # ウィンドウの幅と高さを設定

        layout = QVBoxLayout()#縦に配置

        # self.label = QLabel("Enter text:")
        # layout.addWidget(self.label)

        self.entry = QLineEdit()
        # layout.addWidget(self.entry)

        self.key_label = QLabel("Enter key:")
        layout.addWidget(self.key_label)

        self.key_entry = QLineEdit()
        layout.addWidget(self.key_entry)

        self.add_button = QPushButton("Add Text")
        self.add_button.clicked.connect(self.add_text)
        layout.addWidget(self.add_button)

        self.list_widget = QListWidget()
        layout.addWidget(self.list_widget)

        self.copy_button = QPushButton("Copy")
        self.copy_button.clicked.connect(self.copy_text)

        self.delete_button = QPushButton("Delete")
        self.delete_button.clicked.connect(self.delete_text)

        self.save_button = QPushButton("Save")
        self.save_button.clicked.connect(self.save_texts)

        self.load_button = QPushButton("Load")
        self.load_button.clicked.connect(self.load_texts)

        button_layout = QGridLayout()#grid状に配置
        button_layout.addWidget(self.copy_button,0,0)
        button_layout.addWidget(self.delete_button,0,1)
        button_layout.addWidget(self.save_button,1,0)
        button_layout.addWidget(self.load_button,1,1)
        layout.addLayout(button_layout)

        self.setLayout(layout)
        self.update_list_widget()

    def add_text(self):
        key = self.key_entry.text()
        text = self.entry.text()
        self.texts[key] = text
        self.list_widget.addItem(QListWidgetItem(key))
        print(f"Text '{key}' added.")

    def copy_text(self):
        selected_items = self.list_widget.selectedItems()
        if selected_items:
            key = selected_items[0].text()
            if key in self.texts:
                subprocess.run("clip", input=key, text=True)
                print(f"Text '{key}' copied to clipboard.")
            else:
                print(f"Text '{key}' not found.")
        else:
            print("No text selected.")

    def delete_text(self):
        selected_items = self.list_widget.selectedItems()
        if selected_items:
            key = selected_items[0].text()
            if key in self.texts:
                del self.texts[key]
                self.list_widget.takeItem(self.list_widget.row(selected_items[0]))
                print(f"Text '{key}' deleted.")
            else:
                print(f"Text '{key}' not found.")
        else:
            print("No text selected.")

    def save_texts(self):
        with open("texts.json", "w") as file:
            json.dump(self.texts, file)
        print("Texts saved.")

    def load_texts(self):
        try:
            with open("texts.json", "r") as file:
                self.texts = json.load(file)
        except FileNotFoundError:
            self.texts = {}

        self.update_list_widget()

    def update_list_widget(self):
        self.list_widget.clear()
        for key in self.texts:
            self.list_widget.addItem(QListWidgetItem(key))

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = TextStorageApp()
    window.show()
    sys.exit(app.exec())
```