tkinter 파이썬 기본 UI 프레임 - 파일, 폴더 선택 등.. python

파일, 폴더 경로명 적어주기 귀찮아서 만들었습니다.
tkinter는 파이썬에 기본으로 들어있어 따로 라이브러리를 설치할 필요 없습니다.

파이썬으로 간단한 프로그램 만들때 사용할 기본 UI 프레임.
파일 선택, 폴더 선택, 텍스트 에디터, Run 으로 구성.





import tkinter
import tkinter.filedialog
import tkinter.messagebox


def open_file():
    path = tkinter.filedialog.askopenfilename(
        initialdir="/",
        title="Select file",
        filetypes=(("txt files", "*.txt"), ("all files", "*.*")),
    )
    return path


def save_file():
    path = tkinter.filedialog.asksaveasfilename(
        initialdir="/",
        title="Select file",
        filetypes=(("txt files", "*.txt"), ("all files", "*.*")),
    )
    return path


def select_folder():
    path = tkinter.filedialog.askdirectory()
    return path


def select_file():
    path = tkinter.filedialog.askopenfilename()
    return path


def insert_text(str):
    text_edit.insert(tkinter.END, str + "\n")
    text_edit.pack()


def run():
    try:
        insert_text("Run!")
        insert_text(select_file())


    except Exception as e:
        tkinter.messagebox.showinfo("Exception!", e)
        # with open('Log.txt', 'a', encoding='utf-8', errors='ignore') as f:
        #    f.write(e)


if __name__ == "__main__":
    root = tkinter.Tk()
    root.title("Simple")

    # menubar Start
    menubar = tkinter.Menu(root)

    ## menubar - File
    filemenu = tkinter.Menu(menubar, tearoff=0)
    menubar.add_cascade(label="File", menu=filemenu)
    filemenu.add_command(label="Open_File", command=open_file)
    filemenu.add_command(label="Save_File", command=save_file)
    filemenu.add_command(label="Select Folder", command=select_folder)
    filemenu.add_separator()
    filemenu.add_command(label="Exit", command=root.quit)

    ## menubar - Run
    runmenu = tkinter.Menu(menubar, tearoff=0)
    menubar.add_cascade(label="Run", menu=runmenu)
    runmenu.add_command(label="Run", command=run)

    root.config(menu=menubar)
    # menubar End


    # button
    button = tkinter.Button(
        root, text="Run", command=run, height=2, width=10, background="#d1d1e0"
    )
    button.pack()

    text_edit = tkinter.Text(root)
    text_edit.pack()

    insert_text("Hi!")

    root.mainloop()



댓글

이 블로그의 인기 게시물

파이썬 vscode에서 자동 코드 정렬. Formatter.

Unity3D git 저장소에 올릴때 필요없는 파일 제외하기. gitignore

플러터(flutter) 개발 참고 사이트들.