Advertisement
here2share

# rmv_comments.py

Apr 24th, 2025 (edited)
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. # rmv_comments.py
  2.  
  3. import tkinter as tk
  4. import re
  5.  
  6. def format_code(code, space_setting):
  7.     code = re.sub(r' +', ' ', code)
  8.     space_count = 4 if space_setting.get() == "4" else 2
  9.     code = re.sub(rf'^( {space_count})+', lambda m: '\t' * (len(m.group(0)) // space_count), code, flags=re.MULTILINE)
  10.     code = re.sub(r'\n\n+', '\n\n', code)
  11.     return code
  12.  
  13. def get_clipboard():
  14.     return root.clipboard_get()
  15.  
  16. def set_clipboard(text):
  17.     root.clipboard_clear()
  18.     root.clipboard_append(text)
  19.     root.update()
  20.  
  21. def process_code():
  22.     code = get_clipboard()
  23.     cleaned_code = format_code(code, space_setting)
  24.     output_text.delete("1.0", tk.END)
  25.     output_text.insert(tk.END, cleaned_code)
  26.  
  27. def copy_to_clipboard():
  28.     cleaned_code = output_text.get("1.0", tk.END).strip()
  29.     if cleaned_code:
  30.         set_clipboard(cleaned_code)
  31.  
  32. root = tk.Tk()
  33. root.title("Remove Comments")
  34.  
  35. frame = tk.Frame(root)
  36. frame.pack()
  37.  
  38. btn_paste = tk.Button(frame, text="Paste and Copy from Clipboard", command=process_code)
  39. btn_paste.grid(row=0, column=0)
  40.  
  41. space_setting = tk.StringVar(value="4")
  42.  
  43. label = tk.Label(frame, text="Replace leading spaces:")
  44. label.grid(row=0, column=1)
  45.  
  46. rb_4 = tk.Radiobutton(frame, text="4 spaces → Tabs", variable=space_setting, value="4")
  47. rb_2 = tk.Radiobutton(frame, text="2 spaces → Tabs", variable=space_setting, value="2")
  48.  
  49. rb_4.grid(row=0, column=2, sticky="w")
  50. rb_2.grid(row=0, column=3, sticky="w")
  51.  
  52. output_text = tk.Text(frame, height=20, width=120)
  53. output_text.grid(row=1, column=0, columnspan=4)
  54.  
  55. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement