6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

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

Last updated at Posted at 2022-07-03

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

step.1:スクロールバー

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

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

step.2:メニューを作る

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

step.3:ファイルダイヤログ

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

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

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()

step.5:ファイルに書き込み

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()

step.6:背景色の変更、ウィンドウサイズ変更

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

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

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
スクリーンショット 2022-07-11 18.18.31.png

6
7
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
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?