Advertisement
here2share

# tk_Reverse_Findall.py

Dec 24th, 2022
1,030
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. # tk_Reverse_Findall.py
  2.  
  3. import tkinter as tk
  4. import re
  5. import tkinter.font as tkFont
  6.  
  7. def copy_regex():
  8.     regex = regex_label.cget("text")
  9.     root.clipboard_append(regex)
  10.  
  11. def find_regex(event):
  12.     input_string = input_string_entry.get()
  13.     expected_results_string = expected_results_entry.get("1.0", "end-1c")
  14.     actual_output_string = actual_output_entry.get("1.0", "end-1c")
  15.  
  16.     expected_results = expected_results_string.split("\n")
  17.     actual_output = actual_output_string.split("\n")
  18.     expected_results = [s.rstrip() for s in expected_results]
  19.  
  20.     possible_patterns = [
  21.         r"\b\d+\b",
  22.         r"\b[A-Z]\w*\b",
  23.         r"\b\w*ing\b"
  24.     ]
  25.  
  26.     for pattern in possible_patterns:
  27.         matches = re.findall(pattern, input_string)
  28.         if matches == expected_results:
  29.             regex_label.config(text=pattern)
  30.             matches_label.config(text=matches)
  31.             break
  32.  
  33. def clear_all():
  34.     input_string_entry.delete(0, "end")
  35.     expected_results_entry.delete("1.0", "end")
  36.     actual_output_entry.delete("1.0", "end")
  37.     regex_label.config(text="")
  38.     matches_label.config(text="")
  39.  
  40. root = tk.Tk()
  41. root.title("Reverse Findall")
  42.  
  43. data_label = tk.Label()
  44. input_string_label = tk.Label(text="Input String:")
  45. input_string_entry = tk.Entry(root)
  46. font = tk.font.Font(root, input_string_entry.cget("font"))
  47. char_width = font.measure("M")
  48. canvas_width = root.winfo_width()
  49. entry_width = int(canvas_width / char_width) # ???
  50. input_string_entry.config(width=214)
  51.  
  52. expected_results_label = tk.Label(text="Expected Results (Separated By Newlines):")
  53. expected_results_entry = tk.Text(root)
  54.  
  55. actual_output_label = tk.Label(text="Actual Output (Separated By Newlines):")
  56. actual_output_entry = tk.Text(root)
  57.  
  58. copy_regex_button = tk.Button(text="Copy Regex", command=copy_regex)
  59. clear_button = tk.Button(text="Clear", command=clear_all)
  60.  
  61. regex_label = tk.Label(text="")
  62. matches_label = tk.Label(text="")
  63.  
  64. input_string_label.grid(row=0, sticky="W")
  65. input_string_entry.grid(row=1, columnspan=2)
  66. expected_results_label.grid(row=2, column=0, sticky="W")
  67. expected_results_entry.grid(row=3, column=0)
  68. actual_output_label.grid(row=2, column=1, sticky="W")
  69. actual_output_entry.grid(row=3, column=1)
  70. copy_regex_button.grid(row=4, column=0, sticky="W")
  71. clear_button.grid(row=4, column=1, sticky="W")
  72. regex_label.grid(row=5, column=0, sticky="W")
  73. matches_label.grid(row=5, column=1, sticky="W")
  74.  
  75. root.bind("<Key>", find_regex)
  76.  
  77. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement