-
Boilerplate CopierPython/이것저것 파이썬 2023. 12. 25. 01:42반응형
관용구를 txt파일로 관리하는 프로그램.
text 폴더에 txt파일 형식으로 저장해 두면 되고, 폴더는 1단계만 지원된다..
좌측 트리에서 파일을 선택하면 그 내용이 클립보드에 올라간다.
원하는 곳에 붙여 넣기만 하면 된다.
pip install pyperclip
import os import tkinter as tk from tkinter import ttk, filedialog, messagebox import pyperclip def browse_folder(): folder_path = filedialog.askdirectory() if folder_path: show_folder_structure(folder_path) def show_folder_structure(folder_path): tree.delete(*tree.get_children()) tree.heading("#0", text=os.path.abspath(folder_path), anchor='w') display_folder_contents(folder_path, '') def display_folder_contents(folder_path, parent): for item in os.listdir(folder_path): item_path = os.path.join(folder_path, item) is_folder = os.path.isdir(item_path) item_id = tree.insert(parent, 'end', text=item, open=False, tags=('folder' if is_folder else 'file', item_path)) if is_folder: display_folder_contents(item_path, item_id) def show_file_content(event): file_content_text.config(state=tk.NORMAL) file_content_text.delete('1.0', tk.END) if not tree.selection(): browse_folder() return selected_item = tree.selection()[0] tag, item_path = tree.item(selected_item, 'tags') if tag == 'file' and os.path.isfile(item_path): with open(item_path, 'r', encoding='utf-8') as file: content = file.read() pyperclip.copy(content) file_content_text.delete('1.0', tk.END) file_content_text.insert(tk.END, content) file_content_text.config(state=tk.DISABLED) messagebox.showinfo('Copied!', os.path.abspath(item_path)) root = tk.Tk() root.title("Boilerplate Copier") root.wm_attributes("-topmost", True) if os.path.isfile(path := os.path.join(os.path.dirname(__file__), 'file_icon.ico')): root.iconbitmap(path) # Folder Structure Frame frame = ttk.Frame(root) frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) tree = ttk.Treeview(frame) tree.heading("#0", text="Browser Folder First", anchor='w') scrollbar = ttk.Scrollbar(frame, orient='vertical', command=tree.yview) tree.configure(yscroll=scrollbar.set) scrollbar.pack(side='right', fill='y') tree.pack(expand=True, fill=tk.BOTH) browse_button = ttk.Button(frame, text="Browse", command=browse_folder) browse_button.pack(side=tk.BOTTOM) # File Content Frame file_content_frame = ttk.Frame(root) file_content_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True) file_content_text = tk.Text(file_content_frame, wrap=tk.WORD, height=20, width=50) file_content_text.pack(expand=True, fill=tk.BOTH) # Bind tree selection event to show file content tree.bind("<ButtonRelease-1>", show_file_content) if not os.path.isdir(r'.\text'): os.mkdir(r'.\text') show_folder_structure(r'.\text') root.mainloop()
반응형