tkinter 파이썬 기본 UI 프레임 - 파일, 폴더 선택 등.. python
파일, 폴더 경로명 적어주기 귀찮아서 만들었습니다.
파일 선택, 폴더 선택, 텍스트 에디터, 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", "*.*")),
)
def save_file():
path = tkinter.filedialog.asksaveasfilename(
initialdir="/",
title="Select file",
filetypes=(("txt files", "*.txt"), ("all files", "*.*")),
)
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!")
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 = tkinter.Menu(root)
# 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)
# button
button = tkinter.Button(
root, text="Run", command=run, height=2, width=10, background="#d1d1e0"
)
button.pack()
# Run
runmenu = tkinter.Menu(menubar, tearoff=0)
menubar.add_cascade(label="Run", menu=runmenu)
runmenu.add_separator()
runmenu.add_command(label="Run", command=run)
runmenu.add_separator()
root.config(menu=menubar)
text_edit = tkinter.Text(root)
text_edit.pack()
insert_text("Hi!")
root.mainloop()
댓글
댓글 쓰기