본문 바로가기
Python

텍스트 수정 파일 제작기 - 04

by Ao1 2022. 7. 11.

from tkinter import *

tk = Tk()
tk.title('텍스트 파일 수정 도구 v0.04')


file_path = 'C:\\Users\\SungJin Jo\\Desktop\\test.txt'

tk.geometry('1000x400')
tk.resizable(0, 0)


def check():
    global num_check
    num_check = entry_num.get()
    if(ord(num_check) >= 48 and ord(num_check) <= 57):
        return num(num_check)
    
def num(x):
    global var2
    global var4
    var1 = [0]*int(x)
    var2 = [0]*int(x)
    var3 = [0]*int(x)
    var4 = [0]*int(x)
    for i in range(0,int(x)):
        var1[i] = Label(tk,text='변경할 열'+str(1+i)).place(x=90,y=70+(i*30))
        var2[i] = Entry(tk,width=3)
        var2[i].place(x=160,y=70+(i*30))
        var3[i] = Label(tk,text='내용').place(x=200,y=70+(i*30))
        var4[i] = Entry(tk,width=100)
        var4[i].place(x=240,y=70+(i*30))

def commit():
    n = 0
    var5 = [0] * (int(num_check) - 2)
    var6 = [0] * (int(num_check) - 2)
    
    new_text_content = ''
    new_string = ''
    item = []
    
    for j in range(0,int(num_check)):
        var5.insert(j,var2[j].get())
        var6.insert(j,var4[j].get())

    with open(file_path, 'r') as f:
        lines = f.readlines()
        for k, l in enumerate(lines):
            item.append(k)
        for m in range(0,len(item)):
            if item[m+1:m+2] == list(map(int,var5[n:n+1])):
                new_string = var6[n]
                n = n+1
                
            else:
                new_string = l.strip()

            if new_string:
                new_text_content += new_string + '\n'
            else:
                new_text_content += '\n'
                
    with open(file_path, 'w') as f:
        f.write(new_text_content)    


def reset():
    try:
        global tk
        tk.destroy()
    finally:
        tk = Tk()
        tk.title('텍스트 파일 수정 도구 v0.04')
        def check():
            num_check = entry_num.get()
            if(ord(num_check) >= 48 and ord(num_check) <= 57):
                return num(num_check)
            else:
                print("Not Number")

        def num(x):
            var1 = [0]*int(x)
            var2 = [0]*int(x)
            for i in range(0,int(x)):
                var1[i] = Label(tk,text='변경할 열'+str(1+i)).place(x=90,y=70+(i*30))
                var1[i] = Entry(tk,width=3)
                var1[i].place(x=160,y=70+(i*30))
                var2[i] = Label(tk,text='내용').place(x=200,y=70+(i*30))
                var2[i] = Entry(tk,width=100)
                var2[i].place(x=240,y=70+(i*30))

        def commit():
            n = 0
            var5 = [0] * (int(num_check) - 2)
            var6 = [0] * (int(num_check) - 2)
    
            new_text_content = ''
            new_string = ''
            item = []
    
            for j in range(0,int(num_check)):
                var5.insert(j,var2[j].get())
                var6.insert(j,var4[j].get())

            with open(file_path, 'r') as f:
                lines = f.readlines()
                for k, l in enumerate(lines):
                    item.append(k)
                for m in range(0,len(item)):
                    if item[m+1:m+2] == list(map(int,var5[n:n+1])):
                        new_string = var6[n]
                        n = n+1
                
                    else:
                        new_string = l.strip()

                    if new_string:
                        new_text_content += new_string + '\n'
                    else:
                        new_text_content += '\n'
                
            with open(file_path, 'w') as f:
                f.write(new_text_content)
                
        label_num = Label(tk,text='열 입력').place(x=5,y=3)
        entry_num = Entry(tk,width=3)
        entry_num.place(x=50,y=5)
        btn0 = Button(tk,text='생성',command=check).place(x=90,y=2)
        btn1 = Button(tk,text='초기화',command=reset).place(x=130,y=2)
        btn2 = Button(tk,text='확인',command=commit).place(x=930,y=350)

        tk.geometry('1000x400')
        tk.mainloop()


label_num = Label(tk,text='열 입력').place(x=5,y=3)
entry_num = Entry(tk,width=3)
entry_num.place(x=50,y=5)
btn0 = Button(tk,text='생성',command=check).place(x=90,y=2)
btn1 = Button(tk,text='초기화',command=reset).place(x=130,y=2)
btn2 = Button(tk,text='확인',command=commit).place(x=930,y=350)

tk.mainloop()