python テキストエディタ作成 -tkinter-

<H1>テキストエディタ作成 -tkinter-</H1>
<H2>step.1:スクロールバー</H2>

```python
import tkinter

root = tkinter.Tk()
root.title("スクロールバー")
fr = tkinter.Frame()
fr.pack()
te = tkinter.Text(fr, width=80, height=30)
sc = tkinter.Scrollbar(fr, orient = tkinter.VERTICAL, command=te.yview)
sc.pack(side=tkinter.RIGHT, fill=tkinter.Y)
te.pack()
te[ "yscrollcommand" ] = sc.set

root.mainloop()
```
![スクリーンショット 2022-07-03 7.55.58.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1614609/65b87586-e9c4-00dd-f819-4e044ef0f14c.png)

:::note info
インフォメーション
改行を複数入れるとスクロールバーが右に出ます。
:::



<H2>step.2:メニューを作る</H2>

```python
import tkinter

def load_text():
    pass

def save_text():
    pass

root = tkinter.Tk()
root.title("スクロールバー")
fr = tkinter.Frame()
fr.pack()
te = tkinter.Text(fr, width=80, height=30)
sc = tkinter.Scrollbar(fr, orient = tkinter.VERTICAL, command=te.yview)
sc.pack(side=tkinter.RIGHT, fill=tkinter.Y)
te.pack()
te[ "yscrollcommand" ] = sc.set

mbar = tkinter.Menu()
mcom = tkinter.Menu(mbar, tearoff = 0)
mcom.add_command(label="読み込み", command=load_text)
mcom.add_separator()
mcom.add_command(label="書き込み", command=save_text)
mbar.add_cascade(label="ファイル", menu=mcom)
root["menu"] = mbar

root.mainloop()
```

![スクリーンショット 2022-07-04 11.21.14.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1614609/e98d9179-6ba6-5ca0-9e0e-2c17e7b63cfb.png)

<H2>step.3:ファイルダイヤログ</H2>

```python
import tkinter
import tkinter.filedialog

def load_text():
    typ = [("Text","*.txt"),("Python","*.py")] 
    fn = tkinter.filedialoaskopenfilename(filetypes=typ)

def save_text():
    pass

root = tkinter.Tk()
root.title("スクロールバー")
fr = tkinter.Frame()
fr.pack()
te = tkinter.Text(fr, width=80, height=30)
sc = tkinter.Scrollbar(fr, orient = tkinter.VERTICAL, command=te.yview)
sc.pack(side=tkinter.RIGHT, fill=tkinter.Y)
te.pack()
te[ "yscrollcommand" ] = sc.set

mbar = tkinter.Menu()
mcom = tkinter.Menu(mbar, tearoff = 0)
mcom.add_command(label="読み込み", command=load_text)
mcom.add_separator()
mcom.add_command(label="書き込み", command=save_text)
mbar.add_cascade(label="ファイル", menu=mcom)
root["menu"] = mbar

root.mainloop()
```

![スクリーンショット 2022-07-06 5.47.30.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1614609/ae4d7e7d-9474-89d9-de43-7782f743bea8.png)

<H2>step.4:ファイルの読み込みと文字エンコード</H2>

```python
import tkinter
import tkinter.filedialog

def load_text():
    typ = [("Text","*.txt"),("Python","*.py")] 
    fn = tkinter.filedialog.askopenfilename(filetypes=typ)
    if fn != "":
        f = None
        try:
            f = open(fn, 'r', encoding= "utf-8")
            te.delete("1.0", "end")
            te.insert("1.0", f.read())
        except:
            f = open(fn, 'r', encoding= "shift-jis")
            te.delete("1.0", "end")
            te.insert("1.0", f.read())
        finally:
            if f != None:
                f.close()
                

def save_text():
    pass

root = tkinter.Tk()
root.title("スクロールバー")
fr = tkinter.Frame()
fr.pack()
te = tkinter.Text(fr, width=80, height=30)
sc = tkinter.Scrollbar(fr, orient = tkinter.VERTICAL, command=te.yview)
sc.pack(side=tkinter.RIGHT, fill=tkinter.Y)
te.pack()
te[ "yscrollcommand" ] = sc.set

mbar = tkinter.Menu()
mcom = tkinter.Menu(mbar, tearoff = 0)
mcom.add_command(label="読み込み", command=load_text)
mcom.add_separator()
mcom.add_command(label="書き込み", command=save_text)
mbar.add_cascade(label="ファイル", menu=mcom)
root["menu"] = mbar

root.mainloop()
```
<H2>step.5:ファイルに書き込み</H2>

```python
import tkinter
import tkinter.filedialog

def load_text():
    typ = [("Text","*.txt"),("Python","*.py")] 
    fn = tkinter.filedialog.askopenfilename(filetypes=typ)
    if fn != "":
        f = None
        try:
            f = open(fn, 'r', encoding= "utf-8")
            te.delete("1.0", "end")
            te.insert("1.0", f.read())
        except:
            f = open(fn, 'r', encoding= "shift-jis")
            te.delete("1.0", "end")
            te.insert("1.0", f.read())
        finally:
            if f != None:
                f.close()
                

def save_text():
    typ = [("Text", "*.txt")]
    fn = tkinter.filedialog.asksaveasfilename(filetypes=typ)
    if fn != "":
        if fn[-4:] != ".txt":
            fn = fn + ".txt"
        with open(fn, 'w', encoding="utf-8") as f:
            f.write(te.get("1.0","end-1c"))

root = tkinter.Tk()
root.title("スクロールバー")
fr = tkinter.Frame()
fr.pack()
te = tkinter.Text(fr, width=80, height=30)
sc = tkinter.Scrollbar(fr, orient = tkinter.VERTICAL, command=te.yview)
sc.pack(side=tkinter.RIGHT, fill=tkinter.Y)
te.pack()
te[ "yscrollcommand" ] = sc.set

mbar = tkinter.Menu()
mcom = tkinter.Menu(mbar, tearoff = 0)
mcom.add_command(label="読み込み", command=load_text)
mcom.add_separator()
mcom.add_command(label="書き込み", command=save_text)
mbar.add_cascade(label="ファイル", menu=mcom)
root["menu"] = mbar

root.mainloop()

```
<H2>step.6:背景色の変更、ウィンドウサイズ変更</H2>

```python
import tkinter
import tkinter.filedialog

def load_text():
    typ = [("Text","*.txt"),("Python","*.py")] 
    fn = tkinter.filedialog.askopenfilename(filetypes=typ)
    if fn != "":
        f = None
        try:
            f = open(fn, 'r', encoding= "utf-8")
            te.delete("1.0", "end")
            te.insert("1.0", f.read())
        except:
            f = open(fn, 'r', encoding= "shift-jis")
            te.delete("1.0", "end")
            te.insert("1.0", f.read())
        finally:
            if f != None:
                f.close()
                

def save_text():
    typ = [("Text", "*.txt")]
    fn = tkinter.filedialog.asksaveasfilename(filetypes=typ)
    if fn != "":
        if fn[-4:] != ".txt":
            fn = fn + ".txt"
        with open(fn, 'w', encoding="utf-8") as f:
            f.write(te.get("1.0","end-1c"))

def col_black():
    te.configure(bg="black", fg="white", insertbackground="white")

def col_white():
    te.configure(bg="white", fg="black", insertbackground="black")

root = tkinter.Tk()
root.title("スクロールバー")
fr = tkinter.Frame()
fr.pack(expand=True, fill=tkinter.BOTH)
te = tkinter.Text(fr, width=80, height=30)
sc = tkinter.Scrollbar(fr, orient = tkinter.VERTICAL, command=te.yview)
sc.pack(side=tkinter.RIGHT, fill=tkinter.Y)
te.pack(expand=True, fill=tkinter.BOTH)
te[ "yscrollcommand" ] = sc.set

mbar = tkinter.Menu()
mcom = tkinter.Menu(mbar, tearoff = 0)
mcom.add_command(label="読み込み", command=load_text)
mcom.add_separator()
mcom.add_command(label="書き込み", command=save_text)
mbar.add_cascade(label="ファイル", menu=mcom)
mcom2 = tkinter.Menu(mbar, tearoff = 0)
mcom2.add_command(label="黒", command=col_black)
mcom2.add_command(label="白", command=col_white)
mbar.add_cascade(label="背景色", menu=mcom2)

root["menu"] = mbar

root.mainloop()
```

![スクリーンショット 2022-07-08 11.20.57.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1614609/fd7814ca-f293-9132-fb07-cc931c346a1c.png)

<H2>step.7:自動処理 半角カタカナから全角カタカナ</H2>

```python
import tkinter
import tkinter.filedialog

def load_text():
    typ = [("Text","*.txt"),("Python","*.py")] 
    fn = tkinter.filedialog.askopenfilename(filetypes=typ)
    if fn != "":
        f = None
        try:
            f = open(fn, 'r', encoding= "utf-8")
            te.delete("1.0", "end")
            te.insert("1.0", f.read())
        except:
            f = open(fn, 'r', encoding= "shift-jis")
            te.delete("1.0", "end")
            te.insert("1.0", f.read())
        finally:
            if f != None:
                f.close()
                

def save_text():
    typ = [("Text", "*.txt")]
    fn = tkinter.filedialog.asksaveasfilename(filetypes=typ)
    if fn != "":
        if fn[-4:] != ".txt":
            fn = fn + ".txt"
        with open(fn, 'w', encoding="utf-8") as f:
            f.write(te.get("1.0","end-1c"))

def col_black():
    te.configure(bg="black", fg="white", insertbackground="white")

def col_white():
    te.configure(bg="white", fg="black", insertbackground="black")

HANKAKU = [
    "ｶﾞ", "ｷﾞ", "ｸﾞ", "ｹﾞ", "ｺﾞ",
    "ｻﾞ", "ｼﾞ", "ｽﾞ", "ｾﾞ", "ｿﾞ",
    "ﾀﾞ", "ﾁﾞ", "ﾂﾞ", "ﾃﾞ", "ﾄﾞ",
    "ﾊﾞ", "ﾋﾞ", "ﾌﾞ", "ﾍﾞ", "ﾎﾞ",
    "ﾊﾟ", "ﾋﾟ", "ﾌﾟ", "ﾍﾟ", "ﾎﾟ",
    "ｱ", "ｲ", "ｳ", "ｴ", "ｵ",
    "ｶ", "ｷ", "ｸ", "ｹ", "ｺ",
    "ｻ", "ｼ", "ｽ", "ｾ", "ｿ",
    "ﾀ", "ﾁ", "ﾂ", "ﾃ", "ﾄ",
    "ﾅ", "ﾆ", "ﾇ", "ﾈ", "ﾉ",
    "ﾊ", "ﾋ", "ﾌ", "ﾍ", "ﾎ",
    "ﾏ", "ﾐ", "ﾑ", "ﾒ", "ﾓ",
    "ﾔ", "ﾕ", "ﾖ",
    "ﾗ", "ﾘ", "ﾙ", "ﾚ", "ﾛ",
    "ﾜ", "ｦ", "ﾝ",
    "ｧ", "ｨ", "ｩ", "ｪ", "ｫ",
    "ｬ", "ｭ", "ｮ", "ｯ",
    "｡", "､", "ｰ", "｢", "｣"
    ]

ZENKAKU = [
    "ガ", "ギ", "グ", "ゲ", "ゴ",
    "ザ", "ジ", "ズ", "ゼ", "ゾ",
    "ダ", "ヂ", "ヅ", "デ", "ド",
    "バ", "ビ", "ブ", "ベ", "ボ",
    "パ", "ピ", "プ", "ペ", "ポ",
    "ア", "イ", "ウ", "エ", "オ",
    "カ", "キ", "ク", "ケ", "コ",
    "サ", "シ", "ス", "セ", "ソ",
    "タ", "チ", "ツ", "テ", "ト",
    "ナ", "ニ", "ヌ", "ネ", "ノ",
    "ハ", "ヒ", "フ", "ヘ", "ホ",
    "マ", "ミ", "ム", "メ", "モ",
    "ヤ", "ユ", "ヨ",
    "ラ", "リ", "ル", "レ", "ロ",
    "ワ", "ヲ", "ン",
    "ァ", "ィ", "ゥ", "ェ", "ォ",
    "ャ", "ュ", "ョ", "ッ",
    "。", "、", "ー", "「", "」"
    ]

def auto_proc():
    txt = te.get("1.0", "end-1c")
    for i in range(len(HANKAKU)):
        txt = txt.replace(HANKAKU[i], ZENKAKU[i])
    te.delete("1.0", "end")
    te.insert("1.0", txt)

root = tkinter.Tk()
root.title("スクロールバー")
fr = tkinter.Frame()
fr.pack(expand=True, fill=tkinter.BOTH)
te = tkinter.Text(fr, width=80, height=30)
sc = tkinter.Scrollbar(fr, orient = tkinter.VERTICAL, command=te.yview)
sc.pack(side=tkinter.RIGHT, fill=tkinter.Y)
te.pack(expand=True, fill=tkinter.BOTH)
te[ "yscrollcommand" ] = sc.set

mbar = tkinter.Menu()
mcom = tkinter.Menu(mbar, tearoff = 0)
mcom.add_command(label="読み込み", command=load_text)
mcom.add_separator()
mcom.add_command(label="書き込み", command=save_text)
mbar.add_cascade(label="ファイル", menu=mcom)
mcom2 = tkinter.Menu(mbar, tearoff = 0)
mcom2.add_command(label="黒", command=col_black)
mcom2.add_command(label="白", command=col_white)
mbar.add_cascade(label="背景色", menu=mcom2)
mcom3 = tkinter.Menu(mbar, tearoff = 0)
mcom3.add_command(label="半角ｶﾀｶﾅ→全角カタカナ", command=auto_proc)
mbar.add_cascade(label="自動処理", menu=mcom3)


root["menu"] = mbar

root.mainloop()

```
![スクリーンショット 2022-07-11 18.18.18.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1614609/a9d640dc-80e9-8354-ed74-be3589f1ba64.png)
![スクリーンショット 2022-07-11 18.18.31.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1614609/dea0c270-90a2-0fa7-24f6-39d7593cab62.png)