2
4

More than 1 year has passed since last update.

pythonでメモ帳を作ってみた。

Last updated at Posted at 2023-01-21

完成系

仮想環境を作成(Windows)

python -m venv memo
.\memo\Scripts\activate

ライブラリインストール

今回利用するtkinterは標準ライブラリのため、今回特にインストールする必要はありません。

アプリ作成

1.枠組み作成

まずは以下のような中身のないアプリを作成します。

import tkinter as tk

root = tk.Tk()
root.mainloop()

2.文字入力設定

文字を入力できるようにします。

お試し入力

import tkinter as tk

root = tk.Tk()
root.title("メモ帳")

text_widget = tk.Text(root)
text_widget.pack()
root.mainloop()

3.中身のないボタンを設置

import tkinter as tk

root = tk.Tk()
root.title("メモ帳")

text_widget = tk.Text(root)
text_widget.pack()

save_button = tk.Button(root, text="名前を付けて保存", command='')
save_button.pack(side=tk.LEFT)
open_button = tk.Button(root, text="開く", command='')
open_button.pack(side=tk.LEFT)

root.mainloop()

4.完成

import tkinter as tk
from tkinter import filedialog

def save_file():
    file = filedialog.asksaveasfile(mode='w', defaultextension=".txt")
    if file:
        text_content = text_widget.get("1.0", tk.END)
        file.write(text_content)
        file.close()

def open_file():
    file = filedialog.askopenfile(mode='r', defaultextension=".txt")
    if file:
        text_widget.delete("1.0", tk.END)
        text_widget.insert("1.0", file.read())
        file.close()

root = tk.Tk()
root.title("メモ帳")

text_widget = tk.Text(root)
text_widget.pack()

save_button = tk.Button(root, text="名前を付けて保存", command=save_file)
save_button.pack(side=tk.LEFT)
open_button = tk.Button(root, text="開く", command=open_file)
open_button.pack(side=tk.LEFT)

root.mainloop()

exe化

pyinstaller memo.py --hidden-import=tk-inter --onefile --noconsole

image.png

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4