Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_Reverse_Findall.py
- import tkinter as tk
- import re
- import tkinter.font as tkFont
- def copy_regex():
- regex = regex_label.cget("text")
- root.clipboard_append(regex)
- def find_regex(event):
- input_string = input_string_entry.get()
- expected_results_string = expected_results_entry.get("1.0", "end-1c")
- actual_output_string = actual_output_entry.get("1.0", "end-1c")
- expected_results = expected_results_string.split("\n")
- actual_output = actual_output_string.split("\n")
- expected_results = [s.rstrip() for s in expected_results]
- possible_patterns = [
- r"\b\d+\b",
- r"\b[A-Z]\w*\b",
- r"\b\w*ing\b"
- ]
- for pattern in possible_patterns:
- matches = re.findall(pattern, input_string)
- if matches == expected_results:
- regex_label.config(text=pattern)
- matches_label.config(text=matches)
- break
- def clear_all():
- input_string_entry.delete(0, "end")
- expected_results_entry.delete("1.0", "end")
- actual_output_entry.delete("1.0", "end")
- regex_label.config(text="")
- matches_label.config(text="")
- root = tk.Tk()
- root.title("Reverse Findall")
- data_label = tk.Label()
- input_string_label = tk.Label(text="Input String:")
- input_string_entry = tk.Entry(root)
- font = tk.font.Font(root, input_string_entry.cget("font"))
- char_width = font.measure("M")
- canvas_width = root.winfo_width()
- entry_width = int(canvas_width / char_width) # ???
- input_string_entry.config(width=214)
- expected_results_label = tk.Label(text="Expected Results (Separated By Newlines):")
- expected_results_entry = tk.Text(root)
- actual_output_label = tk.Label(text="Actual Output (Separated By Newlines):")
- actual_output_entry = tk.Text(root)
- copy_regex_button = tk.Button(text="Copy Regex", command=copy_regex)
- clear_button = tk.Button(text="Clear", command=clear_all)
- regex_label = tk.Label(text="")
- matches_label = tk.Label(text="")
- input_string_label.grid(row=0, sticky="W")
- input_string_entry.grid(row=1, columnspan=2)
- expected_results_label.grid(row=2, column=0, sticky="W")
- expected_results_entry.grid(row=3, column=0)
- actual_output_label.grid(row=2, column=1, sticky="W")
- actual_output_entry.grid(row=3, column=1)
- copy_regex_button.grid(row=4, column=0, sticky="W")
- clear_button.grid(row=4, column=1, sticky="W")
- regex_label.grid(row=5, column=0, sticky="W")
- matches_label.grid(row=5, column=1, sticky="W")
- root.bind("<Key>", find_regex)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement