Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # rmv_comments.py
- import tkinter as tk
- import re
- def format_code(code, space_setting):
- code = re.sub(r' +', ' ', code)
- space_count = 4 if space_setting.get() == "4" else 2
- code = re.sub(rf'^( {space_count})+', lambda m: '\t' * (len(m.group(0)) // space_count), code, flags=re.MULTILINE)
- code = re.sub(r'\n\n+', '\n\n', code)
- return code
- def get_clipboard():
- return root.clipboard_get()
- def set_clipboard(text):
- root.clipboard_clear()
- root.clipboard_append(text)
- root.update()
- def process_code():
- code = get_clipboard()
- cleaned_code = format_code(code, space_setting)
- output_text.delete("1.0", tk.END)
- output_text.insert(tk.END, cleaned_code)
- def copy_to_clipboard():
- cleaned_code = output_text.get("1.0", tk.END).strip()
- if cleaned_code:
- set_clipboard(cleaned_code)
- root = tk.Tk()
- root.title("Remove Comments")
- frame = tk.Frame(root)
- frame.pack()
- btn_paste = tk.Button(frame, text="Paste and Copy from Clipboard", command=process_code)
- btn_paste.grid(row=0, column=0)
- space_setting = tk.StringVar(value="4")
- label = tk.Label(frame, text="Replace leading spaces:")
- label.grid(row=0, column=1)
- rb_4 = tk.Radiobutton(frame, text="4 spaces → Tabs", variable=space_setting, value="4")
- rb_2 = tk.Radiobutton(frame, text="2 spaces → Tabs", variable=space_setting, value="2")
- rb_4.grid(row=0, column=2, sticky="w")
- rb_2.grid(row=0, column=3, sticky="w")
- output_text = tk.Text(frame, height=20, width=120)
- output_text.grid(row=1, column=0, columnspan=4)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement